0

I want to loop on a JSON data and put a part of it in a sperate array.

I have this JSON data:

{"msg":"[{"parent":"0","child":"1"},{"parent":"0","child":"2"}]","sig":"3045022100b3f03c83ed1f3d0bc72475550f23c0dd38277b0e2230e1fe76611577b2ca4b7e022037c77d37cc08ed897d0d1a11d09342e6abb73e641d20d5d55f367044d5489ddf"}

I want to make an array of msg only like this:

[ '0', '1', '0', '2 ]

My code:

var message = '[{"msg":[{"parent":"0","child":"1"},{"parent":"0","child":"2"}],"sig":"3045022100b3f03c83ed1f3d0bc72475550f23c0dd38277b0e2230e1fe76611577b2ca4b7e022037c77d37cc08ed897d0d1a11d09342e6abb73e641d20d5d55f367044d5489ddf"}]'
var g = [];
const jsArray = JSON.parse(message);
Array.from(jsArray.msg).forEach(jsdata => {
  console.log(jsdata)
  g.push(jsdata.parent, jsdata.child)
})
console.log(g);

I could not meet what I really want it says:

[
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined, undefined, undefined,
  undefined, undefined, undefined, undefined,
  ... 10 more items
]

How could I solve this, please? Thanks in advance

5
  • Does msg contain String value? If so, you need to JSON.parse that as well: Array.from(JSON.parse(jsArray.msg)) Commented Mar 6, 2021 at 17:10
  • 1
    The JSON string is not valid JSON. The array contained in msg cannot be quoted to be legit. Commented Mar 6, 2021 at 17:17
  • As you can see, your code produces a syntax error because the JSON is malformed. Please edit your question to provide the actual JSON string. Commented Mar 6, 2021 at 17:24
  • @RandyCasburn Thanks! you're right. I have edited the code. Commented Mar 6, 2021 at 17:25
  • Great - please see my answer. Commented Mar 6, 2021 at 17:27

3 Answers 3

2

It is as simple as using the .map method to gather the data and then flatting the final array to get you output.

var message ='[{"msg":[{"parent":"0","child":"1"},{"parent":"0","child":"2"}],"sig":"3045022100b3f03c83ed1f3d0bc72475550f23c0dd38277b0e2230e1fe76611577b2ca4b7e022037c77d37cc08ed897d0d1a11d09342e6abb73e641d20d5d55f367044d5489ddf"}]';

let data = JSON.parse(message);
let output = data[0].msg.map(d=>[d.parent,d.child]).flat();
console.log(output);

Sign up to request clarification or add additional context in comments.

Comments

1

Try the code below to loop the inner array:

    var message ='[{"msg":[{"parent":"0","child":"1"},{"parent":"0","child":"2"}],"sig":"3045022100b3f03c83ed1f3d0bc72475550f23c0dd38277b0e2230e1fe76611577b2ca4b7e022037c77d37cc08ed897d0d1a11d09342e6abb73e641d20d5d55f367044d5489ddf"}]';
    let g=[];
    const jsArray = JSON.parse(message);
    jsArray.forEach(jsMsgData => {
         // console.log(jsMsgData.msg);
         jsMsgData.msg.forEach(jsdata => {
            g.push(jsdata.parent, jsdata.child);
         })
    });
    console.log(g);

check full code here: https://ideone.com/QywYfb

4 Comments

I assume he is getting data from an api call so json format should be correct or else he won't get lots of undefined values pushed in g right
Thanks for answering! The link of full code is not working. Could you please check the link?
Its working for me I checked in incognito mode so no access issues. Maybe your firewall is blocking it.
I converted it into code snippet now try it @ismsm
0

For working with JSON as a JS you will have to use JSON.parse(). This will spread your JSON object to an Array instance with its attribute . Then you can loop over the array and do whatever you like. When you are done with the instance and you want to pass it to a function , use JSON.stringify() to convert the Object to a string (which you can write or pass)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.