Need help to figure out how to fix a jolt spec - json

I am trying to write a specification using Jolt, I have been struggling with it a little, my desired output is:
{
"Team": [
{
"ID": "1",
"Source": {
"HV": 1,
"ID": 1
}
},
{
"ID": "3",
"Source": {
"HV": 2,
"ID": 3
}
}
]
}
I have the following input:
[
{
"Page": 910407,
"TimingID": 10014,
"ScoreInfo": {
"Inn": 9,
"TB": 1,
"Team": [
{
"HV": 1,
"ID": 1,
"Score": 4
},
{
"HV": 2,
"ID": 3,
"Score": 2
}
],
"HV": null
}
}
]
But after execute this specification:
[
{
"operation": "shift",
"spec": {
"0": {
"ScoreInfo": {
"Team": {
"*": "&"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "Team.#ID.Source"
}
}
]
I ended up with this result:
{
"Team" : {
"1" : {
"Source" : {
"HV" : 1,
"ID" : 1,
"Score" : 4
}
},
"3" : {
"Source" : {
"HV" : 2,
"ID" : 3,
"Score" : 2
}
}
}
}
I would some help to figure out how to solve, I have being reading and practing but still having some doubts in cases, where it needs a little bit more miles using Jolt when the output starting get a little bit more advanced.
Thanks,

Applying one level of shift transformation is enough such as
[
{
"operation": "shift",
"spec": {
"*": {
"ScoreInfo": {
"Team": {
"*": {
"HV": "&2.[&1].Source.&",
"ID": "&2.[&1].Source.&",
"#(0,ID)": "&2.[&1].ID"
}
}
}
}
}
}
]
where &2 s represent the outermost key name(Team), [&1] s are used to get rid of inner arrays rather to get objects, the & leaves (innermost ampersands) stand for getting the values of the current level of keys such as HV&ID

Related

Modify a JSON using Jolt Transform

I have an array of JSONs as listed below:
[
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
]
The following are the objectives:
(1) Modify the list above into:
{
"data": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
],
"idList": [ 1, 2, 3 ]
}
(2) Calculate the Minimum and Maximum of "idList" to finally obtain:
{
"data": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
],
"minID": 1,
"maxID": 3,
}
I think (2) is straightforward after getting (1), as I can simply use:
min(#(1,idList))
I have a problem in converting the original input into (1), here's my attempt:
[
{
"operation": "shift",
"spec": {
"*": "data"
}
},
{
"operation": "shift",
"spec": {
"data": { "*": { "id": "idList" } }
}
}
]
which yields:
{
"idList" : [ 1, 2, 5 ]
}
Can anyone help on this?
Also, am a newbie to this Jolt Transform technique, can anyone suggest a good source for mastering this ? (like a book)
You can consecutively use a shift, and modify transformation specs such as
[
{
// for the first objective
"operation": "shift",
"spec": {
"*": {
"#": "data",
"*": "idList"
}
}
},
{
// for the second one
"operation": "modify-overwrite-beta",
"spec": {
"minID": "=min(#(1,idList))",
"maxID": "=max(#(1,idList))"
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

Jolt transform array to dissipate an outer attribute to each sub-object of an array

I want to apply a Jolt Transformation, but this is still cloudy on my side:
This is my input data:
{
"results": 1,
"data": [
{
"detail": [
{
"num": "140"
},
{
"num": "158"
},
{
"num": "180"
},
{
"num": "183"
},
{
"num": "213"
}
],
"code": "01007"
}
],
"response_code": 200
}
My desired output:
[
{
"code": "01007",
"num": "140"
},
{
"code": "01007",
"num": "158"
},
....
{
"code": "01007",
"num": "213"
}
]
And my trial for the JOLT specification so far, I do not understand how to add a custom field to all elements of the list:
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"detail": {
"*": {
"code": "[&1].code",
"#": "[&1]"
}
}
}
}
}
}
]
You can use the shift transformation like this
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"detail": {
"*": {
"#(2,code)": "[&1].code", // to go 2 levels up to reach the level of the attribute "code"
"*": "[&1].&" // to get the values of the existing attributes within the current object without object wrapper
}
}
}
}
}
}
]
applying just some slight changes.
the demo on the site http://jolt-demo.appspot.com/ is

Create array of simpler JSON objects from a nested JSON using jolt

My input JSON is like
{
"common": {
"name": "abc"
},
"details": {
"id": 4,
"node": [
{
"name": "node1",
"array2": []
},
{
"name": "node2",
"array2": [
{
"name": "node2_a2_1"
}
]
},
{
"name": "node3",
"array2": [
{
"name": "node3_a2_1"
},
{
"name": "node3_a2_2"
},
{
"name": "node3_a2_3"
}
]
}
]
}
}
What I want is for each leaf node in array2 (e.g. {"name": "node3_a2_1"}) I will traverse towards the root and add all common items and create an array of JSON objects without any nested object. So, the output I want is like
[
{
"common_name": "abc",
"id": 4,
"node_name": "node2",
"name": "node2_a2_1"
},
{
"common_name": "abc",
"id": 4,
"node_name": "node3",
"name": "node3_a2_1"
},
{
"common_name": "abc",
"id": 4,
"node_name": "node3",
"name": "node3_a2_2"
},
{
"common_name": "abc",
"id": 4,
"node_name": "node3",
"name": "node3_a2_3"
}
]
Could you please suggest how can I do that?
You can walk through the array2 while picking values of each element from their original location in the JSON value.
For example; traverse } four times to grab the value of id by using #(4,id), and use #(3,name)[&1] as the common distinguishing identifier for each attribute such as
[
{
"operation": "shift",
"spec": {
"details": {
"node": {
"*": {
"array2": {
"*": {
"#(5,common.name)": "#(3,name)[&1].common_name",
"#(4,id)": "#(3,name)[&1].id",
"#(2,name)": "#(3,name)[&1].node_name",
"name": "#(3,name)[&1].&"
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]

Jolt update all 0 value to 1

I would like to know in Jolt, how do I count the number of levels I need to moved up to get the required data.
I played around the Jolt spec to convert value in "quantity" from 0 to 1
Input
{
"items": [
{
"product": {
"name": "product1",
"id": "001"
},
"quantity": 1
},
{
"product": {
"name": "product2",
"id": "002"
},
"quantity": 0
},
{
"product": {
"name": "product3",
"id": "003"
},
"quantity": 0
}
]
}
The expected output
{
"items": [
{
"product": {
"name": "product1",
"id": "001"
},
"quantity": 1
},
{
"product": {
"name": "product2",
"id": "002"
},
"quantity": 1
},
{
"product": {
"name": "product3",
"id": "003"
},
"quantity": 1
}
]
}
Jolt spec with some notes for what I understand.
[
{
"operation": "shift",
"spec": {
"items": {
"*": {
"quantity": {
"0": {
"#1": "items.[&3].quantity" //[&3] => I move 3 levels up to items.* to get index?
},
"*": {
"#(2,quantity)": "items.[&3].quantity" //#(2,quantity) => I move 2 levels up to get items.*.quantity value?
}
},
"*": "items.[&1].&" //[&1] => I move 1 level up to items.* to get index?
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"items": {
"*": {
"quantity": "=toInteger(#(1,quantity))"
}
}
}
}
]
Do I understand it correctly? Please advice.
Thanks you
As much as I understand, you want to reflect the first quantity value within the items array to all other quantity values of the other objects. Then you can use these shift transformations :
[
{
"operation": "shift",
"spec": {
"items": {
"0": {
"#": "&"
},
"*": {
"#(0,product)": "&.product",
"#(2,&1[0].quantity)": "&.quantity"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "items"
}
}
]

create a new array using JOLT

I am trying to modify the JSON output using JOLT, I have been using it mostly to rename/remove fields until now.
I want to create a new object of existing 2 arrays as elements.
input.json
[
{
"features": [
{
"featureId": 1,
"feature": "feature"
}
],
"product": [
{
"id": 1,
"name": "name"
}
]
}
]
current spec json:
[{
"operation": "shift",
"spec": {
"*": {
"features": {
"*": {
"featureId": "[&3].&2.[&1].featureId",
"feature": "[&3].&2.[&1].feature"
}
},
"product": {
"*": {
"id": "[&3].&2.[&1].id",
"name": "[&3].&2.[&1].name"
}
}
}
}
}
]
expected output:
[
{
"newarray": {
"features": [
{
"featureId": 1,
"feature": "feature"
}
],
"product": [
{
"id": 1,
"name": "name"
}
]
}
}
]
Basically, I want to move both of my existing arrays inside a new object. Thanks in advance!
Spec
[
{
"operation": "shift",
"spec": {
"*": { // outer array index
// match features and product which are lists
// and copy those lists to the output
"*": "[0].newarray.&"
}
}
}
]