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

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].&"
}
}
}
}
}
]

Related

How to work with an array inside of another array?

I'm having problems while working with a nested array inside another array on a JSON document. I need to obtain an array containing one JSON document with the "CandidateEmail" and the "ApplicationId" "JobRefNumber". I think that the example below will help you understand better.
My input is:
{
"Content": [
{
"CandidateEmail": "john1#noexist.com",
"Applications": [
{
"ApplicationId": "app1",
"JobRefNumber": "REF1"
},
{
"ApplicationId": "app2",
"JobRefNumber": "REF2"
}
]
},
{
"CandidateEmail": "carl2#email.com",
"Applications": [
{
"ApplicationId": "app3",
"JobRefNumber": "REF3"
},
{
"ApplicationId": "app4",
"JobRefNumber": "REF4"
}
]
}
]
}
The expected output is:
[
{
"CandidateEmail": "john1#noexist.com",
"ApplicationId": "app1",
"JobRefNumber": "REF1"
},
{
"CandidateEmail": "john1#noexist.com",
"ApplicationId": "app2",
"JobRefNumber": "REF2"
},
{
"CandidateEmail": "carl2#email.com",
"ApplicationId": "app3",
"JobRefNumber": "REF3"
},
{
"CandidateEmail": "carl2#email.com",
"ApplicationId": "app4",
"JobRefNumber": "REF4"
}
]
I don't know if it will be of any help, but here is the spec I've been able to draft so far:
[
{
"operation": "shift",
"spec": {
"Content": {
"*": {
"Applications": {
"*": {
"#(2,CandidateEmail)": "[&1].CandidateEmail",
"ApplicationId": "[&1].ApplicationId",
"JobRefNumber": "[&1].JobRefNumber"
}
}
}
}
}
}
]
You can use a shift transformation spec such as
[
{
"operation": "shift",
"spec": {
"Content": {
"*": {
"Appl*": {
"*": {
"#2,CandidateEmail": "&3[#2].CandidateEmail", // you an go two levels up the tree to reach the level of CandidateEmail
"*": "&3[#2].&" // distinguish by indexes of "Applications" and "Content" arrays through use of [#2] and &3 respectively. Replicate the keys by using &, and all attributes nested within innermost objects by using * wildcards.
}
}
}
}
}
},
{
"operation": "shift",
"spec": {// get rid of the keys of wrapper arrays and objects
"*": {
"*": ""
}
}
}
]

How to use value of key in JOLT

I'm looking for breaking nested JSON file and trying to flatten them to fit into a SQL database.
Current JSON:
{
"content": {
"failedPerProductLineAndReason": {
"Product1": {
"Downsizing licenses is not allowed": 1
}
}
}
}
Expected outcome:
{
"ErrorType": "failedPerProductLineAndReason",
"product": "Product1",
"error": "Downsizing licenses is not allowed",
"quantity": 1
}
Go inside the Downsizing licenses is not allowed and get its value by # and get another keys you want by $
[
{
"operation": "shift",
"spec": {
"*": { // content
"*": { // failedPerProductLineAndReason
"*": { // Product1
"*": { // Downsizing licenses is not allowed
"$2": "ErrorType",
"$1": "product",
"$": "error",
"#": "quantity"
}
}
}
}
}
}
]
You can use $ wildcards incrementally nested within objects/attribute such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"$": "ErrorType",
"*": {
"$": "product",
"*": {
"$": "error",
"#": "quantity"
}
}
}
}
}
}
]
as principally needed to extract the keys
the demo on the site http://jolt-demo.appspot.com/ is

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 converting an array to a string with an indifinite numbers of elements in the array

Help pls. I lookead at all simular questions, but did not find an answer to my. I know how converting this
{
"rooms": {
"room_number": [
{
"number": "1"
},
{
"number": "2"
},
{
"number": "3"
},
{
"number": "4"
},
{
"number": "5"
}
]
}
}
into this
{
"room_numbers" : "1;2;3;4;5"
}
with a this JOLT
[
{
"operation": "shift",
"spec": {
"rooms": {
"room_number": {
"*": {
"#(number)": "room_numbers[]"
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": { "room_numbers": "=concat(#(2,room_numbers[0]),';',#(2,room_numbers[1]),';',#(2,room_numbers[2]),';',#(2,room_numbers[3]),';',#(2,room_numbers[4]))" }
}
]
but i d'nt know how this make if number of elements in a array is a variable.
You can use join rather than concat along with modify transformation in order to perform concatenation at once instead of adding each array element individually such as
[
{
// dynamically get the array of numbers, namely "room_numbers"
"operation": "shift",
"spec": {
"rooms": {
"room_number": {
"*": {
"*": "&2s"
}
}
}
}
},
{
// concatenate all of them
"operation": "modify-overwrite-beta",
"spec": {
"*": "=join(';',#(1,&))"
}
}
]

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"
}
}
}
}
]

Resources