bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.56k stars 329 forks source link

ifElse multiple conditions? #1250

Closed hartjo closed 6 months ago

hartjo commented 7 months ago

Source JSON

{ "carValidation": { "pressureValue": { "type": 0, "unit": 0, "value": "00H" } } }

check if type and unit value and change the pressureValue.value base on the condition

example: pressureValue.type == 0 and pressureValue.unit == 0 then pressureValue.value = 60;

can you provide example for this scenario thank you!

barbaros74 commented 7 months ago

You can use the following transformation :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "type": {
            "@": "&3.&2.&1", // returns the "type" attribute
            "0": {
              "@2,unit": {
                "0": {
                  "#60": "&6.&5.value" //return the respective keys by expressing their levels within the hierarchy through integer suffixes 1,2,3 of identifiers &1,&2,&3
                },
                "*": {
                  "@4,value": "&6.&5.value" //return the respective keys by expressing their levels within the hierarchy through integer suffixes 6,5 of identifiers &6,&5
                }
              }
            },
            "*": {
              "@2,value": "&4.&3.value"
            }
          },
          "value": { "": "" }, // get rid of the original "value" attribute
          "*": "&2.&1.&" // return all remaining attributes
        }
      }
    }
  },
  { //get rid of the quotes for the stringified integers 
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "value": "=toInteger"
        }
      }
    }
  }
]
barbaros74 commented 6 months ago

You can follow me on SO , have a nice day!