0

I currently have an existing JSON that I want to change/reformat into a new JSON to be able to be used in an external service. The format is a bit complicated but I can't change it, so I have to edit my existing JSON. to match my desired output.

Existing JSON:

{
    "specifiers": [{
        "value": "test",
        "type": "text",
        "label": "Brand ID"
    }, {
        "value": "test",
        "type": "text",
        "label": "Program ID"
    }]
}

Desired Output:


{
    "specifiers": {
        "Brand ID": {
            "text": {
                "value": "test",
                "type": "text"
            }
        },

        "Program ID": {
            "text": {
                "value": "test",
                "type": "text"
            }
        }
    }
}

I've tried iterating through the existing JSON using loops, but I don't really know how to format my loops to use the values as the keys? I'm guessing that I might have to use Object.keys or Object.values, but I'm not sure how to get a specific value for a specific key.

Example Format:

        "[label]": {
            "[type]": {
                "value": [value],
                "type": [type]
            }
        }
2
  • So you always receive it in the same input format and want to transform it to the same output format? Commented Mar 13, 2019 at 19:42
  • The first option is imo a better structure. The reason why? Unless you know that "specifiers" contains "Brand ID" and "Program ID" properties ahead of time (similar to how you know that "document" contains "querySelector" and "getElementById" methods), you will have to loop over the object's properties, in which case you might as well have used an array. No accounting for requirements of APIs, though. Commented Mar 13, 2019 at 19:54

3 Answers 3

3

function tranform({specifiers}) {
  return { specifiers: specifiers.reduce((obj, {label, type, value}) => ({...obj, [label]: { [type]: { type, value } } }), {}) }
}

const json = {
    "specifiers": [{
        "value": "test",
        "type": "text",
        "label": "Brand ID"
    }, {
        "value": "test",
        "type": "text",
        "label": "Program ID"
    }]
}

console.log(tranform(json))

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

3 Comments

Why not map instead of reduce?
@BrunoEly map transform childs of array and returns new array (with transformed childs), but OP needs an object instead of an array
You're right, for a second I thought specifiers was an array in the output
0

Pretty straightforward with a reduce:

const formattedSpecifiers = existingJSON.specifiers.reduce((newSpecifiers, specifier) => {
  newSpecifiers[specifier.label] = {
      [specifier.type]: {
        type: specifier.type,
        value: specifier.value,
      },
    };
  };

  return newSpecifiers;
}, {});

const newJSON = { specifiers: formattedSpecifiers };

Comments

0

you can use #Array.reduce. snippet below.

let input = {
  "specifiers": [{
    "value": "test",
    "type": "text",
    "label": "Brand ID"
  }, {
    "value": "test",
    "type": "text",
    "label": "Program ID"
  }]
}
const res = input.specifiers.reduce((res, obj) => {
  const {
    label,
    type,
    value
  } = obj
  res[label] = {};
  res[label][type] = {
    value,
    type
  };
  return res;
}, {});
console.log(res);

Comments

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.