jsonjolt

Jolt transform JSON to different JSON structure


Having this structure as response from webservice

{
  "software": [
    {
      "service": "someService",
      "versions": [
        "24.32.1.57-docker",
        "24.32.1.56-docker"
      ]
    }
  ]
}

how to transform it using Jolt spec to following structure required by teamcity webparameters plugin

{
  "options": [
    {
      "key": "24.32.1.57-docker",
      "value": "24.32.1.57-docker",
      "enabled": true,
      "default": "as default first entry in list"
    },
    {
      "key": "24.32.1.56-docker",
      "value": "24.32.1.56-docker",
      "enabled": true
    }
  ]
}

Solution

  • You can use the following transformation :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "versions": {
                "0": { //filter only for the first object as component of the options array 
                  "*": {
                    "$": ["options[&2].key", "options[&2].value"],//$ replicates the keys from one uper level 
                    "#true": "options[&2].enabled",//hardcode by using # sign
                    "#as default first entry in list": "options[&2].default"
                  }
                },
                "*": {
                  "*": {
                    "$": ["options[&2].key", "options[&2].value"],
                    "#true": "options[&2].enabled"
                  }
                }
              }
            }
          }
        }
      },
      {//boolean conversion occurs for the litrals "true" or "false" 
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "*": "=toBoolean"
            }
          }
        }
      }
    ]
    

    the demo on the site https://jolt-demo.appspot.com/ is :

    enter image description here