I’m using jolt and I have an input object where I would like to take the keys out of one property and insert them into each object of an array in another property:
My input:
{
"jolt_marketplaceMetadata": {
"NAN_KEY": 1,
"TEMP": 3
},
"jolt_attributes": [
{
"name": "HELLO",
"yyup": 3
},
{
"huh": "please",
"work": 2
}
]
}
The result I’m aiming for:
"jolt_attributes": [
{
"name": "HELLO",
"yyup": 3,
"NAN_KEY": 1,
"TEMP": 3
},
{
"huh": "please",
"work": 2,
"NAN_KEY": 1,
"TEMP": 3
}
]
I’ve tried a couple of different spec arrays and I’ve gotten pretty close, but I can’t seem to figure out how to get the values in without merging all of the data from each of the objects in the array:
[
{
"operation": "shift",
"spec": {
"jolt_attributes": {
"*": {
"*": "jolt_attributes[&1].&",
"@(2,jolt_marketplaceMetadata)": "jolt_attributes[&1]"
}
}
}
}
]
which gives me:
{
"jolt_attributes": [
{
"NAN_KEY": 1,
"TEMP": 3,
"name": "HELLO",
"yyup": 3,
"huh": "please",
"work": 2
},
{
"NAN_KEY": 1,
"TEMP": 3,
"name": "HELLO",
"yyup": 3,
"huh": "please",
"work": 2
}
]
}
It adds the keys from jolt_marketplaceMetadata, but it also merges all of the key/values from each of the array objects together each time.
Any ideas??
>Solution :
You can use the following shift transformation spec
[
{
"operation": "shift",
"spec": {
"jolt_attributes": {
"*": {
"*": "&2[#2].&",
"@2,jolt_marketplaceMetadata.NAN_KEY": "&2[#2].NAN_KEY",
"@2,jolt_marketplaceMetadata.TEMP": "&2[#2].TEMP"
}
}
}
}
]
where
- &2 replicates the key of the array (
"jolt_attributes") - [#2] loops through the indexes of that array after traversing the
tree 2 levels(once for colon, once for opening curly brace) and generates arraywise(array of objects here) result