bazaarvoice / jolt

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

ifElse multiple conditions? #1250

Closed hartjo closed 2 months ago

hartjo commented 2 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 2 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 1 month ago

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