Jolt tranfrorm nested json array - arrays

How can I break and flatten nested JSON with arrays using Jolt transformations
from:
{
"product": [
"test1",
"test2"
],
"Purchase": [
1,
2
],
"Renewal": [
1,
2
]
}
to:
[
{
"product": "test1",
"Purchase": 1,
"Renewal": 1
},
{
"product": "test2",
"Purchase": 2,
"Renewal": 2
}
]
I want to flatten this array json file in order to fit to sql db format.

You can use a shift transformation spec such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"#": "[#2].&2"
}
}
}
}
]
where &2 leaf separates values into new sub-arrays by going to levels up to grab their current respective array names, and [#2] re-organizes them to form array of objects.
the demo on the site http://jolt-demo.appspot.com/ is
Edit : If the input is like the below one as asked in the last comment
{
"product": [
"test- 1",
"test- 2"
],
"Purchase": [
2,
2
],
"Renewal": 1
}
then you can consider using
[
{
"operation": "shift",
"spec": {
"*r*": {
"*": {
"#": "[#2].&2",
"#(2,Renewal)": "[#2].Renewal"
}
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"Re*": "ONE"
}
}
}
]

Related

Jolt: How to level-up elements from an object?

I have the following JSON :
{
"Content": [
{
"CandidateFirstName": "Nagu",
"Applications": {
"ApplicationId": "456",
"Sarasa": "test"
}
},
{
"CandidateFirstName": "Deleted",
"Applications": {
"ApplicationId": "123",
"Sarasa": "test2"
}
}
]
}
and I'd like to have the following output:
[
{
"FirstName": "Nagu",
"ApplicationsId": "456",
"Sarasa": "test"
},
{
"FirstName": "Deleted",
"ApplicationsId": "123",
"Sarasa": "test2"
}
]
I'm close with the following jolt, but I'm not being able to remove the object "Applications":
[
{
"operation": "shift",
"spec": {
"Content": {
"*": {
"CandidateFirstName": "[&1].FirstName",
"Applications": "[&1].Applications"
}
}
}
}
]
Do you know how to achieve this?
Thanks in advance!
Yes, you were quite close. You have to just add one more level of nested section for Applications section
[
{
"operation": "shift",
"spec": {
"Content": {
"*": {
"CandidateFirstName": "[&1].FirstName",
"Applications": {
"*": "[&2].&"
}
}
}
}
}
]
You can use two consecutive shift transformation specs
[
{//get individual objects nested within square brackets
"operation": "shift",
"spec": {
"Content": {
"*": {
"Applications": {
"#1,CandidateFirstName": "&2[#1].FirstName",
"*": "&2[#2].&"
}
}
}
}
},
{// get rid of the keys of index values
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]
or use a single spec such as
[
{
"operation": "shift",
"spec": {
"Content": {
"*": {
"Candidate*": "[#2].FirstName",// the expression "&(0,1)" replicates the literal extracted from the current( 0th ) level of the 1st asterisk(might be multiple) which is stated on the LHS
"#Applications.ApplicationId": "[#2].ApplicationId",
"#Applications.Sarasa": "[#2].Sarasa"
}
}
}
}
]
where [#2], as having a # wildcard and nested within square brackets and staying on the right-hand-side, represents traversing one colon(:), and one opening curly brace { those makes 2 in order to reach the level of the indexes of the Content array to grab them.
Yet, there's another method as follows :
[
{
"operation": "shift",
"spec": {
"Content": {
"*": {
"Appl*": {// represents the key of the node starts with "Appl" in order to shorten the literal "Applications"
"#1,CandidateFirstName": "[&2].FirstName",// need to go one level up to get the value of the "CandidateFirstName" attribute
"*": "[&2].&"
}
}
}
}
}
]
where [&2] on the RHS represents traversing two opening curly braces in contrast to the [#2]
You can use this short and concise spec:
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"Candidate*": "[&1].&(0,1)",
"*": {
"*": "[&2].&"
}
}
}
}
}
]

JOLT : Make Nested Make a nested array as part of main array

I am trying to transform the JSON as below expected output. Stuck with the spec below. Can someone help in this?
There is an inner array with name "content" in the "results" array which I want to make it as a part of main array.
Input JSON
{
"total": 100,
"start": 1,
"page-length": 10,
"results": [
{
"index": 1,
"uri": "uri1/uri2",
"extracted": {
"kind": "object",
"content": [
{
"code": "A1",
"region": "APAC"
}
]
}
},
{
"index": 2,
"uri": "uri1/uri2",
"extracted": {
"kind": "object",
"content": [
{
"code": "B1",
"region": "AMER"
}
]
}
},
{
"index": 3,
"uri": "uri1/uri2",
"extracted": {
"kind": "object",
"content": [
{
"code": "C1",
"region": "APAC"
}
]
}
}
]
}
Expected json output
[
{
"code": "A1",
"region": "APAC"
},
{
"code": "B1",
"region": "AMER"
},
{
"code": "C1",
"region": "APAC"
}
]
Spec Tried
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"extracted": { "content": { "#": "&" } }
}
}
}
},
{
"operation": "shift",
"spec": {
"content": {
"#": "&"
}
}
}
]
Find the output below I am getting on Jolt tool
You can use # wildcard nested within square brackets in order to reach the level of the indexes of the "results" array such that
[
{
"operation": "shift",
"spec": {
"results": {
"*": {//the indexes of the "results" array
"extracted": {
"content": {
"*": {//the indexes of the "content" array
"*": "[#5].&"
}
}
}
}
}
}
}
]
Also the following spec, which repeats the content of the inner array without keys, will give the same result :
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"extracted": {
"content": {
"*": "[]"// [] seems like redundant but kept for the case the array has a single object.
}
}
}
}
}
}
]
which is quite similar to your tried one.

Jolt transformation needed for multidimensional array

I am trying to transform the data from a multi array to single array using jolt technique. But unable to do so. Below are the details.
Input file:
{
"oi": [
{
"ei": [
{
"type": "bs",
"id": "797416713"
}
]
},
{
"ei": [
{
"type": "bs",
"id": "797416716"
}
]
}
]
}
Jolt file used is as below:
[
{
"operation": "shift",
"spec": {
"oi": {
"*": {
"ei": {
"*": {
"type": {
"bs": {
"#(2,id)": "oi[#3].bs"
}
}
}
}
}
}
}
}
]
Expected output from above is as below.
{
"oi": [
{
"bs": [
"797416713",
"797416716"
]
}
]
}
Actual output coming from jolt is :
{
"oi": [
{
"bs": [
"797416713",
"797416716"
]
}
]
}
Currently the desired and actual output values are identical.
I understand from the expression
to transform the data from a multi array to single array
that you need to get such a result of type array of objects tagged current inner(ei) and outer(oi) keys such as(if not please clearly state) :
[
{
"oi": {
"ei": [
{
"type": "bs",
"id": "797416713"
},
{
"type": "bs",
"id": "797416716"
}
]
}
}
]
Then, you can use this shift transformation spec :
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"*": {
"*": {
"#": "[#2].&4.&2"
}
}
}
}
}
}
]
where &4 will grab the key oi after going 4 levels up, &2 will grab the key ei after going 2 levels up the tree

JOLT array transformation: add key in all objects in list

I have an input object:
{
"array": [
{
"id": 1
},
{
"id": 2
}
],
"object": {
"fixed-value": "some-value"
}
}
And I want to transform it into:
{
"NewObject" : [ {
"objectId" : 1,
"fixedValue": "some-value"
}, {
"objectId" : 2,
"fixedValue": "some-value"
} ]
}
I made this JOLT spec which shifts the list of objects in array, but I'm not able to add the fixed-value key in all of these objects:
[
{
"operation": "shift",
"spec": {
"array": {
"*": {
"id": "NewObject[&1].objectId"
}
}
}
}
]
Check this spec,
Traverse back to the root and then select the fixed-value,
"#(2,object.fixed-value)": "NewObject[&1].fixedValue"
[
{
"operation": "shift",
"spec": {
"array": {
"*": {
"id": "NewObject[&1].objectId",
"#(2,object.fixed-value)": "NewObject[&1].fixedValue"
}
}
}
}
]

JOLT - Split array into elements for Nifi Databaserecord

I am struggeling with some jolt transformation. I need to extract informations from an array, but also need some uppper level informations.
I have bills and some bills have multiple attachments. I want to store this attachments in a Postgress db and for each attachment aplly the bill id...
My input
[
{
"bill_id": 1,
"entities": [
{
"type": "alpha",
"data": "foo"
},
{
"type": "beta",
"data": "bar"
}
]
},
{
"bill_id": 2,
"entities": []
}
]
My desired output
[
{
"bill_id": 1,
"type": "alpha",
"data": "foo"
},
{
"bill_id": 1,
"type": "beta",
"data": "bar"
}
]
I would be very glad, if someone could help me out
Well, i found an answer, that perfectly matches my needs. A little bit tricky, but it is working fine with two shifts:
[
{
"operation": "shift",
"spec": {
"*": {
"entities": {
"*": {
"#(2,bill_id)": "[&3].[&1].bill_id",
"type": "[&3].[&1].type",
"data": "[&3].[&1].data"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[]"
}
}
}
]

Resources