I started with Jolt, but I can't concatenate the elements of the array to single string,
I have json like this:
{
"partNb": "1234",
"partDescriptions": [
{
"country": "GB",
"language": "en",
"content": "1 tool description in en_GB"
},
{
"country": "GB",
"language": "en",
"content": "2 tool description in en_GB"
}
]
}
and with jolt spec:
[
{
"operation": "shift",
"spec": {
"partNb": "id",
"partDescriptions": {
"*": {
"content": "description"
}
}
}
}
]
For this I have this output:
{
"id" : "1234",
"description" : [ "1 tool description in en_GB", "2 tool description in en_GB" ]
}
but how to get result like this?:
{
"id" : "1234",
"description" : "1 tool description in en_GB , 2 tool description in en_GB"
}
Spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
"description": "=join(', ',#(1,description))"
}
}
]
To get only content fields concatenated into descriptions:
[
{
"operation": "shift",
"spec": {
"partDescriptions": {
"*": {
"content": {
"#": "content"
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"description": "=join(', ', #(2,content))"
}
}
]
Output:
{
"content" : [ "1 tool description in en_GB", "2 tool description in en_GB" ],
"description" : "1 tool description in en_GB, 2 tool description in en_GB"
}
Related
Is there a way in Jolt to convert a dot operator in JSON value to a nested JSON object while transforming the input ?
For example in the below json I would like target_property of id=2 to be converted to a json object as shown in the expected O/P. Any help is appreciated
Input:
{
"name": "Test",
"email": "Test",
"form_id": "123",
"field_list": [
{
"id": 1,
"value": "Test Subject",
"is_custom_field": false,
"target_property": "subject"
},
{
"id": 2,
"value": "Test Description",
"is_custom_field": false,
"target_property": "comment.body"
}
]
}
Jolt Transform tried
[
{
"operation": "shift",
"spec": {
"form_id": "ticket.ticket_form_id",
"name": "ticket.requester.name",
"email": "ticket.requester.email",
"field_list": {
"*": {
"is_custom_field": {
"true": {
"#(2,target_property)": "ticket.custom_fields[&3].id",
"#(2,value)": "ticket.custom_fields[&3].value"
},
"*": {
"#(2,value)": "ticket.#(3,target_property)"
}
}
}
}
}
}
]
Current Output
{
"ticket" : {
"ticket_form_id" : "123",
"requester" : {
"name" : "Test",
"email" : "Test"
},
"subject" : "Test Subject",
"comment.body" : "Test Description"
}
}
Expected Output
{
"ticket": {
"ticket_form_id": "123",
"requester": {
"name": "Test",
"email": "Test"
},
"subject": "Test Subject",
"comment": {
"body": "Test Description"
}
}
}
You can add another shift transformation spec such that
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&",
"*.*": "&1.&(0,1).&(0,2)"
}
}
}
where
"*.*" represents the attributes having a dot within them. the zeroes
within &(0,..) expressions stand for the current level
1,2 as second arguments of them represent the first and the second pieces of the splitted keys (in this case comment.body)
respectively.
I am trying to convert below JSON into name value pair :
{
"Size": "2",
"done": "true",
"records": [
{
"Id": "a7g6s0000004GZuAAM",
"NN": "00096411.0",
"Name": "ISOLIN TRADE & INVEST",
"RecordType": {
"attributes": {
"type": "TestType"
},
"Name": "Term"
}
},
{
"Id": "a7g6s0000004GZzAAM",
"Number": "00096412.0",
"Name": "ISOLIN"
}
]
}
Expecting output JSON :
{
"Size" : "2",
"done" : "true",
"Items" : [ {
"Fields" : [ {
"Name" : "Id",
"Value" : "a7g6s0000004GZuAAM"
}, {
"Name" : "NN",
"Value" : "00096411.0"
}, {
"Name" : "Name",
"Value" : "ISOLIN TRADE & INVEST"
}, {
"Name" : "RecordType_Name",
"Value" : "Term"
} ]
}, {
"Fields" : [ {
"Name" : "Id",
"Value" : "a7g6s0000004GZzAAM"
}, {
"Name" : "Number",
"Value" : "00096412.0"
}, {
"Name" : "Name",
"Value" : "ISOLIN"
} ]
} ]
}
I am using below jolt spec but transformation of RecordType element is not as expected :
Jolt Spec :
[
{
"operation": "remove",
"spec": {
"records": {
"*": {
"attributes": " "
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"records": {
"*": {
"*": {
"$": "Items.&2.[#2].Name",
"#": "Items.&2.[#2].Value"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"Items": {
"*": {
"*": "Items.[#2].Fields[]"
}
}
}
}
]
How can I transform this into required format ?
I think remove transformation is not needed, and better to index each Field.
The trick for the solution would be distinguish RecordType attribute from the others by use of
"RecordType": {"Name": "Field&2.&1\\_&"}
such as
[
{
"operation": "shift",
"spec": {
"*": "&",
"records": {
"*": {
"*": "Field&1.&",
"RecordType": {
"Name": "Field&2.&1\\_&"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"Field*": {
"*": {
"$": "Items[0].&2.[#2].Name",
"#": "Items[0].&2.[#2].Value"
}
}
}
}
]
I've following input json
{
"NAME1": "Mueller",
"NAME2": "Max",
"STREET1": "Adlerstr.",
"STREET2": "1",
"COUNTRY": "Munich"
}
I renamed the name of the fields and concatinate the street* fields
[
{
"operation": "shift",
"spec": {
"NAME1": "name",
"NAME2": "surName",
"STREET1": "tmp_street1",
"STREET2": "tmp_street2",
"COUNTRY": "country"
}
},
{
"operation": "modify-default-beta",
"spec": {
"street": "=concat(#(1,tmp_street1),' ',#(1,tmp_street2))"
}
},
{
"operation": "remove",
"spec": {
"tmp_*": ""
}
}
]
I getting the following output
{
"name" : "Mueller",
"surName" : "Max",
"country" : "Munich", // Should be after the street field
"street" : "Adlerstr. 1"
}
Is it possible to move the street field on the 3rd position in the json output ?
Yes, street field can be moved to the 3rd position of the json output, with only two operations.
Check this spec,
[
{
"operation": "modify-default-beta",
"spec": {
"street": "=concat(#(1,STREET1),' ',#(1,STREET2))"
}
},
{
"operation": "shift",
"spec": {
"NAME1": "name",
"NAME2": "surName",
"street": "street",
"COUNTRY": "country"
}
}
]
Trying to make a Jolt script that will put in a single number line, then a array of strings in one single array and a tag on the end of that array. These are the values that I have been working with.
JSON INPUT
[
{
"foo": "111",
"bar": "222",
"sun": "333",
"ListofStrings": [
"Dog",
"Train"
],
"ID": "BLAH"
},
{
"foo": "999",
"bar": "222",
"sun": "777",
"ListofStrings": [
"CAT",
"PLANE"
],
"ID": "HAHA"
}
]
JOLT SPEC This is what I have been working with that prints out the ListofStrings but this is the one that works stably.
[
{
"operation": "shift",
"spec": {
"*": {
"foo": "input[].number",
"bar": "input[].number",
"sun": "input[].number",
"ListofStrings": "input[].List"
}
}
},
{
"operation": "default",
"spec": {
"app_id": "test",
"input": {
"*": {
"app_id": "test"
}
}
}
}
]
CURRENT OUTPUT
{
"input" : [ {
"number" : "111"
}, {
"number" : "222"
}, {
"number" : "333"
}, {
"List" : [ "Dog", "Train" ]
}, {
"number" : "999"
}, {
"number" : "222"
}, {
"number" : "777"
}, {
"List" : [ "Cat", "Car" ]
} ],
"app_id" : "test"
}
DESIRED OUTPUT
{
"input" : [ {
"number" : "111"
"List" : [ "Dog", "Train" ]
"ID": "BLAH_foo"
}, {
"number" : "222"
"List" : [ "Dog", "Train" ]
"ID": "BLAH_bar"
}, {
"number" : "333"
"List" : [ "Dog", "Train" ]
"ID": "BLAH_sun"
}, {
"number" : "999"
"List" : [ "Cat", "Car" ]
"ID": "HAHA_foo"
}, {
"number" : "222"
"List" : [ "Cat", "Car" ]
"ID": "HAHA_bar"
}, {
"number" : "777"
"List" : [ "Cat", "Car" ]
"ID": "HAHA_sun"
} ],
"app_id" : "test"
}
Check this spec
[
//Converting list to Map
{
"operation": "shift",
"spec": {
"*": {
"ListofStrings": null,
"*": {
"#": "#1.number",
"#(1,ListofStrings)": "#1.list"
}
}
}
},
//Shift the number and list to the input array
{
"operation": "shift",
"spec": {
"*": {
"$": "input[#2].number",
"#(0,list)": "input[#2].List"
}
}
}, {
"operation": "default",
"spec": {
"app_id": "test",
"input": {
"*": {
"app_id": "test"
}
}
}
}
]
Edit 1
Add the ID node to the map using first shift operation "ID": null, and "#(1,ID)": "#1.ID". Then shift the ID node to the input array in the second shift operation "#(0,ID)": "input[#2].ID".
[
//Converting list to Map
{
"operation": "shift",
"spec": {
"*": {
"ListofStrings": null,
"ID": null,
"*": {
"#": "#1.number",
"#(1,ListofStrings)": "#1.list",
"#(1,ID)": "#1.ID"
}
}
}
},
//Shift the number and list to the input array
{
"operation": "shift",
"spec": {
"*": {
"$": "input[#2].number",
"#(0,list)": "input[#2].List",
"#(0,ID)": "input[#2].ID"
}
}
}, {
"operation": "default",
"spec": {
"app_id": "test",
"input": {
"*": {
"app_id": "test"
}
}
}
}
]
I have a JSON like this
{
"id": "1234",
"name": "something",
"list": [
{
"A": "Something"
},
{
"B": "Something1"
}
]
}
What I would like is to do is add id and name to both the JSON's present inside list
I've gone through a couple of questions and I couldn't find anywhere where somebody had done this.
I believe the following Shift spec will work:
{
"id|name": "&",
"list": {
"*": {
"#(2,id)": "&2.[&1].id",
"#(2,name)": "&2.[&1].name",
"*": "&2.[&1].&"
}
}
}
With your sample data, the output produced was:
{
"id": "1234",
"name": "something",
"list": [{
"id": "1234",
"name": "something",
"A": "Something"
}, {
"id": "1234",
"name": "something",
"B": "Something1"
}]
}
This spec should give you what you want:
[
{
"operation": "shift",
"spec": {
"list": {
"*": {
"#(2,id)": "&2.[&1].id",
"#(2,name)": "&2.[&1].name",
"*": "&2.[&1].&"
}
}
}
}
]
With your input it gives the following as output:
{
"list" : [ {
"A" : "Something",
"id" : "1234",
"name" : "something"
}, {
"B" : "Something1",
"id" : "1234",
"name" : "something"
} ]
}