JSLT Conversion
Sample Data:
{
"name": "Homer J. Simpson",
"occupation": "Security inspector",
"street": "Evergreen Terrace",
"number": "742",
"city": "Springfield",
"state": "Kentucky",
"parents": {
"father": {
"name": "Abraham",
"age": "80"
},
"mother": {
"name": "Mona",
"age": "70"
}
}
}
Problems & Solution:
Refer some useful links here:
Link for library: https://github.com/schibsted/jslt
Link for JSLT playground for testing: https://www.garshol.priv.no/jslt-demo
Link for JSLT Tutorial: https://github.com/schibsted/jslt/blob/master/tutorial.md
Author's note: https://www.slideshare.net/larsga/jslt-json-querying-and-transformation
1. Omission of object, array, or any value by the key
{
* - parents, spouse, grandParents : .
}
- the above line will return whole object as it is but will eliminate any values with the key "parents", "spouse", "grandParents". the comma separation works as OR ( || ) operator.
Output:
{
"name" : "Homer J. Simpson",
"occupation" : "Security inspector",
"street" : "Evergreen Terrace",
"number" : "742",
"city" : "Springfield",
"state" : "Kentucky"
}
2. Renaming of any key (RENAME)
{
"fullName": .name,
* - name: .
}
- the above line will first create a key "fullName" with the value of "name" from input and then in 2nd line it will remove the old "name" field from input object
Output:
{
"fullName" : "Homer J. Simpson",
"occupation" : "Security inspector",
"street" : "Evergreen Terrace",
"number" : "742",
"city" : "Springfield",
"state" : "Kentucky",
"parents" : {
"father" : {
"name" : "Abraham",
"age" : "80"
},
"mother" : {
"name" : "Mona",
"age" : "70"
}
}
}
3. Merging two values into one (MERGE)
{
"address": .number + ", " + .street + ", " + .city + ", " + .state,
* - number, street, city, state: .
}
- the above code will create new field called address which combines the values of fields like "number", "street", "city" and "state" with + (plus) operator as concatenation.
- the second line in code removes the unnecessary values of the merged values and return the rest object as it is.
Output:
{
"address" : "742, Evergreen Terrace, Springfield, Kentucky",
"name" : "Homer J. Simpson",
"occupation" : "Security inspector",
"parents" : {
"father" : {
"name" : "Abraham",
"age" : "80"
},
"mother" : {
"name" : "Mona",
"age" : "70"
}
}
}
4. Flattening objects (FLATTEN)
{
"father": .parents.father.name,
"mother": .parents.mother.name,
* - parents: .
}
- the above given code creates new fields with desired key name, "father" and "mother" in this instance. To access values inside objects we use . (dot) operator to access nested key-value.
- Then eliminate flattened object using - (minus) operator as shown in 3rd line.
Output:
{
"father" : "Abraham",
"mother" : "Mona",
"name" : "Homer J. Simpson",
"occupation" : "Security inspector",
"street" : "Evergreen Terrace",
"number" : "742",
"city" : "Springfield",
"state" : "Kentucky"
}
5. Ignore the objects and translate them as it is (IGNORE)
- Write the script for only those objects you need to operate upon, the rest will be carried out using rest expression (* : .)
- It will by default access all the elements in current scope of the object. To avoid that, we use omission of unnecessary fields.
6. Ordering the fields or sorting them (SORTING & ORDERING)
- Ordering is not currently supported in JSLT and neither is sorting.
- JSON itself is unordered directory of elements having key and value pair, so it does not need to be sorted or ordered to work with anyways.
7. Recursive traversal using DFS and deletion of field (RECURSIVE DELETE)
def deleteByKey(jsonObj, searchTerm)
if (is-object($jsonObj))
{for ($jsonObj)
.key:
if (is-object(.value) or is-array(.value))
deleteByKey(.value, $searchTerm)
else if (.key != $searchTerm)
.value
}
else if (is-array($jsonObj))
[for($jsonObj)
deleteByKey(., $searchTerm)
]
deleteByKey(., "age")
- The above given code takes two parameters, first one is the json payload you want to perform the task upon and second one is the term you want to search and delete
- The operation performed here is deletion of any field regardless of if it exists, in which sub-object it exists, or is it object or array.
Warning: The scenario in which this function can be used is very specific. Using it without reading documentation may result in loss of data.
Output:
{
"name" : "Homer J. Simpson",
"occupation" : "Security inspector",
"street" : "Evergreen Terrace",
"number" : "742",
"city" : "Springfield",
"state" : "Kentucky",
"parents" : {
"father" : {
"name" : "Abraham"
},
"mother" : {
"name" : "Mona"
}
}
}
8. Recursive traversal using DFS and updation of field (RECURSIVE UPDATE)
def updateByKey(jsonObj, searchTerm, updateData)
if (is-object($jsonObj))
{for ($jsonObj)
.key:
if ((is-object(.value) or is-array(.value)) and .key != $searchTerm)
updateByKey(.value, $searchTerm, $updateData)
else if (.key == $searchTerm)
$updateData
else
.value
}
else if (is-array($jsonObj))
[for($jsonObj)
updateByKey(., $searchTerm, $updateData)
]
updateByKey(., "city", "Surat")
- The above given code takes three parameters, first one is the json payload you want to perform the task upon and second one is the term you want to search and update, and the third one is the value you want to update the specific field with.
- The operation performed here is updation of any field regardless of if it exists, in which sub-object it exists, or is it object or array.
Warning: The scenario in which this function can be used is very specific. Using it without reading documentation may result in mal-data.
Output:
{
"name" : "Homer J. Simpson",
"occupation" : "Security inspector",
"street" : "Evergreen Terrace",
"number" : "742",
"city" : "Springfield",
"state" : "Kentucky",
"parents" : {
"father" : {
"name" : "Abraham",
"age" : 30
},
"mother" : {
"name" : "Mona",
"age" : 30
}
}
}