0

I have the following structure in my Javascript which then outputs Json:

request.on('row', function(columns) {
        trans.push({
          key : columns[0].value,
          value :columns[1].value,
          locale : columns[2].value
        });
    });

The current format which it creates:

 {
  "key": "keyname1",
  "value": "some value in here",
  "locale": "ar_AE"
 },
 {
  "key": "keyname2",
  "value": "some value in here",
  "locale": "ar_AE"
 }

But would like the structure of the json to be like the following:

{
  "keyname1": {
  "value": "some value in here",
  "locale": "ar_AE"
},
  "keyname2": {
  "value": "some value in here",
  "locale": "ar_AE"
 }

Seem to be a bit stuck on how to this, can anyone help??

user1177860
  • 509
  • 4
  • 12
  • 24
  • possible duplicate of [Javascript: How to generate formatted easy-to-read JSON straight from an object?](http://stackoverflow.com/questions/3515523/javascript-how-to-generate-formatted-easy-to-read-json-straight-from-an-object) –  Feb 03 '15 at 13:26

1 Answers1

1

So it seems like you want to generate an object of objects, not a list of object.

For this, you just need to rewrite your JS a bit to store result as object inside your trans object:

// assuming "trans" is an Object, NOT an Array
request.on('row', function(columns) {
    trans[columns[0].value] = {
      value :columns[1].value,
      locale : columns[2].value
    };
});
Andrew Dunai
  • 3,061
  • 19
  • 27