Search code examples
jsonconditional-statementsjolt

Jolt conditional match field in array and display output


I can't figure out how to do condition matches based on fields in the array in Jolt.

eg I have one such the below input I want to print the value of the restaurant which matches the locale

{
  "locale": "en_US",
  "restaurants": [
    {
      "restLocale": "en_US",
      "value": "english"
    },
    {
      "restLocale": "pr_PT",
      "value": "portugese"
    }
  ]
}

here is the output I want

Output:

{
  "value": "english"
}

I tried

matching with the first value but didn't work


Solution

  • You might set object keys to the "restLocale" attibutes' values, and match object keys with the value of the "locale" attribute such as

    [
      { // set object keys to the restLocale attibutes' values 
        "operation": "shift",
        "spec": {
          "*": "&",
          "restaurants": {
            "*": {
              "*": "@1,restLocale.&"
            }
          }
        }
      },
      { // match object keys with the value of the locale attribute
        "operation": "shift",
        "spec": {
          "locale": {
            "*": {
              "@2,&": ""
            }
          }
        }
      },
      { // pick the value attribute only
        "operation": "shift",
        "spec": {
          "v*": "&"
        }
      }
    ]
    

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

    enter image description here