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

123
Views
Merge array of dynamically generated objects into single object

I'm struggling to convert the below JSON into expected JSON format using JavaScript.

Current JSON :

    {
  "furniture": {
    "matter": [
      {
        "matter1": "Matter 1 value"
      },
      {
        "matter2": "Matter 2 value"
      },
      {
        "matter3": "Matter 3 value"
      }
    ],
    "suspense": [
      {
        "suspense1": "suspense 1 value"
      },
      {
        "suspense2": "suspense 2 value"
      }
    ],
    "Direct": [
      {
        "System": "System value"
      }
    ],
    "Road": [
      {
        "Road key 1 ": "Road value "
      }
    ]
  }
}

expected JSON:

{
  "furniture": {
    "matter": {
      "matter1": "Matter 1 value",
      "matter2": "Matter 2 value",
      "matter3": "Matter 3 value"
    },
    "suspense": {
      "suspense1": "suspense 1 value",
      "suspense2": "suspense 2 value"
    },
    "Direct": {
      "System": "System value"
    },
    "Road": {
      "Road key 1 ": "Road value "
    }
  }
}

Note: furniture in above code is only static. Apart from that all keys and values are dynamically generated.

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

0

You could look for arrays and create a joined object, otherwise flat the deeper level.

const
    flat = object => Object.fromEntries(Object
        .entries(object)
        .map(([k, v]) => [k, Array.isArray(v)
            ? Object.assign({}, ...v)
            : v && typeof v === 'object' ? flat(v) : v
        ])),
    data = { furniture: { matter: [{ matter1: "Matter 1 value" }, { matter2: "Matter 2 value" }, { matter3: "Matter 3 value" }], suspense: [{ suspense1: "suspense 1 value" }, { suspense2: "suspense 2 value" }], Direct: [{ System: "System value" }], Road: [{ "Road key 1 ": "Road value " }] }, Diversity: { "Sistema de direção": "Hidráulica" } },
    result = flat(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

try this

var furniture = {};
for (const property in jsonOrig.furniture) {
  var newObj = {};
  jsonOrig.furniture[property].forEach((value) => {
    for (const prop in value) {
      newObj[prop] = value[prop];
    }
    furniture[property] = newObj;
  });
}

var newJson = { furniture: furniture };
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!