简体   繁体   中英

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. 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.

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM