Skip to content

Latest commit

 

History

History
149 lines (122 loc) · 2.05 KB

README.md

File metadata and controls

149 lines (122 loc) · 2.05 KB

JSLT example transformations

A list of example transformations taken from other sites around the web.

Collapsing repeated objects

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.

Pick one of two values, or complain

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.

Another one

https://stackoverflow.com/questions/39585360/rename-fields-in-nested-arrays-using-jolt-transformation?rq=1

{ "state" : [for (.state) . | { "locatedIn" : .location, "cities" : [for (.cities) { "cityname" : .name, "citypopulation" : .pop }], * : . }],

  • : . }

N-queens

If you want something more hardcore, look at solving the N-queens problem in JSLT