4

In documentation some of the postgresql json functions uses a json path attribute. For exemple the jsonb_set function :

jsonb_set(target jsonb, path text[], new_value jsonb[, create_missing boolean])

I can't find any of the specifications of this type of attribute.

Can it be used for example to retrieve an array element based on it's attribute's value ?

3
  • hey there! I didn't quite get your point. Could you be a bit more specific on specifications of this type of attribute? Just to avoid that we understand your question wrong. Commented Apr 3, 2018 at 12:59
  • Ok, to be more specific, what is the syntax of the 'path' variable. Commented Apr 3, 2018 at 13:08
  • I'd argue it is a json representation to the path of your node. Commented Apr 3, 2018 at 13:13

1 Answer 1

4

The path is akin to a path on a filesystem: each value drills further down the leaves of the tree (in the order you specified). Once you get a particular JSONB value from extracting it via a path, you can chain other JSONB operations if needed. Using functions/operators with JSONB paths is mostly useful for when there are nested JSONB objects, but can also handle simple JSONB arrays too.

For example:

SELECT '{"a": 42, "b": {"c": [1, 2, 3]}}'::JSONB #> '{b, c}' -> 1;

...should return 2.

The path {b, c} first gets b's value, which is {"c": [1, 2, 3]}. Next, it drills down to get c's value, which is [1, 2, 3]. Then the -> operation is chained onto that, which gets the value at the specified index of that array (using base-zero notation, so that 0 is the first element, 1 is the second, etc.). If you use -> then it will return a value with a JSONB data type, whereas ->> will return a value with a TEXT data type.

But you could have also written it like this:

SELECT '{"a": 42, "b": {"c": [1, 2, 3]}}'::JSONB #> '{b, c, 1}';

...and simply included both keys and array indexes in the same path.

For arrays, the following two should be equivalent, except the first uses a path, and the second expects an array and gets the value at the specified index:

SELECT '[1, 2, 3]'::JSONB #> '{1}';

SELECT '[1, 2, 3]'::JSONB -> 1;

Notice a path must always be in JSON array syntax, where each successive value is the next leaf in the tree you want to drill down to. You supply keys if it is a JSONB object, and indexes if it is a JSONB array. If these were file paths, the JSONB keys are like folders, and array indexes are like files.

Sign up to request clarification or add additional context in comments.

6 Comments

Is this "JSON array syntax" documented anywhere ? I can't find it out.
And so as the definition you gave, it's not possible to search for an element in an array by the value of one of it's attribute. Am I correct ?
JSON is not defined by PostgreSQL. It's a web standard: json.org w3schools.com/js/js_json_arrays.asp
In terms of using the "value of one of it's attribute" to search for a JSON array element, then no PostgreSQL doesn't yet support the full jsonpath syntax. You'll currently have to use a workaround like a CTE. Some details about the issue are here: gmile.me/jsonpath
Yeah, I wish the JSON path syntax was more readily available as documentation so its usage would be more clear. I've only used it casually with simple cases. I found out that the official source for the JSON path syntax is only found in the SQL:2016 standard (i.e. non-free). This article explains more details: modern-sql.com/blog/2017-06/whats-new-in-sql-2016
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.