I have a json input as follows, need to replace the machine key with it's name and then need to have following output json with the required mapping. Could someone help on this.
distance -> "code" : "X2-P1"
temp -> "code" : "X1-P1"
weight -> "code" : "weight"
Input JSON
[
[
{
"id": 26359,
"name": "station green"
},
{
"id": 22321,
"name": "ravenstein"
}
],
{
"nxt": "https://xxx/api/v3/w1",
"results": [
{
"time": "2024-06-16T21:30:40.289705Z",
"machine": 22321,
"temp": 18,
"distance": 0.113,
"long": 0.7866,
"weight": 0.2563
},
{
"time": "2024-06-16T19:02:16.970647Z",
"machine": 26359,
"temp": 27,
"distance": 0.453,
"site": 2219,
"weight": 7.0261
}
]
}
]
Output JSON
[
{
"stationname": "ravenstein",
"Date": "2024-06-16 21:30:40",
"data": [
{
"value": "0.113",
"code": "X2-P1"
},
{
"value": "18.0",
"code": "X1-P1"
},
{
"value": 0.2563,
"code": "weight"
}
]
},
{
"stationname": "station green",
"Date": "2024-06-16 19:02:16",
"data": [
{
"value": "0.453",
"code": "X2-P1"
},
{
"value": "27.0",
"code": "X1-P1"
},
{
"value": 7.0261,
"code": "weight"
}
]
}
]
Thank you.
You can use the following transformation :
[
{ //partition by id vs. machine attributes in order to match by their values
//and reform objects with the keys of their matched values to accumulate the attributes
//of common id and machine values
"operation": "shift",
"spec": {
"*": {
"*": {
"name": "@1,id.station&"//reanme as desired this and the ones below
},
"results": {
"*": {
"time": "@1,machine.Date",
"distance": "@1,machine.data.X2-P1",
"temp": "@1,machine.data.X1-P1",
"weight": "@1,machine.data.&"
}
}
}
}
},
{ //match key vs. values
"operation": "shift",
"spec": {
"*": {
"*": "[#2].&",
"Date": {
"*.*": {
"$(0,1)": "[#4].&2"//extract the value the upto dot only, starting from the left
}
},
"data": {
"*": {
"$": "[#4].&2[#2].key",
"@": "[#4].&2[#2].value"
}
}
}
}
}
]
the demo on the site https://jolt-demo.appspot.com/ is :