21.6. Checking a JSON Value (json_contains) #

Required privileges: Postgres Pro AXE administrator only. For a full list of stored procedures and privileges, refer to Section 12.1.

Execute the following command on the Postgres Pro AXE server:

  SELECT json_contains(JSON, value) ON CONFLICT DO NOTHING;

Where:

  • JSON: The JSON data where the search must be performed.

    You must specify a JSON string literal or a column with JSON data.

  • value: The JSON value that must be searched.

    You must specify a JSON string literal, a numeric value, or a column with JSON data. String values must be wrapped in double quotes (e.g., "duck").

Postgres Pro AXE returns true if value is contained in JSON and false otherwise.

Example 21.6. Executing the json_contains Stored Procedure

JSON data in the j column of the example table:

  {
  "family": "anatidae",
  "species": ["duck", "goose", "swan", null]
  }

Checking if a value is contained in the JSON data:

  SELECT json_contains(j, '"duck"') FROM example;
  true
  SELECT json_contains(j, '"eagle"') FROM example;
  false

Checking if a JSON object is contained in the JSON data:

  SELECT json_contains(j, '{"family": "anatidae"}') FROM example;
  true