I’m having issues with Jolt transformation, I need to extract values/fields from array list and add one custom field either from array I’m targeting to extract or outer list. But let me try to explain more in detailed through code.
This is my input (with only 2 fields sets in "data", originally I have more than 200 sets in "data"):
{
"result": {
"id": "123456789",
"year": 2023,
"version": "0.1",
"country": "UK",
"modified": "2023-11-09T00:00:00",
"data": [
{
"FieldNumber1": "CompanyNumber",
"FieldNumber2": "0.1",
"FieldNumber3": "123456789",
"FieldNumber4": false,
"FieldNumber5": false,
"FieldNumber6": 1,
"FieldNumber7": 0
},
{
"FieldNumber1": "CompanyName",
"FieldNumber2": "0.1",
"FieldNumber3": "FunctionsAndStrings",
"FieldNumber4": false,
"FieldNumber5": false,
"FieldNumber6": 1,
"FieldNumber7": 0
}
],
"description": null,
"downloadNumber": "aaaaa-bbbbb-11111-22-abcabc123"
},
"true": null,
"false": null
}
The result am expecting is something like this:
[
{
"id": "123456789",
"FieldNumber1": "CompanyNumber",
"FieldNumber2": "0.1",
"FieldNumber3": "123456789",
"FieldNumber4": false,
"FieldNumber5": false,
"FieldNumber6": 1,
"FieldNumber7": 0
},
{
"id": "123456789",
"FieldNumber1": "CompanyName",
"FieldNumber2": "0.1",
"FieldNumber3": "FunctionsAndStrings",
"FieldNumber4": false,
"FieldNumber5": false,
"FieldNumber6": 1,
"FieldNumber7": 0
}
]
or
[
{
"CompanyNumber": "123456789",
"FieldNumber1": "CompanyNumber",
"FieldNumber2": "0.1",
"FieldNumber3": "123456789",
"FieldNumber4": false,
"FieldNumber5": false,
"FieldNumber6": 1,
"FieldNumber7": 0
},
{
"CompanyNumber": "123456789",
"FieldNumber1": "CompanyName",
"FieldNumber2": "0.1",
"FieldNumber3": "FunctionsAndStrings",
"FieldNumber4": false,
"FieldNumber5": false,
"FieldNumber6": 1,
"FieldNumber7": 0
}
]
I would like to have only a list from "data" but with fixed "id" from the "result", before each data set.
Also in the first data set "FieldNumber1" will always be "CompanyNumber" and it will always contain the same value in "FieldNumber3" as "id" in "result". Maybe it easier to calculate it from "FieldNumber3".
I didn’t move further than removing "result" and extracting only "data" list with this easy jolt spec:
[
{
"operation": "shift",
"spec": {
"result": {
"data": ""
}
}
}
]
I need to store each set of data in the table with the first attribute "id" or "CompanyNumber" so I can join it with other tables.
Thanks a lot!!!
2
Answers
You can use the below Jolt spec
What you need is to loop through all indexes under the data array while picking the value of
id
from the level where it’s stated(eg. going two levels up the tree) within a shift transformation such asthe demo on the site http://jolt-demo.appspot.com/ is :
id
key might be switched toCompanyNumber
through use of"@2,id" : "[#2].CompanyNumber"