Say I have the following Json:
{
"collection": [
{
"sequence": 1,
"value": "event1"
},
{
"sequence": 2,
"value": "event2"
},
{
"sequence": 3,
"value": "event3"
}
]
}
Given that the array is not guaranteed to be sorted by "sequence", how do I extract the element that has the highest value of "sequence"?
the sub-objects of the "collection" array might be sorted by the sequence values by putting them as indexes for reformed array (call "objectWithHighestSequence") such as
[
{
"operation": "shift",
"spec": {
"collection": {
"*": "objectWithHighestSequence[#(0,sequence)]"
}
}
},
{
// only pick the last object after sorting within the shift transformation
"operation": "modify-overwrite-beta",
"spec": {
"*": "=lastElement(#(1,objectWithHighestSequence))"
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
Related
I need to merge array data from two different arrays from the Input JSON and make a flat list of items in the Output JSON. The first array contains the key required for the output and the second array contains the value.
So far, all my attempts at a spec haven't even come close, which is why I haven't listed one. Please see the Input and Desired Output below. Thanks for any help!!
Input JSON :
{
"data": [
{
"2": {
"value": "DC1"
},
"3": {
"value": 5
}
},
{
"2": {
"value": "DC2"
},
"3": {
"value": 10
}
}
],
"fields": [
{
"id": 2,
"label": "DataCenter",
"type": "text"
},
{
"id": 3,
"label": "CCount",
"type": "numeric"
}
],
"metadata": {
"numFields": 2,
"numRecords": 2,
"skip": 0,
"totalRecords": 2
}
}
Desired Output JSON:
[
{
"DataCenter": "DC1",
"CCount": "5"
},
{
"DataCenter": "DC2",
"CCount": "10"
}
]
You can use this spec:
[
{
"operation": "shift",
"spec": {
"data": "&",
"fields": {
"*": {
"label": "fields.#(1,id)"
}
}
}
},
{
"operation": "shift",
"spec": {
"data": {
"*": {
"*": {
"*": "[&2].#(4,fields.&1)"
}
}
}
}
}
]
In your desired output CCount value is a string, You can add the below spec to change an integer to a string.
,
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"CCount": "=toString"
}
}
}
You might reciprocally match id value of fields array vs. the object keys of the data array within a common object within the first shift transformation spec in order to prepare for the second one in which we tile the attributes into their individual objects such as
[
{
"operation": "shift",
"spec": {
"*": { // represents the node for both "data" and "fields" arrays
"*": {
"*": {
"value": "&1.&",
"#1,label": "#2,id.&"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"value": {
"*": "[&].#2,label"
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
I have been trying to set key names for json array using the fields provided. I need to fetch a separate list of managers and colleagues.
Input:
{
"employeelist": [
{
"employee": "test",
"firstName": "ABC",
"lastName": "DEF"
},
{
"employee": "test1",
"firstName": "nametest",
"lastName": "namelast"
}
],
"manager": "test",
"colleague": "test1"
}
Expected Output:
{
"manager": [
{
"employee": "test",
"firstName": "ABC",
"lastName": "DEF"
}
],
"colleague": [
{
"employee": "test1",
"firstName": "nametest",
"lastName": "namelast"
}
]
}
Spec I used repeats the the complete list for both managers and colleagues.
[
{ // segregate values of the same key and form respective arrays.
"operation": "shift",
"spec": {
"employeelist": {
"*": {
"employee": {
"#(3,manager)": {
"#2": "manager.[]"
},
"#(3,colleague)": {
"#2": "colleague.[]"
}
}
}
}
}
}
]
You can use the following shift transformation spec
[
{
"operation": "shift",
"spec": {
"employeelist": {
"*": {
"#": "#1,employee" // accumulate values of "manager" and "colleague" attributes with respective "employee" values under the common arrays labeled by those values
}
},
"*": {
"$": "#(0)" // exchange the key-value pairs of the attributes "manager" and "colleague"
}
}
},
{
"operation": "shift",
"spec": {
"*": { // all newly transformed arrays
"#1,&[0]": "#(2,&[1])[]" //match values of 0th and 1st components per each array while adding a [] suffix to convert the resultant objects nested within square brackets as desired
}
}
}
]
where the first components([0]) of the newly derived arrays(test and test1) are matched with the second components([1]) within the last spec
the demo on the site http://jolt-demo.appspot.com/ is
I think this simpler jolt spec might achieve the expected output, but please note that it works only if the document that needs to be moved to the "manager" field is the first element of the array (that's why it matches the 0 index), and the document of "colleague" is the second element of the array (index 1).
[
{
"operation": "shift",
"spec": {
"employeelist": {
"0": "manager[]",
"1": "colleague[]"
}
}
}
]
Hope you find it useful!
EDIT: The key names are dynamic and unknown.
I want to take an Object and create an String array that has each key and value concated together.
My keys contain underscores which I need to get rid of (I have that part working). I'm struggling to figure out the next step to join everything together. (I guess I'm missing how to reference key and value from RHS?)
Input:
{
"object": {
"key_1": [
"A",
"B"
],
"key_2": [
"C"
],
"key_3": [
"D",
"E",
"F"
]
}
}
Desired Output:
{
"results": [
"key 1: A B",
"key 2: C",
"key 3: D E F"
]
}
Spec:
[
{
"operation": "shift",
"spec": {
"object": {
"*_*": "results.&(0,1) &(0,2)",
"*": "results.&"
}
}
}
]
Current spec output:
{
"results": {
"key 1": [
"A",
"B"
],
"key 2": [
"C"
],
"key 3": [
"D",
"E",
"F"
]
}
}
Seems you need to use modify transformations with join functions, along with shift transformation specs such as
[
{
// combine the components of the each array with one extra whitespaces between them
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": "=join(' ',#(1,&))"
}
}
},
{
// get rid of underscores
"operation": "shift",
"spec": {
"*": {
"*_*": "&1.&(0,1) &(0,2)"
}
}
},
{
// generate new arrays with the first components are the keys, and second ones are the values of the previous attributes of the object
"operation": "shift",
"spec": {
"*": {
"*": {
"$": "&",
"#": "&"
}
}
}
},
{
// get concatenated values for key-value pairs with colons between them
"operation": "modify-overwrite-beta",
"spec": {
"*": "=join(': ',#(1,&))"
}
},
{
// get the desired result as a single array
"operation": "shift",
"spec": {
"*": {
"#": "results[]"
}
}
}
]
the demo on the site https://jolt-demo.appspot.com/ is :
Problems
Hi, I'm using Jolt to transform e-commerce products data. For e.g:
[
{
"objectID": 1,
"string_facet": [
{
"facet_name": "eco_collection",
"facet_value": "No"
},
{
"facet_name": "performance_fabric",
"facet_value": "No"
}
]
},
{
"objectID": 2,
"string_facet": [
{
"facet_name": "activity",
"facet_value": [
"Hiking"
]
}
]
}
]
And my desired output is:
[
{
"objectID": 1,
"string_facet": {
"eco_collection": "No",
"performance_fabric": "No"
}
},
{
"objectID": 2,
"string_facet": {
"activity": [
"Hiking"
]
}
}
]
What I have done so far
I have tried this spec, but it isn't what I need:
[
{
"operation": "shift",
"spec": {
"*": {
"*": "[&1].&",
"string_facet": {
"*": {
"#facet_value": "[&1].string_facet.#facet_name"
}
}
}
}
}
]
I'm looking for an explanation in addition to solution.
Any help would be much appreciated!
You principally need "#facet_value": "#facet_name" match, start with it, then add
&2 to represent going two levels up and grabbing the key name's value(string_facet)
&3 to represent going three levels up and grabbing the indices of the main array to accumulate each attributes nested within individual object of their own.
Then, add an extra shift transformation spec to get rid of the integer key names such as
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&1.&",
"string_facet": {
"*": {
"#facet_value": "&3.&2.#facet_name"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": ""
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
In a Jolt transform Im trying to filter an array of objects by an id, if equal to an Id on a field outside of the array.
Here is my data
{
"position": {
"positionManagerId": "123"
},
"managers": [
{
"id": "123",
"name": "John"
},
{
"id": "456",
"name": "Jack"
}
]
}
Id like to grab the manager where
managers.id == position.positonManagerId
My output would look like
{
"managerId": "123",
"managerName": "John"
}
You can apply shift transformation twice.
Combine key-value pairs under the common id values as key names within the first one so as to get a list for the searched id value( 123 in this case ) while the other pair(s) won't be lists.
Then use index values(0and1) within the second spec in order to eliminate the pair(s) without list value such as
[
{
"operation": "shift",
"spec": {
"position": {
"*": {
"$": "#(0)"
}
},
"managers": {
"*": {
"#name": "#id"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"0": {
"$1": "managerId"
},
"1": "managerName"
}
}
}
]