Skip to main content
saquibsk
Gold ⭐️⭐️
April 21, 2024
Solved

Extracting List(Array) element using JOLT

Hello,

Can you assist me in retrieving the values stored in the "customfield_10161" field from JSON input? My expectation is to consistently receive the values and name it "DEFECT_ROOT_CAUSE" , whether they are stored as a list or as a single value."

How to write JOLT which will handle the both the case. let say If I will get list then extract last element of it, if not list then record as it is.?

Extracting List: (works fine when input is list)

saquibsk_0-1713731749160.png

when input is not list: 

then DEFECT_ROOT_CAUSE column is missing My expectation to get the records in DEFECT_ROOT_CAUSE columns whether its list or not.

saquibsk_1-1713731973678.png

 

 

 

 

 

SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
May 6, 2024

Based on the provided new input and the expected output as I was able to understand where you always want to consider the last element of both DEFECT_ROOT_CAUSE & SPRINT_LIST which both may or may not be an array of values, I would take a different approach to the jolt spec which is much simpler as follows:

 

 

[
// First spec is convert everything into list regardless
{
"operation": "cardinality",
"spec": {
"*": {
"SPRINT_LIST": "MANY",
"DEFECT_ROOT_CAUSE": "MANY"
}
}
}
,
// From the generated list above return last element
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"SPRINT_LIST": "=lastElement",
"DEFECT_ROOT_CAUSE": "=lastElement"
}
}
}

]

 

 

 

16 replies

saquibsk
saquibskAuthor
Gold ⭐️⭐️
May 6, 2024

Thank you very much, @SAMSAL. I really appreciate your time and effort.

My input format has changed. I will adjust the JOLT accordingly if it does not work or encounter any issues, I will ask one final question. Otherwise, I will mark your last reply as the solution.

Translate
Shakib M.
saquibsk
saquibskAuthor
Gold ⭐️⭐️
May 6, 2024

Just to give a heads up, my input will be something like the example below. I have around 60 columns, and out of those, only 4 columns are in the list.

Example: from below example i would like to handle the "DEFECT_ROOT_CAUSE" and "SPRINT_LIST" 

Output would be same as your solution:

DEFECT_ROOT_CAUSE:"Frontend"

SPRINT_LIST:"24-w2-3"

 

 

==================================
-- input 1
==================================

[ {
"INTEGRATION_ID" : "1111",
"ISSUE_KEY" : "PP2-7426",
"SUMMARY" : "Confirmation Web",
"SPRINT_LIST" :"24-w2-1",
"DEFECT_TYPE" : "poor user interface design, confusing navigation",
"TEAM" : "Web Engineering",
"PLATFORM" : "Customer Web",
"DEFECT_ROOT_CAUSE" : [ "Backend", "Frontend" ]
}
]


==================================
-- input 2
==================================


[ {
"INTEGRATION_ID" : "2222",
"ISSUE_KEY" : "PP2-7427",
"SUMMARY" : "Confirmation Web",
"SPRINT_LIST" : [ "24-w2-1", "24-w2-2", "24-w2-3" ],
"DEFECT_TYPE" : "poor user interface design, confusing navigation",
"TEAM" : "Web Engineering",
"PLATFORM" : "Customer Web",
"DEFECT_ROOT_CAUSE" : "Backend"
}
]

 

Translate
Shakib M.
SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
May 6, 2024

Based on the provided new input and the expected output as I was able to understand where you always want to consider the last element of both DEFECT_ROOT_CAUSE & SPRINT_LIST which both may or may not be an array of values, I would take a different approach to the jolt spec which is much simpler as follows:

 

 

[
// First spec is convert everything into list regardless
{
"operation": "cardinality",
"spec": {
"*": {
"SPRINT_LIST": "MANY",
"DEFECT_ROOT_CAUSE": "MANY"
}
}
}
,
// From the generated list above return last element
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"SPRINT_LIST": "=lastElement",
"DEFECT_ROOT_CAUSE": "=lastElement"
}
}
}

]

 

 

 

Translate
saquibsk
saquibskAuthor
Gold ⭐️⭐️
May 7, 2024

Thank you, @SAMSAL. This logic is much simpler. My JOLT logic was a bit complex. This is going to save me a lot of time.

Is there are any tutorial where we can learn the JOLT ?

Translate
Shakib M.
SAMSAL
Platinum ⭐️⭐️⭐️
May 8, 2024

There is not really a comprehensive Jolt tutorial that I could find out there. Two important references I frequently use to learn is :

1- The Jolt code in github where you can see how things work under the hood and what are the available functions:

https://github.com/bazaarvoice/jolt

2- A site that talks about the fundamental of jolt:

https://docs.digibee.com/documentation/components/tools/transformer-jolt/transformer-getting-to-know-jolt#cardinality

The rest is practice, practice and more practice. Its the only way you can get some depth in the understanding of jolt and its capabilities.  You can do that by trying to help and learn from others posts in this community forum or other forums like the below where you can find tons of jolt problems\solutions:

https://stackoverflow.com/questions/tagged/jolt

Hope that helps and thanks for accepting the solution.

 

 

Translate
omeraran
Silver ⭐️
June 5, 2024

hi SAMSAL, i send you message. Can you check it please ?

Translate