jsonspecificationsjolt

JOLT SPEC for Filtering on two Conditions


I have trouble finding the right Jolt Spec.

This is my Input JSON:

{
  "value": [
    {
      "identifier": "plm",
      "value": "3",
      "value_unit_key": "",
      "language": ""
    },
    {
      "identifier": "mytest",
      "value": "whatever",
      "value_unit_key": "",
      "language": ""
    },
   {
      "identifier": "new ",
      "value": "anything",
      "value_unit_key": "kg",
      "language": ""
    }
  ]
}  

This is my desired Output JSON In every object where the identifier has the value 'plm' AND the value is '3', the value should be changed to 'active'.

All other information should remain unchanged.

{
  "value": [
    {
      "identifier": "plm",
      "value": "active",
      "value_unit_key": "",
      "language": ""
    },
    {
      "identifier": "mytest",
      "value": "whatever",
      "value_unit_key": "",
      "language": ""
    },
   {
      "identifier": "new ",
      "value": "anything",
      "value_unit_key": "kg",
      "language": ""
    }
  ]
}  

Here's my approach, but it doesn't yield the desired result.

[
  {
    "operation": "shift",
    "spec": {
      "value": {
        "*": {
          "value_unit_key": "value[#2].value_unit_key",
          "language": "value[#2].language",
          "identifier": {
            "plm": {
              "@(2,value)": {
                "3": {
                  "#active": "value[#1].value",
                  "#plm": "value[#1].identifier"
                }
              }
            },
            "*": {
              "identifier": "value[#2].identifier",
              "value": "value[#2].value"
            }
          }
        }
      }
    }
  }
]

It does not return the original identifier and the original value if the AND-Condition is not fullfilled. The output is:

{
  "value": [
    {
      "identifier": "plm",
      "language": "",
      "value": "active",
      "value_unit_key": ""
    },
    {
      "language": "",
      "value_unit_key": ""
    },
    {
      "language": "",
      "value_unit_key": "kg"
    }
  ]
}

Any help is greatly appreciated.

Thanks in advance

Kind Regards Katja


Solution

  • You can convert the current shift transformation to this one :

    [
      {
        "operation": "shift",
        "spec": {
          "*": { // the representative for the "value" array
            "*": { // for the indexes of the array
              "identifier": { // manage "identifier" and "value" within this node
                "plm": {
                  "@2,value": {
                    "3": {
                      "@(4,&3)": "&6[&5].&4", //for the "identifier" attribute
                      "#active": "&6[&5].value" //&6 reprsents the key "value"
                    },
                    "*": { // for other values of "value"
                      "@(4,&3)": "&6[&5].&4",
                      "@4,value": "&6[&5].value" // go up 4 levels to reach the
                        //level of the "value" attribute
                    }
                  }
                },
                "*": { // else case for "identifier" values
                  "@(2,&1)": "&4[&3].&2", // reduce 2 levels for the each identifier 
                  "@2,value": "&4[&3].value"
                }
              },
              "value": { "": "" }, //get rid of the current "value" attribute
              "*": "&2[&1].&" //all other attributes
            }
          }
        }
      }
    ]
    

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

    enter image description here