JSON_COUNT_MATCHES

Use the JSON_COUNT_MATCHES script function to return the number of matches of a JSONPath expression in a JSON document.

Syntax

JSON_COUNT_MATCHES (JSONJSONPath)

Parameters

Parameter Description Format
JSON A string containing a valid JSON document. String
JSONPath A valid JSONPath expression. String

Runtime Errors

JSON_COUNT_MATCHES will fail with a runtime error on the following conditions:

  • 45334: If your JSON document is invalid.
  • 45342: Syntax Error in JSONPath expression.

Examples

The following example uses the script function to find the number of books in your JSON shopping basket:

:SET &JSON# = '[{"item":"book", "price": 12.99, "title": "Learn Cobol in 24 hrs"},{"item":"CD", "price": 6.99, "title": "Coder Muzak"}, {"item":"book", "price": 7.99, "title": "Coder Muzak"}, {"item":"book", "price": 7.99, "title": "Java is also an island"}]'

:SET &BOOKCOUNT# = JSON_COUNT_MATCHES(&JSON#,'$..[?(@.item=="book")]')

:P 'You have &BOOKCOUNT# books in your basket'

Activation report result:

U00020408 You have 0000000000000002 books in your basket

This example uses the script function to check whether your server returned an error when validating a form via REST API:

:SET &JSON# = '[ ]'

! Uncomment the following line to simulate the error case

:SET &JSON# = '[{"error": "isRequired", "field": "username"},{"error": "outOfRange", "field": "age"}]'

:SET &ERRORCOUNT# = JSON_COUNT_MATCHES(&JSON#,'$..error')

:IF &ERRORCOUNT# <> 0

  :P 'You have &ERRORCOUNT# mistakes in your form'

:ELSE IF

  :P 'Validation successful'

:ENDIF

Activation report result:

U00020408 You have 0000000000000002 mistakes in your form

See also: