2

The strings that are coming from source is coming in fragmented JSON object in JSON object.I want to convert this JSON structure to a another simple JSON structure:

{  
  "nestA":{  
    "a":"link",
    "b":2711
  },
  "nestB":{  
    "a":"img",
    "b":4165 
  }
}

could it changed to be like

 {  
     "key":"nestA"
     "a":"link"
     "b":711
 },
 {  
     "key":"nestB"
     "a":"img"
     "b":165
 }
AmericanUmlaut
  • 2,817
  • 2
  • 17
  • 27
Kei Shuri
  • 77
  • 1
  • 5
  • firstly, JSON is a string, so you need to JSON.parse it to work with an object ... once you've created the object in the desired format, you'll need to JSON.stringify the object to get back to JSON – Jaromanda X Feb 23 '17 at 22:02
  • You can get a list of an object's keys by using [`Object.keys(some_object)`](http://stackoverflow.com/a/8763227/1848578). Then you could loop over those, adding to a new array in the structure you want. – qxz Feb 23 '17 at 22:04
  • 1
    Are you trying to turn it into an array? Part two is not valid JSON – lonewarrior556 Feb 23 '17 at 22:04
  • Also, can you give us some context about how you're using this? Context will always help people find the best solution for you :) – qxz Feb 23 '17 at 22:06
  • Sorry for a bad format of a question, I am relatively new to stackoverflow and I cant find how to edit the question I posted so I am adding a comment. The question is in the context of pentaho data integration, in which I am getting a json string of the above mentioned type, the default JSON input step plugin cant do the way I want it to do, so I try it do by modified Javascript plugin, but my knowledge of javascript is mediocre, I tried but couldnt do it. Thank you all for responding in a short time. – Kei Shuri Feb 23 '17 at 22:29

3 Answers3

2

//convert json string into an object
var json = '{"nestA":{"a":"link","b":2711},"nestB":{"a":"img","b":4165}}'
var sourceObject = JSON.parse(json);

//get a list of the object keys
var keys = Object.keys(sourceObject);

//create a new array to hold our output
var array = [];

//loop through our keys adding them to our array in the format we want
keys.forEach(function(key){
 var nest = sourceObject[key];
 nest.key = key;
 array.push(nest);
});

//convert our array back to json
var result = JSON.stringify(array);
console.log(result);
FreakinaBox
  • 421
  • 1
  • 4
  • 15
1

Working with JSON it's best to parse it first, modify it and then turn it back into JSON:

var orgObj = JSON.parse(orgJSON);
var newArr = [];

for( var key in orgObj){
  var temp = { key: key };
  Object.assign( temp, orgObj[key]);
  newArr.push(temp);
}

var newJSON = JSON.stringify(newArr);

Assuming an array was what you were going for. Please clean up your desired JSON.

Without using es6 the for loop could be:

 for( var key in orgObj){
      orgObj[key].key = key;
      newArr.push( orgObj[key] );
 }

but orgObj would get modified.

lonewarrior556
  • 3,917
  • 2
  • 26
  • 55
1

var json = [{  
  "nestA":{  
     "a":"link",
     "b":2711

  },
  "nestB":{  
     "a":"img",
     "b":4165
  }
}];
  
var arr = [];
json.forEach(function(v) {
  Object.keys(v).forEach(c => arr.push(v[c]));
  arr.forEach((b, x) => b['key'] = Object.keys(v)[x]);
});

console.log(arr);
kind user
  • 40,029
  • 7
  • 67
  • 77