Edit1: My goal is to be able to remove a specific attribute that has a specific value. In my example, the rating attribute should be removed as soon as it has a "?" as content.
"rating": "?" => should be deleted
"rating": "1" => should NOT be deleted
Hi all, is there any way that I don't remove all attributes of a certain type, but only those that have a certain content? Example:
"test1": "123",
"test1":"?"
Here I would like to remove only test1:"?".
Input:
{
"TEST": {
"revision": "2021",
"results": [
{
"parameter": "Abweichung X L/R",
"example": "123",
"rating": "1"
},
{
"parameter": "10001",
"example": "BIN7934659050003696",
"rating": "?"
},
{
"parameter": "1015 Bauteil niO",
"rating": "0"
}
]
}
}
Desired Output:
{
"TEST": {
"revision": "2021",
"results": [
{
"parameter": "Test",
"example": "123456",
"rating": "1"
},
{
"parameter": "10001",
"example": "123"
},
{
"parameter": "Test123",
"rating": "0"
}
]
}
}
All "ratings" except the one with the content "?" remain.
Thanks in advance and many greetings
You can use below shift transformation spec in order to remove the attribute rating with value ?
[
{
"operation": "shift",
"spec": {
"TEST": {
"*": "&1.&",
"results": {
"*": {
"rating": {
"?": {
"#(2,parameter)": "&5.&4.[&3].parameter",
"#(2,example)": "&5.&4.[&3].example"
},
"*": {
"#2": "&5.&4.[&3]"
}
}
}
}
}
}
}
]
where
"*" represents the rest of the keys(revision in this case) other than specified(results)
the &1 at the top and &5s represent the outermost key(TEST)
the &4s represent the array's(results) name
the [&3] s are needed to combine the independent sub-elements due to the indexes of the array(results) as a common center
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 have requirement where input will have one or two arrays based on scenarios:
Input 1:
{
"name": "xyz",
"age": "30",
"addressA": [
{
"id": "111111",
"details": ""
},
{
"id": "111112",
"details": ""
}
],
"addressB": [
{
"id": "222222",
"details": ""
},
{
"id": "222223",
"details": ""
}
]
}
Input 2:
{
"name": "xyz",
"age": "30",
"addressB": [
{
"id": "222222",
"details": ""
},
{
"id": "222223",
"details": ""
}
]
}
So when 'addressA' is present in input json it should use that to give output if its no present then it should use 'addressB' to populate the same
Output expected:
For input 1:
[
{
"id": "111111",
"details": ""
},
{
"id": "111112",
"details": ""
}
]
For Input 2:
[
{
"id": "222222",
"details": ""
},
{
"id": "222223",
"details": ""
}
]
Any help would be appreciated.
I see option in JOLT to have if-else based on string value, but could not find anything to check just if exists
Thanks
Mahendra
You can determine the arrays A and B within the first shift transformation spec, and then use conditional within the second one such as
[
{
"operation": "shift",
"spec": {
"address*": {
"*": "&(1,1)" // in order to determine the labels of the arrays as "A" and "B"
}
}
},
{
"operation": "shift",
"spec": {
"A": {
"#": ""
},
"*": "&"
}
},
{
"operation": "shift",
"spec": {
"*": ""
}
}
]
I have a problem writing objects to an array. Basically I want to merge the arrays and rename the fields, while keeping the objects seperately.
My input json looks like this:
{
"board":[
{
"role":"Head of board",
"id":"111",
"name":"John Snow"
}
],
"leaders":[
{
"role":"Accounting leader",
"id":"222",
"name":"Amanda Johns"
},
{
"role":"HR leader",
"id":"333",
"name":"Frank Smith"
}
]
}
This is my spec: (I am aware that the values in brackets are probably not right)
[
{
"operation":"shift",
"spec":{
"board":{
"*":{
"id":"employees.bosses[#2].emp_num",
"role":"employees.bosses[#2].position",
"name":"employees.bosses[#2].name"
}
},
"leaders":{
"*":{
"id":"employees.bosses[#2].emp_num",
"role":"employees.bosses[#2].position",
"name":"employees.bosses[#2].name"
}
}
}
}
]
and this is my output:
{
"employees": {
"bosses": [ {
"emp_num": ["111", "222"],
"position": ["Head of board", "Accounting leader"],
"name": ["John Snow", "Amanda Johns"]
}, {
"emp_num": "333",
"position": "HR leader",
"name": "Frank Smith"
} ]
}
}
while I expect output that looks like this:
{
"employees": {
"bosses": [ {
"emp_num": "111",
"position": "Head of board",
"name": "John Snow"
}, {
"emp_num": "222",
"position": "Accounting leader",
"name": "Amanda Johns"
}, {
"emp_num": "333",
"position": "HR leader",
"name": "Frank Smith"
} ]
}
}
I have major troubles understanding what to do and how the [#n] work, I would really appreciate any help with fixing my spec and explaination why this does/does not work!
Need to distinguish the indexes of the arrays while combining them. To accomplish this, add a suffix letter or word(here I chose a for a.[&1]) for the first array while renaming all keys as desired within the first shift operation, then apply new extensions employees.bosses in the consecutive shift operation such as
[
{
"operation": "shift",
"spec": {
"board": {
"*": {
"id": "a.[&1].emp_num",
"role": "a.[&1].position",
"name": "a.[&1].name"
}
},
"leaders": {
"*": {
"id": "&1.emp_num",
"role": "&1.position",
"name": "&1.name"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "employees.bosses"
}
}
]
I am quite new to JOLT and I need to transform my JSON files to the desired schema. This is my input
[
{
"PK": 12345,
"FULL_NAME":"Amit Prakash",
"BIRTHDATE":"1987-05-25",
"SEX":"M",
"EMAIL": "amprak#mail.com",
"PHONE": "809386731",
"TS":"2015-11-19 14:36:34.0"
},
{
"PK": 12654,
"FULL_NAME": "Rohit Dhand",
"BIRTHDATE":"1979-02-01",
"SEX":"M",
"EMAIL": "rodha#mail.com",
"PHONE": "937013861",
"TS":"2015-11-20 11:03:02.6"
},
...
]
and this is my desired output:
{
"records": [
{
"attribs": [{
"type": "customer",
"reference": "CUST"
}],
"name": "Amit Prakash",
"personal_email": "amprak#mail.com",
"mobile": "809386731",
"id": 12345
},
{
"attribs": [{
"type": "customer",
"reference": "CUST"
}],
"name": "Rohit Dhand",
"personal_email": "rodha#mail.com",
"mobile": "937013861",
"id": 12654
},
...
]
}
So far, I have only managed up to this point:
[
{
"operation": "remove",
"spec": {
"*": {
"BIRTHDATE": "",
"SEX": "",
"TS": ""
}
}
},
{
"operation": "shift",
"spec": {
"*": "records"
}
}
]
But I can't go on from here. I don't know how to rename keys in the output.
Also, what's the alternative to remove operation? remove operation is good if you have fewer keys to exclude than to include, but how about the reverse (few keys to include, more than to exclude within a JSON object)?
Spec
[
{
"operation": "shift",
"spec": {
"*": {
"PK": "records[&1].id",
"PHONE": "records[&1].mobile",
"EMAIL": "records[&1].personal_email",
"FULL_NAME": "records[&1].name"
}
}
},
{
"operation": "default",
"spec": {
"records[]": {
"*": {
"attribs[]": {
"0": {
"type": "customer",
"reference": "CUST"
}
}
}
}
}
}
]
Shift makes copy of your data, whereas the other operations do not. Thus, one way to remove stuff is to just not have it copied across in your shift spec.
Generally the remove is used to get rid of things that would "mess up" the shift.
Input JSON that I have to transform is as follows :
{
"Business": [
{
"Label": "Entertainment",
"category": "Advert",
"weight": "",
"types": [
"T1",
"T2"
]
},
{
"Label": "FMCG",
"category": "Campaign",
"weight": "",
"types": [
"T9",
"T10"
]
}
]
}
Expected Output :
{
"Business": [
{
"Label": "Entertainment",
"category": "Advert",
"weight": "",
"types": "T1"
},
{
"Label": "Entertainment",
"category": "Advert",
"weight": "",
"types": "T2"
},
{
"Label": "FMCG",
"category": "Campaign",
"weight": "",
"types": "T9"
},
{
"Label": "FMCG",
"category": "Campaign",
"weight": "",
"types": "T10"
}
]
}
I have tried the different JsonSpecs provided at the JOLT github help page. But I am not able to solve this. Any help or pointers will be appreciated.
You have to do two shift operations.
You want to "duplicate" the Label and Category based on how many entries you have in "types" array. So do that first, into a temporary "bizArray".
Also record which "type" goes with that duplicated Label and Category in a temporary "typeArray", that has the same indexes as the bizArray.
In the second shift, "join" the two parallel arrays, "bizArray" and "typesArray" to get your final array.
Spec
[
{
"operation": "shift",
"spec": {
"Business": {
"*": { // business array
"types": {
"*": { // type array
"#2": "bizArray[]", // make a copy of the whole biz object
"#": "typesArray[]"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"bizArray": {
"*": { // bizArray index
"Label": "Business[&1].Label",
"category": "Business[&1].category",
"weight": "Business[&1].weight"
}
},
"typesArray": {
"*": "Business[&].types"
}
}
}
]