1

I have a set of array like this : [["Sarah"],["Jamie"],["B"],["148"]]

and I want to convert this into JSON string with specific element for each vaues. For example,

{ "name":"Sarah", "grade":"148", "School":"B"...}

How should I proceed? I tried to toString the array then bind with this element but it doesn't work out well..

Original Json

   "Data":{
      "Table":[
         {
            "Name":[
               "Jamie"
            ],
            "School":[
               "A"
            ],
            "grade":[
               "99"
            ]

         },
         {
            "Name":[
               "Mike"
            ],
            "School":[
               "B"
            ],
            "grade":[
               "148"
            ]
         }
      ]
   }
}
11
  • I was initially getting an JSON object from web-service. I had to some parse, and got the specific values for each of names but now I have to return it with names.. does it make sense? For example {"Sarah": "1", "Jamie": "59", "Mike" : "3"..} then parsing Commented Nov 8, 2017 at 21:43
  • What is the angular part ? If there is none, please remove the tag Commented Nov 8, 2017 at 21:45
  • @Xufox - updated in post. Thanks. Not sure below answer by Ori Dori is the best way to work for me.. I know that will work but Is there any better way to do this? Commented Nov 8, 2017 at 21:48
  • Do you really need to nest each number string in another array? Just ["1", "2", "3", "4"] would be simpler. Commented Nov 8, 2017 at 21:51
  • I don't need to.. but that's what I get at the end.. Commented Nov 8, 2017 at 21:53

3 Answers 3

1

You can try with the simple forEach

var data =   {"Data": {"Table": [{"Name": ["Jamie"],"School": ["A"],"grade": ["99"]},{"Name": ["Mike"],"School": ["B"],"grade": ["148"]}]}};

var items = [];
data['Data']['Table'].forEach(function(item){
  items.push({name: item.Name[0], grade: item.grade[0], school: item.School[0]});
});

console.log(JSON.stringify(items));

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

Comments

0

You can use reduce to do this!

let newData = data.Data.Table.reduce(function(arr, obj) {
    let newObj = {};
    for (let key in obj) {
        newObj[key] = obj[key][0]
    }

    arr.push(newObj);
    return arr;
}, [])

Demo: https://jsfiddle.net/500eo2gp/

1 Comment

Thanks for your suggestion, but I don't want all array to be retrieved into JSON string. I want only values returned to be JSON string with each of names. For now, I only returned [["Sarah"],["Jamie"],["B"],["148"]], so I just want JSON string for this arrys along with their element names. Just like this [ "Name" : "Sarah", "Name" : "Jamie", "School":"B", "Grade" : "148"] and ignore rest
0
let newDataArray = this.data.Data.Table.reduce(function(arr, obj) {
  let newObj = {};
  for (let key in obj) {
    newObj[key] = obj[key][0]
  }

  arr.push(newObj);
    return arr;
  }, []
);
newData =JSON.stringify(newDataArray);

JSON.stringify(newDataArray) array from tymeJV 's code snippet will give you the JSON string as follows.

[{"Name":"Jamie","School":"A","grade":"99"},{"Name":"Mike","School":"B","grade":"148"}]

Demo: http://plnkr.co/edit/wPhVTOFhRgERLXKCuoYl?p=preview

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.