calling outside object property or parent

 https://try.jsonata.org/dxPC5s-zM

{
"list1": [
{
"b": 1,
"list2": [
{
"a": 1
},
{
"a": 2
},
{
"a": 3
}
]
},
{
"b": 2,
"list2": [
{
"a": 1
},
{
"a": 2
},
{
"a": 3
}
]
},
{
"b": 3,
"list2": [
{
"a": 1
},
{
"a": 2
},
{
"a": 3
}
]
}
]
}



{
"result": $$.list1.(
$b := b; /* temp var. */
{
"v": list2[a=$b].{
"v":a
}
})
}

Jsonata Date to milliseconds convertion


https://try.jsonata.org/r0q7GnSOh



lstData.strValue.$toMillis($, '[Y]-[M]-[D]')
{
"lstData": [
{
"strFieldName":"ActivityDate",
"strValue": "2020-06-28",
"dateTimeStamp": 1594857600000
},
{
"strFieldName":"ActivityDate",
"strValue": "2020-07-10",
"dateTimeStamp": 1595116800000
},
{
"strFieldName":"ActivityDate",
"strValue": "2020-07-25",
"dateTimeStamp": 1595289600000
}
]
}

retreiving data with a name that contains spaces or special charecters with Jsonata

 

{
"normal": 1,
"has some spaces": 2,
"@": 3
} Query: $."@" 2. `@`

delete the first element from an array while assuring the result is still an array in jsonata?

 


https://try.jsonata.org/BaO2T7_9i




retriving data greater than particular vvalue



https://try.jsonata.org/0vfDXqv4m


 

How to group the JSON object by a key using JSONata

 I want to use JSONata to do the grouping of json objects in an array based on a key within the JSON object.

For example, I want to group the cars based on their names for the JSON example given below.

[
  {
    "car" : "audi",
    "color" : "blue",
    "reg" : 133434
  },
   {
    "car" : "benz",
    "color" : "red",
    "reg" : 134444
  },
   {
    "car" : "audi",
    "color" : "red",
    "reg" : 134884
  }
]

Expected output

{
  "audi" : [
    {
    "car" : "audi",
    "color" : "blue",
    "reg" : 133434
    },
    {
    "car" : "audi",
    "color" : "red",
    "reg" : 134884
    }
  ],
  "benz" : [
    {
    "car" : "benz",
    "color" : "red",
    "reg" : 134444
    }
   ]
}

$${car: [$.{"car": car, "color": color, "reg": reg}]}



Avg KPI: To get average of array object property

 {"payload":[

{"name": "product1", "price": 100},
{"name": "product2", "price": 25},
{"name": "product1", "price": 50},
{"name": "product2", "price": 75},
{"name": "product3", "price": 25}
]}
payload{name:$average(price)} ~> $each(function($v, $n) {{
'name': $n,
'avg': $v
}}) ^(avg) //this is for ascending/descending order


Jsonata execution through library

 https://stackoverflow.com/questions/67056785/why-does-map-not-output-an-array-in-case-the-input-array-is-one-in-length


var json = { "projectIds": [ 1 ] }
var result = jsonata('projectIds[].{"projectId": $}').evaluate(json);
console.log(JSON.stringify(result))
<script src="https://cdn.jsdelivr.net/npm/jsonata/jsonata.min.js"></script>



Extract fields by partial name( property value data extraction by calling short name)

 I have a json with attribute names that start with some prefixes I need to ignore, and end with the actual field name:

{
    "context": {
        "values": {
            "group1:0:foo": "08119037",
            "group1:0:checkbox": [
                "2",
                "3"
            ]
        }
    }
}

I can extract the exact field group1:0:foo, but not "the field that ends with :foo":

{
    "foo": context.values.`group1:0:foo`
}

https://try.jsonata.org/9vruWw6HR



{
"context": {
"values": {
"group1:0:foo": "08119037"
}
}
}
method1:
$spread(context.values)[$match($keys($)[0], /.*:foo/)] {
$keys($)[0].$split(":")[2]: $.*
}
method2:
{
"foo": $lookup(context.values, context.values.$keys()[$contains($, 'foo')])
}

Use the Transform operator to remove one or more key/value pairs from an Object?

 Given this source...

{
  "Price": 34.45,
  "Product Name": "Bowler Hat",
  "ProductID": 858383,
  "Quantity": 2,
  "SKU": "0406654608"
}

I would like to reduce it to...

{
  "Product Name": "Bowler Hat",
  "ProductID": 858383,
  "SKU": "0406654608"
}
https://try.jsonata.org/qUwOtT-pt




Use JSONata to alpha sort keys in a JSON object

 

Unsorted:

{"B":2,"A":1,"C":3}

Sorted:

{"A":1,"B":2,"C":3}

https://try.jsonata.org/RbJqQxO0i


(

$sortObjectAlphabetically := function($obj){
(
$keys := [$keys($obj)];
$keys := $keys^(<$);
$merge([$map($keys,function($e){
(
{
$e : $lookup($obj,$e)
}
)
})]);
)
};

$sortObjectAlphabetically(str2)
replace $sortObjectAlphabetically($)
)

How to substitute . with _ in key names? (replacing common propertynames )

 https://try.jsonata.org/uZgm69Bfy
https://try.jsonata.org/WsfsFGhjE


https://try.jsonata.org/r2GrHYYou
differnec between 2 menthods

(
$dotsToUnderscores := function($obj) {
$each($obj, function($v, $k) {
{ $replace($k, '.', '_'): $type($v) = 'object' ? $dotsToUnderscores($v) : $v}
}) ~> $merge()
};
$dotsToUnderscores($)
)

(
$dotsToUnderscores :=
$each(?, function($v, $k) {
{ $replace($k, '.', '_'): $type($v) = 'object' ? $dotsToUnderscores($v) : $v}
}) ~> $merge;
$dotsToUnderscores($)
)










Match keys with sibling object JSONATA

 https://try.jsonata.org/--2aRZvSL

merging 2 arrays properties based on key match 



How to convert an array of string to an array of objects using jsonata?

 https://try.jsonata.org/Q5Tt46GBT
https://try.jsonata.org/0L_oYffzT

{
"projectIds": [
1,2,3
]
}
projectIds[].{"projectId": $}




calling outside object property or parent

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