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??

1

1 Answer 1

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
    };
});
Sign up to request clarification or add additional context in comments.

2 Comments

That's it. I was thinking in the same solution. But I'm not sure if you have to create the trans object first like 'var trans=new Object()'
@DavidCosta It's OK to create it like var trans = {};

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.