- Subscribe to RSS Feed
- Mark Question as New
- Mark Question as Read
- Float this Question for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
How to convert two arrays to key value with Jolt?
- Labels:
-
Apache NiFi
Created on ‎11-16-2019 05:10 AM - edited ‎11-16-2019 08:49 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have some JSON that contains two arrays that I would like to convert to key/value pairs. I've already create a spec to convert my input data into the format below.
Input
{
"rows" : [ {
"row" : [ "row1", "row2", "row3" ],
"header" : [ "header1", "header2", "header3" ]
}, {
"row" : [ "row4", "row5", "row6" ],
"header" : [ "header1", "header2", "header3" ]
} ]
}
Is it possible to convert it again with Jolt to something like:
{
"header1" : "row1",
"header2" : "row2",
"header3" : "row3",
"header1" : "row4",
"header2" : "row5",
"header3" : "row6"
}
Any guidance would be highly appreciated 🙂
Created ‎11-16-2019 02:19 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If the original input data (before your Jolt transform) looks something like this...
{ "headers": [ "header1", "header2", "header3" ], "rows": [ [ "row1", "row2", "row3" ], [ "row4", "row5", "row6" ] ] }
...then it may be easier using SplitJSON and EvaluateJSONPath processors in this scenario.
The GenerateFlowFile processor has some sample data that matches the above format using books as the example. The result will be a separate JSON file for each array within "rows". You can configure the MergeContent to bundle the JSON records as needed.
EvaluateJSONPath
SplitJSON
EvaluateJSONPath
ReplaceText
Created ‎11-16-2019 02:19 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If the original input data (before your Jolt transform) looks something like this...
{ "headers": [ "header1", "header2", "header3" ], "rows": [ [ "row1", "row2", "row3" ], [ "row4", "row5", "row6" ] ] }
...then it may be easier using SplitJSON and EvaluateJSONPath processors in this scenario.
The GenerateFlowFile processor has some sample data that matches the above format using books as the example. The result will be a separate JSON file for each array within "rows". You can configure the MergeContent to bundle the JSON records as needed.
EvaluateJSONPath
SplitJSON
EvaluateJSONPath
ReplaceText
Created ‎09-30-2020 11:25 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @DataD,
Please find the below spec:
[
{
"operation": "shift",
"spec": {
"rows": {
"*": {
"row": {
"*": {
"@": "[&3].@(3,header[&1])"
}
}
}
}
}
}
]
This will give the output as:
[ {
"header1" : "row1",
"header2" : "row2",
"header3" : "row3"
}, {
"header1" : "row4",
"header2" : "row5",
"header3" : "row6"
} ]
I didn't convert it to
{ "header1" : "row1", "header2" : "row2", "header3" : "row3", "header1" : "row4", "header2" : "row5", "header3" : "row6" }
because that is not a valid json as header1,2 and 3 are repeated keys in the same level of the json.
