EDIT: The key names are dynamic and unknown.
I want to take an Object and create an String array that has each key and value concated together.
My keys contain underscores which I need to get rid of (I have that part working). I'm struggling to figure out the next step to join everything together. (I guess I'm missing how to reference key and value from RHS?)
Input:
{
"object": {
"key_1": [
"A",
"B"
],
"key_2": [
"C"
],
"key_3": [
"D",
"E",
"F"
]
}
}
Desired Output:
{
"results": [
"key 1: A B",
"key 2: C",
"key 3: D E F"
]
}
Spec:
[
{
"operation": "shift",
"spec": {
"object": {
"*_*": "results.&(0,1) &(0,2)",
"*": "results.&"
}
}
}
]
Current spec output:
{
"results": {
"key 1": [
"A",
"B"
],
"key 2": [
"C"
],
"key 3": [
"D",
"E",
"F"
]
}
}
Seems you need to use modify transformations with join functions, along with shift transformation specs such as
[
{
// combine the components of the each array with one extra whitespaces between them
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": "=join(' ',#(1,&))"
}
}
},
{
// get rid of underscores
"operation": "shift",
"spec": {
"*": {
"*_*": "&1.&(0,1) &(0,2)"
}
}
},
{
// generate new arrays with the first components are the keys, and second ones are the values of the previous attributes of the object
"operation": "shift",
"spec": {
"*": {
"*": {
"$": "&",
"#": "&"
}
}
}
},
{
// get concatenated values for key-value pairs with colons between them
"operation": "modify-overwrite-beta",
"spec": {
"*": "=join(': ',#(1,&))"
}
},
{
// get the desired result as a single array
"operation": "shift",
"spec": {
"*": {
"#": "results[]"
}
}
}
]
the demo on the site https://jolt-demo.appspot.com/ is :
Related
I don't know how to use jolt's transformations such as shift、default、modify-default-beta.
I have the following JSON value as
input :
{
"report": {
"q": "1",
"b": "2"
}
}
that I need to transform to
the desired output :
{
"report": {
"q": "1",
"b": "2"
},
"q": "1"
}
how can i handle it ?
Edit : I was actually trying to find a way to replicate,now i found :
[
{
"operation": "modify-default-beta",
"spec": {
"q": "#(2,report.&)"
}
}
]
You can use a shift transformation spec like this
[
{
"operation": "shift",
"spec": {
"*": "&",
"#(0,report.q)": "q"
}
},
{
// this spec stands only for sorting to obtain the exact appearance with the desired result
"operation": "shift",
"spec": {
"report": "&",
"q": "&"
}
}
]
where zero in #(0,report.q) represents the value taken from the current level
the demo on the site http://jolt-demo.appspot.com/ is
or another method would be by using shift transformation again :
[
{
"operation": "shift",
"spec": {
"*": {
"#": "&", // replicate the whole content
"q": "&" // replicate only attribute "q" without a node
}
}
}
]
I have this JSON:
{
"mapTrue": "true",
"code": [
"X",
"Y",
"Z"
],
"expired": [
"true",
"false",
"true"
]
}
I want to use the "mapTrue" value in order to filter the "code" array.
If "mapTrue": "true", I'll take the 0 and 2 values in "expired" array, therefore I need to output
"code": ["X", "Z"].
Using the same logic, for "mapTrue: "false" I'll return "code": ["Y"].
This specification returns the right "code" array but it doesn't use the "mapTrue" value.
[
{
"operation": "shift",
"spec": {
"expired": {
"*": {
"true": {
"#(3,code[&1])": "code"
}
}
}
}
}
]
That's where my problem is.
This works:
[
{
"operation": "shift",
"spec": {
"expired": "#(1,mapTrue)",
"code": "code"
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": {
"&1": {
"#(3,code[&1])": "code[]"
}
}
}
}
}
]
An alternative option would be using the following specs
[
{
// separate the values for "true", "false" while match "code" with the value of "mapTrue"
"operation": "shift",
"spec": {
"c*": {
"*": "#(2,expired[&])", // walk through the indexes(0,1,2) of "expired" array while matching with the components of "code"
"#(1,mapTrue)": "&1"
}
}
},
{
"operation": "shift",
"spec": {
"c*": {
"*": {
"#(2,&)": "&2" // grab the value going tree up two levels by using "#(2,&)", and copy the main object's label "code" by grabbing it after going tree two levels again
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
I have the following
Input JSON :
{
"a": [
[
"a1"
],
[
"a2"
]
],
"b": [
[
"b1",
"b2"
],
[
"b3",
"b4"
]
]
}
I want to use jolt transform to output it like this:
Output JSON:
[
{
"a": "a1",
"b": "b1"
},
{
"a": "a1",
"b": "b2"
},
{
"a": "a2",
"b": "b3"
},
{
"a": "a2",
"b": "b4"
}
]
I use combination of shift and cardinality but can't seem to get it to iterate through correctly:
[
{
"operation": "shift",
"spec": {
"*": "&"
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[&].&1"
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"*": "ONE"
}
}
}
]
What do I need to change to that jolt transform spec to get it to work?
You can walk by the indexes of the array "b" while picking values from the array "a" as well such as
[
{
// partition by indexes of array "a"&"b" while strolling through the array "b"
"operation": "shift",
"spec": {
"b": {
"*": {
"#(2,a)": {
"*": "&.&1.a"
},
"*": "&1.&.&2"
}
}
}
},
{
// get rid of object labels
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
},
{
// get rid of square brackets wrapping up the values of the attributes "a"
"operation": "cardinality",
"spec": {
"*": {
"a": "ONE"
}
}
},
{
// order the attributes by the key names
"operation": "sort"
}
]
the demo on the site http://jolt-demo.appspot.com/ is :
Say I have the following Json:
{
"collection": [
{
"sequence": 1,
"value": "event1"
},
{
"sequence": 2,
"value": "event2"
},
{
"sequence": 3,
"value": "event3"
}
]
}
Given that the array is not guaranteed to be sorted by "sequence", how do I extract the element that has the highest value of "sequence"?
the sub-objects of the "collection" array might be sorted by the sequence values by putting them as indexes for reformed array (call "objectWithHighestSequence") such as
[
{
"operation": "shift",
"spec": {
"collection": {
"*": "objectWithHighestSequence[#(0,sequence)]"
}
}
},
{
// only pick the last object after sorting within the shift transformation
"operation": "modify-overwrite-beta",
"spec": {
"*": "=lastElement(#(1,objectWithHighestSequence))"
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
I was trying to have a key value pair mapped to an array, distinguishing each value as a type using jolt transform spec
Input json
{
"testurl": "someurl",
"website": "someurl2"
}
tried this spec
[
{
"operation": "shift",
"spec": {
"testurl": "Urls[].testurl",
"website": "Urls[].website"
}
},
{
"operation": "shift",
"spec": {
"Urls": {
"*": {
"$": "Urls[&1].val"
}
}
}
}
]
Expected result is like this
{
"Urls": [{
"url": "someurl",
"val": "testurl"
}, {
"url": "someurl2",
"val": "website"
}]
}
Yes you can turn a set of Json key,value pair into an array.
It requires 2 shifts to be safe.
1st shift isolates all the properties you want to turn into an array.
2nd shift is able to use a "*" to then match all those items and place them an array.
Spec
[
{
"operation": "shift",
"spec": {
"testurl": "temp.testurl",
"website": "temp.website"
}
},
{
"operation": "shift",
"spec": {
"temp": {
"*": {
"$": "Urls[#2].val",
"#": "Urls[#2].url"
}
}
}
}
]