Jane
Jane

Reputation: 707

How to change the format of a JSON data in Javascript?

I get a JSON data with this format:

{
 key1: val1,
 key2: val2,
 key3: val3,
 key4: val4,
 ..
}

Now I want to change it to another format in Javascript:

[
    {key: "key1", value: value1},
    {key: "key2", value: value2},
    {key: "key3", value: value3},
    {key: "key4", value: value4},   
    ...     
]

Is there some libraries that can do this? Or I have to write a function?

Please give me some tips! Thanx!

Upvotes: 1

Views: 89

Answers (1)

David Hedlund
David Hedlund

Reputation: 129782

var newObject = Object.keys(originalObject).map(function(k) { 
  return { key: k, value: originalObject[k] };
});

Upvotes: 7

Related Questions