Combines the key/value pairs into a single object & To get values in to array

 


Use Common Code -1 as Input in jsonata

https://jsonata.blogspot.com/2021/04/common-code-1.html

Query: 

"Phone": [
{
"type": "home",
"number": "0203 544 1234"
},
{
"type": "office",
"number": "01962 001234"
},
{
"type": "office",
"number": "01962 001235"
},
{
"type": "mobile",
"number": "077 7700 1234"
}
],


Query:Phone{type: number}
OP:{

"home": "0203 544 1234",
"office": [
"01962 001234",
"01962 001235"
],
"mobile": "077 7700 1234"
} Query:
Phone{type: number[]}
{
"home": [
"0203 544 1234"
],
"office": [
"01962 001234",
"01962 001235"
],
"mobile": [
"077 7700 1234"
]
}

Combining one property value to other property value as key value


Use Common Code -1 as Input in jsonata

https://jsonata.blogspot.com/2021/04/common-code-1.html

Query: 

"Phone": [
{
"type": "home",
"number": "0203 544 1234"
},
{
"type": "office",
"number": "01962 001234"
},
{
"type": "office",
"number": "01962 001235"
},
{
"type": "mobile",
"number": "077 7700 1234"
}

],

Query:Phone.{type: number} OP:[

{
"home": "0203 544 1234"
},
{
"office": "01962 001234"
},
{
"office": "01962 001235"
},
{
"mobile": "077 7700 1234"
}
]


 

getting different arrays data of same property

 

Getting data of different arrays having same property




Check common code-1 for input

Query:[Address, Other.`Alternative.Address`].City OP:[
"Winchester",
"London"
]



Getting only values of object as array output without keys & Getting All values by property name without chain


Use Common Code -1 as Input in jsonata

https://jsonata.blogspot.com/2021/04/common-code-1.html

IP: "Address": {

"Street": "Hursley Park",
"City": "Winchester",
"Postcode": "SO21 2JN"
},

Query: Address.* OP: [

"Hursley Park",
"Winchester",
"SO21 2JN"
]


To get one values of property query: *.Postcode op:"SO21 2JN"
To get all properties query: **.Postcode
op:[
"SO21 2JN",
"E1 6RF"
]

making string value as output array -("name"= op:["name"])


Use Common Code -1
https://jsonata.blogspot.com/2021/04/common-code-1.html

Address[].City op:["Winchester"]

Normal code

Address.City op:"Winchester" other:
  • Address[].City returns [ "Winchester"]
  • Phone[0][].number returns [ "0203 544 1234" ]
  • Phone[][type='home'].number returns [ "0203 544 1234" ]
  • Phone[type='office'].number[] returns [ "01962 001234", "01962 001235" ]

Getting limited records from array by giving from and to values.

 Use Common Code -1

https://jsonata.blogspot.com/2021/04/common-code-1.html


{
"Phone": [
{
"type": "1",
"number": "0203 544 1234"
},
{
"type": "2",
"number": "01962 001234"
},
{
"type": "3",
"number": "01962 001235"
},
{
"type": "4",
"number": "077 7700 1234"
}
]}

Query:
Phone[[2..3]] op:[
{
"type": "3",
"number": "01962 001235"
},
{
"type": "4",
"number": "077 7700 1234"
}
]

Getting Last Record from array



Use Common Code -1
https://jsonata.blogspot.com/2021/04/common-code-1.html

Query To get last record of array
last record: Phone[-1] last 2nd record: Phone[-2] last 3rd record: Phone[-3]
first record: Phone[0] second record: Phone[1]

Common Code -1

{
"FirstName": "Fred",
"Surname": "Smith",
"Age": 28,
"Address": {
"Street": "Hursley Park",
"City": "Winchester",
"Postcode": "SO21 2JN"
},
"Phone": [
{
"type": "home",
"number": "0203 544 1234"
},
{
"type": "office",
"number": "01962 001234"
},
{
"type": "office",
"number": "01962 001235"
},
{
"type": "mobile",
"number": "077 7700 1234"
}
],
"Email": [
{
"type": "work",
"address": ["fred.smith@my-work.com", "fsmith@my-work.com"]
},
{
"type": "home",
"address": ["freddy@my-social.com", "frederic.smith@very-serious.com"]
}
],
"Other": {
"Over 18 ?": true,
"Misc": null,
"Alternative.Address": {
"Street": "Brick Lane",
"City": "London",
"Postcode": "E1 6RF"
}
}
}

Accessing properties that has spaces & symbols (example accessing property: "Over 18 ?")


1.https://try.jsonata.org/EYJ4BwXlT
2.https://try.jsonata.org/L-EC1IiCj

{
"payload": [
{
"Station ID": "3015432",
"Station Name": "Sg.Klang di Tmn Sri Muda 1",
"District": "Petaling",
"River Basin": "Sg.Klang",
"Last Update": "30/12/2017 01:30",
"River Level (m)": "1.13",
"Normal": "2.80",
"Alert": "4.40",
"Warning": "4.58",
"Danger": "5.00"
},
{
"Station ID": "3015490",
"Station Name": "Sg.Damansara di TTDI Jaya",
"District": "Petaling",
"River Basin": "Sg.Klang",
"Last Update": "30/12/2017 01:30",
"River Level (m)": "13.35",
"Normal": "3.50",
"Alert": "7.30",
"Warning": "7.80",
"Danger": "8.30"
}
]

}
Query:
 payload.(

{
"station": $.'Station ID',
"name": $.'Station Name',
"level": $.'River Level (m)',
"alert": $number($.'River Level (m)') > $number($.Alert) ? true : false
}
)

OP:
[
{
"station": "3015432",
"name": "Sg.Klang di Tmn Sri Muda 1",
"level": "1.13",
"alert": false
},
{
"station": "3015490",
"name": "Sg.Damansara di TTDI Jaya",
"level": "13.35",
"alert": true
}
]
ex:2:
{
"FirstName": "Fred",
"Surname": "Smith",
"Age": 28,
"Address": {
"Street": "Hursley Park",
"City": "Winchester",
"Postcode": "SO21 2JN"
},
"Phone": [
{
"type": "home",
"number": "0203 544 1234"
},
{
"type": "office",
"number": "01962 001234"
},
{
"type": "office",
"number": "01962 001235"
},
{
"type": "mobile",
"number": "077 7700 1234"
}
],
"Email": [
{
"type": "work",
"address": ["fred.smith@my-work.com", "fsmith@my-work.com"]
},
{
"type": "home",
"address": ["freddy@my-social.com", "frederic.smith@very-serious.com"]
}
],
"Other": {
"Over 18 ?": true,
"Misc": null,
"Alternative.Address": {
"Street": "Brick Lane",
"City": "London",
"Postcode": "E1 6RF"
}
}
}
Query : Other.`Over 18 ?` op: true

Merging Multiple Object properties to One object

 JSONATA:  Merging Array of objects properties to one object,
 This can be applicable only when all properties are unique in objects, 
 in below example we made Output properties has unique keys
https://try.jsonata.org/aPTBeXmoj

[
{
"TotalVehicles": 181,
"VehicleStatus": "UnReachable",
"count": 90
},
{
"TotalVehicles": 181,
"VehicleStatus": "Stopped",
"count": 87
},
{
"TotalVehicles": 181,
"VehicleStatus": "Running",
"count": 3
},
{
"TotalVehicles": 181,
"VehicleStatus": "Idle",
"count": 1
}
] CODE:$merge($${ VehicleStatus:{
(VehicleStatus)[0]&"count":count,
"TotalVehicles":TotalVehicles
}}.*) OP:{
"UnReachablecount": 90,
"TotalVehicles": 181,
"Stoppedcount": 87,
"Runningcount": 3,
"Idlecount": 1
}


calling outside object property or parent

 https://try.jsonata.org/dxPC5s-zM { "list1" : [ { "b" : 1 , "list2" : [ { ...