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
Related
I need to merge array data from two different arrays from the Input JSON and make a flat list of items in the Output JSON. The first array contains the key required for the output and the second array contains the value.
So far, all my attempts at a spec haven't even come close, which is why I haven't listed one. Please see the Input and Desired Output below. Thanks for any help!!
Input JSON :
{
"data": [
{
"2": {
"value": "DC1"
},
"3": {
"value": 5
}
},
{
"2": {
"value": "DC2"
},
"3": {
"value": 10
}
}
],
"fields": [
{
"id": 2,
"label": "DataCenter",
"type": "text"
},
{
"id": 3,
"label": "CCount",
"type": "numeric"
}
],
"metadata": {
"numFields": 2,
"numRecords": 2,
"skip": 0,
"totalRecords": 2
}
}
Desired Output JSON:
[
{
"DataCenter": "DC1",
"CCount": "5"
},
{
"DataCenter": "DC2",
"CCount": "10"
}
]
You can use this spec:
[
{
"operation": "shift",
"spec": {
"data": "&",
"fields": {
"*": {
"label": "fields.#(1,id)"
}
}
}
},
{
"operation": "shift",
"spec": {
"data": {
"*": {
"*": {
"*": "[&2].#(4,fields.&1)"
}
}
}
}
}
]
In your desired output CCount value is a string, You can add the below spec to change an integer to a string.
,
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"CCount": "=toString"
}
}
}
You might reciprocally match id value of fields array vs. the object keys of the data array within a common object within the first shift transformation spec in order to prepare for the second one in which we tile the attributes into their individual objects such as
[
{
"operation": "shift",
"spec": {
"*": { // represents the node for both "data" and "fields" arrays
"*": {
"*": {
"value": "&1.&",
"#1,label": "#2,id.&"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"value": {
"*": "[&].#2,label"
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
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
I have a JSON:
{
"relations": {
"advertiser_id": {
"9968": {
"name": "Advance/Unicredit",
"id": 9968
},
"10103": {
"name": "Advance/ ORIMI",
"id": 10103
}
},
"campaign_id": {
"256292": {
"name": "Interests_Aidata",
"id": 256292,
"advertiser_id": 9968
},
"257717": {
"name": "G_14.04",
"id": 257717,
"advertiser_id": 10103
}
}
}
}
I thought that it's an easy shift operation, but I'm stuck because of all values inside random property names like "9968": I don't understand how to move through json with these different propertie names.
Expected Output:
[
{
"name": "Interests_Aidata",
"id": 256292,
"advertiser_id": 9968
},
{
"name": "G_14.04",
"id": 257717,
"advertiser_id": 10103
}
]
UPDATE
Is it possible to add top-level (under relations) advertiser_id or campaign_id as additional propety like in an example?
[
{
"name": "Interests_Aidata",
"id": 256292,
"advertiser_id": 9968,
"entity_type": "campaign_id"
},
{
"name": "G_14.04",
"id": 257717,
"advertiser_id": 10103,
"entity_type": "campaign_id"
}
]
If your aim is to accumulate each array of objects under those properties(advertiser_id and campaign_id), then use the following spec
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"#": "&2"
}
}
}
}
}
]
If you're interested in only the stuff under campaign_id as lately edited, then use
[
{
"operation": "shift",
"spec": {
"*": {
"campaign_id": {
"*": {
"#": ""
}
}
}
}
}
]
The following spec can be used the desired result determined by the last update
[
{
"operation": "shift",
"spec": {
"*": {
"campaign_id": {
"*": {
"*": "[#2].&",
"$1": "[#2].entity_type"
}
}
}
}
}
]
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
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.&"
}
}
}
]