0

I have a json request input below and would want to construct a specific json format that the backend needs using javascript. What is the best way or steps to construct them? Any help would be appreciated! Thanks.

Request input :

{
"Product": "abc"'
"Data": "{"Name":"John","Email":"[email protected]"}"
}

Request output to the backend as follow:

{
  "variables": {
    "Product": {
      "value": "abc",
      "type": "string"
    },
    "Data": {
      "value": "{"Name":"John","Email":"[email protected]"}",
      "type": "string"
    }
  },
  "Key": "123"
}

Thanks in advance

1 Answer 1

1

You can just map over all the entries in your object.

Documentation: Object.entries() Array.reduce()

const input = {
  Product: "abc",
  Data: "{\"Name\":\"John\",\"Email\":\"[email protected]\"}"
};
const variables = Object.entries(input).reduce((output, [key, value]) => {
  output[key] = {
    type: typeof value,
    value
  };
  return output;
}, {});

const result = {
  variables,
  key: '123'
};
console.log(result);

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

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.