I have been trying to set key names for json array using the fields provided. I need to fetch a separate list of managers and colleagues.
Input:
{
"employeelist": [
{
"employee": "test",
"firstName": "ABC",
"lastName": "DEF"
},
{
"employee": "test1",
"firstName": "nametest",
"lastName": "namelast"
}
],
"manager": "test",
"colleague": "test1"
}
Expected Output:
{
"manager": [
{
"employee": "test",
"firstName": "ABC",
"lastName": "DEF"
}
],
"colleague": [
{
"employee": "test1",
"firstName": "nametest",
"lastName": "namelast"
}
]
}
Spec I used repeats the the complete list for both managers and colleagues.
[
{ // segregate values of the same key and form respective arrays.
"operation": "shift",
"spec": {
"employeelist": {
"*": {
"employee": {
"@(3,manager)": {
"@2": "manager.[]"
},
"@(3,colleague)": {
"@2": "colleague.[]"
}
}
}
}
}
}
]
2
Answers
You can use the following shift transformation spec
where the first components(
[0]
) of the newly derived arrays(test
andtest1
) are matched with the second components([1]
) within the last specthe demo on the site http://jolt-demo.appspot.com/ is
I think this simpler jolt spec might achieve the expected output, but please note that it works only if the document that needs to be moved to the
"manager"
field is the first element of the array (that’s why it matches the 0 index), and the document of"colleague"
is the second element of the array (index 1).Hope you find it useful!