jsonPathAdd

Description: The jsonPathAdd function adds a scalar value to an array at the specified JsonPath on a Subject JSON and returns string form of the updated JSON. If the expression target element is a non-array, an empty string is returned and an exception is logged indicating the error. If the expression target element path is not in the JSON, the operation returns the original JSON without any modifications.

Subject Type: String

Arguments:

  • jsonPath : the JSON path expression to set value on the Subject.

  • value : the value expression to be added to the array at the specified path on Subject.

Return Type: String

Examples: If the "myJson" attribute is


{
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "voter" : true,
     "height" : 6.1,
     "address" : {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021-3100"
     },
     "phoneNumbers": [
         {
             "type": "home",
             "number": "212 555-1234"
         },
         {
             "type": "office",
             "number": "646 555-4567"
         }
     ],
     "nicknames" : []
 }

Expression

Value

${myJson:jsonPathAdd('$.nicknames', 'Jimmy')}

{"firstName":"James", lastName":"Smith", "age":25, "voter":true, "height":6.1, "address":{"streetAddress":"21 2nd Street", "city":"New York", "state":"NY", "postalCode":"10021-3100"}, "phoneNumbers":[{"type":"home", "number":"212 555-1234"}, {"type":"office", "number":"646 555-4567"}],"nicknames":["Jimmy"]}

${myJson:jsonPathAdd('$.missingpath', 'Jimmy')}

Returns original JSON document with no modifications

${myJson:jsonPathAdd('$.firstName', 'Jimmy')}

empty