A list of example transformations taken from other sites around the web.
Input:
{
"menu": {
"popup": {
"menuitem": [
{
"value": "Open",
"onclick": "OpenDoc()"
},
{
"value": "Close",
"onclick": "CloseDoc()"
}
]
}
}
}
The desired output is:
{
"result" : {
"Open" : "OpenDoc()",
"Close" : "CloseDoc()"
}
}
This can be done two different ways, depending on whether one wants to hard-wire the output, or make it dynamic.
Hard-wired:
{
"result" : {
"Open" : .menu.popup.menuitem[0].onclick,
"Close" : .menu.popup.menuitem[1].onclick
}
}
Or, we can turn each value/onclick
object into a key/value pair in
the output object dynamically, like this:
{
"result" : {for (.menu.popup.menuitem)
.value : .onclick
}
}
Taken from JUST.
Input:
[
{
"a": "123",
"b": ""
},
{
"a": "",
"b": "456"
},
{
"a": "789",
"b": "789"
},
{
"a": "10",
"b": "11"
}
]
Output:
[
{
"x": "123"
},
{
"x": "456"
},
{
"x": "789"
},
{
"x": "Error!"
}
]
[for (.) {
"x" : if (.a and .b) (
if (.a == .b)
.a
else
"Error!" // or you can fail with the error function
) else if (.a)
.a
else
.b
}]
Taken from JOLT.
{ "state" : [for (.state) . | { "locatedIn" : .location, "cities" : [for (.cities) { "cityname" : .name, "citypopulation" : .pop }], * : . }],
- : . }
If you want something more hardcore, look at solving the N-queens problem in JSLT