Reputation: 21
I need to transform multiple keys and their values to new JSON spec using jolt.
Input:
{
"product": "monitor",
"ID": "222",
"price": "300"
}
Expected output:
{
"record": [
{
"key": "product",
"value": "monitor"
},
{
"key": "ID",
"value": "222"
},
{
"key": "price",
"value": "300"
}
]
}
Thanks in advance
Upvotes: 2
Views: 1613
Reputation: 10648
This is just another example and essentially the same than the previous answer but with more comments:
input
{
"foo": "foo value",
"bar": "bar value",
"zoo": "zoo value"
}
jolt spec
[
{
"operation": "shift",
"spec": {
"foo|bar" : { // pick only these names, other names (i.e. zoo) are filtered
// $ current name (LHS) (i.e. foo or bar)
// @ current value (LHS) (i.e. "foo value" of "bar value")
// & current name (RHS) (i.e. foo or bar)
"$": "&.key",
"@": "&.value"
}
}
},
{
"operation": "shift",
"spec": {
"*": { // iterate all top level names (i.e. foo or bar)
// copy only the values (@), e.g.:
// {
// "key" : "foo",
// "value" : "foo value"
// }
// under a new name (records). [] is redundant but more descriptive
"@": "records[]"
}
}
}
]
output after the first transformation
{
"foo" : {
"key" : "foo",
"value" : "foo value"
},
"bar" : {
"key" : "bar",
"value" : "bar value"
}
}
the final output (after the second transformation)
{
"records": [
{
"key": "foo",
"value": "foo value"
},
{
"key": "bar",
"value": "bar value"
}
]
}
Upvotes: 0
Reputation: 2116
Convert each node in the input json into key/value and shift to the named object. And then named object is moved to the array.
[
{
"operation": "shift",
"spec": {
"*": {
"@": "&1.value",
"$": "&1.key"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"@": "records"
}
}
}
]
Upvotes: 4