1

I have a Json object coming from my api as:

{
    "user1": ["report_dev", "report_test"],
    "user2": ["output1", "output2", "output3", "output4"]
}

I want to convert this to this format:

[{ name: 'user1' }, { name: 'user2' }];

This must be simple enough but not sure about this. Do I have to loop through json and create custom one.

I tried with below code but this is not giving my expected result:

var array =[];
for (i in array1)
{
    //array1 holds the json object
    array.push('name', array1[i]]);
}

Thanks for looking into.

Updated:

  details1:
     user1: (2) ["report_dev", "report_test"]
     user2: (4) ["output1", "output2", "output3", "output4"]

  details2:
     user1: (2) ["report_dev", "report_test"]
     user2: (4) ["output1", "output2", "output3", "output4"]
2
  • 3
    I think you're after something like this: Object.keys(array1).map(name => ({name}));, but can make your input a little clearer? Commented Jul 1, 2019 at 13:59
  • I have updated my question.does this help? Commented Jul 1, 2019 at 14:06

2 Answers 2

2

You can iterate into the json you received from the API as Nick Parsons proposes:

const fetchedData = {"user1":["report_dev", "report_test"], "user2": ["output1", "output2", "output3", "output4"]}

const result = Object.keys(fetchedData).map((key) => {
   return {
       name: key,
       data: fetchedData[key],
   }
})

console.log(result)

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

4 Comments

This is fine but with this I dont get my data associated with user1 and user2 ie ["report_dev", "report_test"] and ["output1", "output2", "output3", "output4"] .Sorry I dint mention this in my post
@user1563677 take a look at your desired output. It doesn't include those arrays :/
Okay, updated, but next time specify better what do you want
Sorry about this but I now realize I am getting complex json back as mentuioned in my updated post above. I wrote too soon, should have analysed my json first. Would appreciate if this can be looked into. Will certainly post full code going further
1

Your approach currently isn't working as you're trying to push two values into your array as separate items/elements. You want to .push() an object in your desired format that you've included in your output array:

var array = [];
for (var i in array1) { // array1 holds the object (it is NOT an array)
    array.push({"name": i, "data": array1[i]});
}

Alternatively, you could get an array of key-value pairs keys from your object using Object.entries() and then map (using .map()) that to its own object:

var array = Object.entries(array1).map(([name, data]) => ({name, data}));

EDIT

As per your edit, you'll need to iterate over your outer details object keys. This can be done by using Object.entries again, and re-mapping each detail key to point to your re-mapped inner object like so:

const obj = {
  "details1": {
    "user1": ["report_dev", "report_test"],
    "user2": ["output1", "output2", "output3", "output4"]
  },

  "details2": {
    "user1": ["report_dev", "report_test"],
    "user2": ["output1", "output2", "output3", "output4"]
  }
}

const res = Object.assign({}, ...Object.entries(obj).map(([det, obj]) => ({
   [det]: Object.entries(obj).map(([name, data]) => ({name, data}))
})));

console.log(res);

This can also be done using Object.fromEntries() by mappying to an array of [key, value] pairs:

const obj = {
  "details1": {
    "user1": ["report_dev", "report_test"],
    "user2": ["output1", "output2", "output3", "output4"]
  },

  "details2": {
    "user1": ["report_dev", "report_test"],
    "user2": ["output1", "output2", "output3", "output4"]
  }
}

const res = Object.fromEntries(Object.entries(obj).map(
  ([det, obj]) => [det, Object.entries(obj).map(([name, data]) => ({name, data}))]
));

console.log(res);

3 Comments

Thanks Nick. Sorry for this but one of my data is returning parent child json as updated above.
what is your desired output now? Can you update the question again please
Everything remains same, I just want to update my original json object with "name": "user1" and "name": "user2" rest of json remains same as it is

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.