Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
Convert a JSON Object to an index based JSON Array in JavaScript

I have a JSON object in the below format which I want to convert into a JSON array. I have tried multiple ways to achieve this, but I can not get success.

{
   "name":{
      "0":"mike",
      "1":"george",
      "2":"Andrew"
   },
   "category":{
      "0":"A",
      "1":"B",
      "2":"C"
   }
}

The output would be like this:

{
   "0":{
      "name":"mike",
      "category":"A"
   },
   "1":{
      "name":"george",
      "category":"B"
   },
   "2":{
      "name":"andrew",
      "category":"C"
   }
}

I am new to JSON. How can I achieve this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can make use of Object.keys and Object.entries

const obj = {
  name: {
    "0": "mike",
    "1": "george",
    "2": "Andrew",
  },
  category: {
    "0": "A",
    "1": "B",
    "2": "C",
  },
};
const props = Object.keys(obj);
const result = props.reduce((acc, key) => {
  Object.entries(obj[key]).forEach(([k, v]) => {
    if (!acc[k]) acc[k] = Object.fromEntries(props.map((p) => [p, ""]));
    acc[k][key] = v;
  });
  return acc;
}, {});

console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!