1

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.

2
  • 3
    you have an object, and you want a nested array ... there is no JSON involved in the question or any potential answer Commented Jan 24, 2017 at 5:59
  • Go through this : stackoverflow.com/questions/4162749/… Commented Jan 24, 2017 at 6:00

2 Answers 2

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);

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

Comments

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);

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.