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":"john@example.com"}"
}
Request output to the backend as follow:
{
"variables": {
"Product": {
"value": "abc",
"type": "string"
},
"Data": {
"value": "{"Name":"John","Email":"john@example.com"}",
"type": "string"
}
},
"Key": "123"
}
Thanks in advance
You can just map over all the entries in your object.
Documentation: Object.entries() Array.reduce()
const input = { Product: "abc", Data: "{\"Name\":\"John\",\"Email\":\"john@example.com\"}" }; 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.