[英]Json jolt transformation from attributes to key value pair
Im trying to transform something like this:我试图改变这样的东西:
{
"Id":"123",
"Att1":"value1",
"Att2":"value2",
...
"Attn":"valuen"
}
To:至:
{
"Id":"123",
"AttJson": "{\\"Att1\\":\\"Value1\\",\\"Att2\\":\\"Value2\\",...,\\"Attn\\":\\"Valuen\\"}"
}
So basically keep the id as is but wrap remaining attributes in one json sting that will be a value of one key.因此,基本上保持 id 不变,但将剩余属性包装在一个 json 字符串中,该字符串将是一个键的值。 Is this possible?这可能吗? Thank you谢谢
Yes, the one which you are looking for(as per my understanding) is possible.是的,您正在寻找的(根据我的理解)是可能的。 Below is the utlity for the same.以下是相同的实用程序。
It'll take data
as input and keep the Id
as is.它将data
作为输入并保持Id
不变。
let data = { Id: '123', Att1: 'value1', Att2: 'value2', Attn: 'valuen', }; const formatJSON = (data) => { const dataCopy = {...data }; const idVal = dataCopy.Id; delete dataCopy.Id; return { Id: idVal, AttJson: JSON.stringify(dataCopy) } } console.log(formatJSON(data)) console.log(data)
.as-console-wrapper { max-height: 100%;important; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.