How to work with an array inside of another array? - arrays

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
"*": {
"*": ""
}
}
}
]

Related

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: 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 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 transfer elements from child array to parent

Here i was trying merge elements from 2 child arrays in to its parent one and leave the other one and move the second array to two levels up.
Is there a way to change value on condition, like in the input below,
parties.party.sno ="1" , can this updated as parties..sno='Y'
input:
{
"Parties": [
{
"party": {
"partyId": "100005767",
"sno": 1,
"fn": "Th1mas",
"ln": "Edison",
"emails": [
{
"emailAddress": "jkjk#ui.com"
}
],
"addresses": [
{
"zip": ""
}
],
"shealth": [
{
"stcd": "TN",
"lno": "1"
}
]
},
"seq": {
"typeCd": "1"
}
}
]
}
Expected output:
{
"person": {
"first_name": "Th1mas",
"middle_initial": "Edison",
"last_name": "",
"email_address": "jkjk#ui.com",
"pinCode": ""
},
"shealth": {
"statecd": "ON"
},
//this is the seq no from party.sno
"primary": "Y",
"typeCd": "1"
}
tried spec like this:
[
{
"operation": "shift",
"spec": {
"Parties": {
"*": {
"party": {
"emails": {
"*": {
"emailAddress": "[&1].email_address",
"#(2,fn)": "[&1].first_name",
"#(2,ln)": "[&1].last_name"
}
},
"addresses": {
"*": {
"zip": "[&1].pinCode"
}
},
"shealth": {
"*": {
"stcd": "[&1].statecd"
}
}
}
}
}
}
}
]
This spec works,
"party": {
"sno": {
"1": {
"#Y": "primary"
}
},
Try applying the condition from the current level,
[
{
"operation": "shift",
"spec": {
"Parties": {
"*": {
"party": {
"sno": {
"1": {
"#Y": "primary"
}
},
"emails": {
"*": {
"emailAddress": "person.email_address",
"#(2,fn)": "person.first_name",
"#(2,ln)": "person.last_name"
}
},
"addresses": {
"*": {
"zip": "person.pinCode"
}
},
"shealth": {
"*": {
"stcd": "shealth.statecd"
}
}
},
"seq": {
"typeCd": "typeCd"
}
}
}
}
}
]

Resources