using jolt to transform empty field - jolt

my json looks like this
{
"group":{
"personnel":[
{
"member":{
"id":"1",
"name":"John"
}
},
{
"member":{
"id":"2",
"name":"Doe"
}
}
]
}
}
and the expected output for this is
[
{
"id":"1",
"name":"John"
},
{
"id":"2",
"name":"Doe"
}
]
but, there's also some time when the json is empty like so:
{}
for this, I want the output to be
[]
my spec looks like this
"spec":{
"group":{
"personnel":{
"*":{
"*":"[]"
}
}
}
}
but this doesnt work on the second case where json is empty, it'll just return null. do I need to add anything?

A tad clunky but this works.
Spec
[
{
"operation": "shift",
"spec": {
"group": {
"personnel": {
"*": {
// write to a temp array so that we can
// default it into existence later if needed
"*": "temp[]"
}
}
}
}
},
{
"operation": "default",
"spec": {
// If "temp" does not exist, make it be an empty array
"temp": []
}
},
{
"operation": "shift",
"spec": {
// write value at "temp" to the root level
"temp": ""
}
}
]

Related

Jolt transformation to retrieve key as value

I have this JSON input that I would like to transform via jolt spec
{
"message": {
"trx": {
"trxId": "1234"
},
"translation": {
"transactions": {
"1234": "http://www.trythisjolt.com"
}
}
}
}
expected output would be something like this
{
"message": {
"trx": {
"trxId": "1234",
"trxName": "http://www.trythisjolt.com"
}
}
}
Is this achievable with Jolt please? I have tried several possibilities but I'm very new to Jolt and have not managed so far
Yes achievable, you can use
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"trxId": "message.trx.&",
"transactions": { "*": "message.trx.trxName" }
}
}
}
}
]
where go to the innermost elements firstly, then prepend message.trx. for both while renaming the second one to the desired value. This way, both of them are nested within the common object.

Newbie: Transform json repetitions to Single Arrays and add Entry Inside

I'm looking for some Jolt help. I'm officially a newbie attempting to understand if Jolt will suffice for usage in our solution. I can't seem to get the following output as I would expect.
I was wondering if anyone could help me, and see if this template would be able to help out on our other needs for structures of the same fashion. Also, if anyone has the best/better reading they suggest on Jolt, I'm happy to hear of that as well.
In a nutshell, I'm trying to use JOLT to handle the following Needs:
From a JSON input, if there are multiple repetitions of a structure, output these repetitions as an array of the independent json structures.
From the parent level, take a named match SetIDAL1 and place into the top created json structure, but not as part of the array, and as its own element.
Here is an example of my input I need to transform:
"AL1": {
"0": {
"AllergenCodeMnemonicDescription": {
"1": {
"Text": "ASPIRIN",
"ID": "TEST1"
},
"2": {
"Text": "TYLENOL",
"ID": "TEST2"
}
},
"SetIDAL1": "1"
},
"1": {
"AllergenCodeMnemonicDescription": {
"1": {
"Text": "ADVIL"
}
},
"SetIDAL1": "2"
}
}
}
My current spec I'm using seems to get me pretty close. However, I can't get the arrays up a level in order to remove the 0:1, nor fix the fact that I want the SetIDAL1 value placed inside the newly created array object, instead it makes it's own array object. I've played around with various other options that only lead me further away. Any help for the solution and input/guidance, general "smart ways" to look at this issue would be appreciated.
Unfortunately, I do not have a copy of previous work I tried, which would perform the matching on all groups and map them as expected. I started moving toward matching each individual 0/1 object underneath my input in attempts to see if I could "bury" the SetIDAL properly, to no avail. I really do not want to code for each level, but hoping there's a solution for the "problem at hand" that someone can assist me with.
[
{
"operation": "shift",
"spec": {
"AL1": {
"0": {
"AllergenCodeMnemonicDescription": {
"#": "AL1.[].AllergenCodeMnemonicDescription.[]"
},
"SetIDAL1": "AL1.[].SetIDAL1"
},
"1": {
"AllergenCodeMnemonicDescription": {
"*": "AL1.[].AllergenCodeMnemonicDescription.[]"
},
"SetIDAL1": "AL1.[].SetIDAL1"
}
}
}
}
]
here is the output I am getting. I assume potentially I need yet another shift after this, to bring the "1"/"2" levels "UP again" somehow. But I can't seem to get the SetIDAL1 in the correct place as stated before.
{
"AL1" : [ {
"AllergenCodeMnemonicDescription" : [ {
"1" : {
"Text" : "ASPIRIN",
"ID" : "TEST1"
},
"2" : {
"Text" : "TYLENOL",
"ID" : "TEST2"
}
} ]
}, {
"SetIDAL1" : "1"
}, {
"AllergenCodeMnemonicDescription" : [ {
"Text" : "ADVIL"
} ]
}, {
"SetIDAL1" : "2"
} ]
}
Here is the output I need:
{
"AL1": [
{
"AllergenCodeMnemonicDescription": [
{
"Text": "ASPIRIN",
"ID": "TEST1"
},
{
"Text": "TYLENOL",
"ID": "TEST2"
}
],
"SetIDAL1": "1"
},
{
"AllergenCodeMnemonicDescription": [
{
"Text": "ADVIL"
}
],
"SetIDAL1": "2"
}
]
}
As the indexes start at 1 (not 0) was getting a null on the first element from each Allergen array and had to remove it in the end (not sure if theres a better way to do this), but this specs will do the trick:
[
{
"operation": "shift",
"spec": {
"AL1": {
"*": {
"AllergenCodeMnemonicDescription": {
"*": {
"Text": "AL1.[&3].AllergenCodeMnemonicDescription.[&1].&",
"ID": "AL1.[&3].AllergenCodeMnemonicDescription.[&1].&"
}
},
"SetIDAL1": "AL1.[&1].&"
}
}
}
},
{
"operation": "remove",
"spec": {
"AL1": {
"*": {
"AllergenCodeMnemonicDescription": {
"0": ""
}
}
}
}
}
]

elasticsearch dynamic templates for custom mappings not working

I'm trying to add dynamic templates. I've done
curl -XPUT 'localhost:9200/my_index_items?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"item": {
"dynamic_templates": [
{
"sales_price_in_cent_as_long": {
"match": "sales_price_in_cent",
"mapping": {
"type": "long"
}
},
"sales_price_formatted_as_double": {
"match": "sales_price_formatted",
"mapping": {
"type": "double"
}
},
...
}
]
}
}
}
'
So in "my_index_items" i got the type "item", when I now index an item (item has a field "sales_price_formatted" which has a value like "12,34") and lookup with GET 'localhost:9200/my_index_items/item/_mapping' it shows the "sales_price_formatted" as of type "text" instead of "double".
Am I doing something wrong? I thought, if an item has a field which is defined in the "match" property in my dynamic templates, then it wouldn't use its own default mechanism but the one defined here?
You have some missing curly brackets in your command. It should be like this:
{
"mappings": {
"item": {
"dynamic_templates": [
{
"sales_price_in_cent_as_long": {
"match": "sales_price_in_cent",
"mapping": {
"type": "long"
}
}
},
{
"sales_price_formatted_as_double": {
"match": "sales_price_formatted",
"mapping": {
"type": "double"
}
}
}
]
}
}
}

JSON to JSON changing array to one long string using JOLT

Lets say my json is:
{
"rating": {
"primary": {
"value": ["a","B",1]
}
}
}
What I want to achieve is:
{
"values": "a, B, 1"
}
I'm using jolt json to json code.
It is not possible with stock transformations of Jolt. You need to write your custom transformation. Check the documentation section on how to write your own transformations.
you can try the following spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
"value": "=concat(#(2,rating.primary.value[0]),',',#(2,rating.primary.value[1]),',',#(2,rating.primary.value[2]))"
}
},
{
"operation": "remove",
"spec": {
"rating": ""
}
}
]

IF condition with logical operators in JOLT mapping

Is there a way to use if condition with AND / OR in JOLT?
I have a json where i need to check two field values and then set the mapping.
E.g
**Input:**
{
"oldStatus":"NEW",
"newStatus":"OPEN"
}
The JOLT logic should be doing something like this:
if(oldStatus == "NEW" AND newStatus=="OPEN")
then
status = "In Progress"
**Expected Output**
{
"status":"In Progress"
}
Try this spec,
[
{
"operation": "modify-default-beta",
"spec": {
"fullStat": "=concat(#(1,oldStatus),#(1,newStatus))"
}
},
{
"operation": "shift",
"spec": {
"fullStat": {
"NEWOPEN": {
"#In Progress": "status"
},
"*": {
"#Not In Progress": "status"
}
}
}
}
]