i have a jsonStrng like var sourceJsonStr= {"foo":25,"xyz":49}; I want similar in JSON object like var targetStrJson = [['foo', 25], ['xyz', 49]]. How do convert sourcejson to targetjson in javascript.
            Asked
            
        
        
            Active
            
        
            Viewed 56 times
        
    1
            
            
         
    
    
        glen maxwell
        
- 193
- 1
- 3
- 13
- 
                    3you have an object, and you want a nested array ... there is no JSON involved in the question or any potential answer – Jaromanda X Jan 24 '17 at 05:59
- 
                    Go through this : http://stackoverflow.com/questions/4162749/convert-js-object-to-json-string – Nitesh Singh Rajput Jan 24 '17 at 06:00
2 Answers
4
            Here's one way to do it:
var source = {"foo": 25, "xyz": 49};
var target = Object.keys(source).map(key => [key, source[key]]);
console.log(target); 
    
    
        Robby Cornelissen
        
- 91,784
- 22
- 134
- 156
0
            
            
        Another way to do this.
var sourceJsonStr= {"foo":25,"xyz":49}; 
var targetStrJson = [];
for(var key in sourceJsonStr){
   targetStrJson.push([key, sourceJsonStr[key]]);
}
console.log(targetStrJson);Using .map in es5
var sourceJsonStr = {
  "foo": 25,
  "xyz": 49
};
var targetStrJson = Object.keys(sourceJsonStr).map(function(key){
  return [key, sourceJsonStr[key]];
});
console.log(targetStrJson); 
    
    
        Jyothi Babu Araja
        
- 10,076
- 3
- 31
- 38