0

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.

4
  • You've got this tagged "typescript"; does that mean you care about strongly typing your array-merging function's output? Or do you just care about the runtime behavior? Either way, does this meet your needs? If so I can write up an answer; if not, what am I missing? Commented Feb 9, 2022 at 18:41
  • Thanks!! Works fine Commented Feb 9, 2022 at 19:10
  • So do you care about types or not? If you're happy with the answer below then I guess you don't, because TypeScript types are absent there and I get compiler warnings about implicit any. Is this really a "typescript" question or do you just care about runtime? Commented Feb 9, 2022 at 19:19
  • No it's just runtime behavior. And your answer works absolutely fine for me. :) Commented Feb 9, 2022 at 19:21

2 Answers 2

3

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; }

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

5 Comments

Getting ERROR RangeError: Maximum call stack size exceeded
do you have object with a single value (no array, no object)?
Yes, "Diversity": { "Sistema de direção": "Hidráulica" },
then you need a check for object as well. please see edit.
Thanks!! works perfectly
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 };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.