From a486ee221469037b08d3663f1ec142a905406f8b Mon Sep 17 00:00:00 2001 From: Dian M Fay Date: Wed, 20 Jan 2021 23:36:34 -0500 Subject: [PATCH] more jsonb subscripting documentation edits --- doc/src/sgml/json.sgml | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/doc/src/sgml/json.sgml b/doc/src/sgml/json.sgml index deeb9e66e0..e16dd6973d 100644 --- a/doc/src/sgml/json.sgml +++ b/doc/src/sgml/json.sgml @@ -616,16 +616,17 @@ SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc @> '{"tags": ["qu UPDATE statements may use subscripting in the - SET clause to modify jsonb values. Object - values being traversed must exist as specified by the subscript path. For - instance, the path val['a']['b']['c'] assumes that - val, val['a'], and val['a']['b'] - are all objects in every record being updated (val['a']['b'] - may or may not contain a field named c, as long as it's an - object). If any individual val, val['a'], - or val['a']['b'] is a non-object such as a string, a number, - or NULL, an error is raised even if other values do conform. - Array values are not subject to this restriction, as detailed below. + SET clause to modify jsonb values. Subscript + paths must be traversible for all affected values insofar as they exist. For + instance, the path val['a']['b']['c'] can be traversed all + the way to c if every val, + val['a'], and val['a']['b'] is an + object. If any val['a'] or val['a']['b'] + is not defined, it will be created as an empty object and filled as + necessary. However, if any val itself or one of the + intermediary values is defined as a non-object such as a string, number, or + jsonb null, traversal cannot proceed so + an error is raised and the transaction aborted. @@ -658,8 +659,9 @@ SELECT * FROM table_name WHERE jsonb_field['key'] = '"value"'; jsonb assignment via subscripting handles a few edge cases differently from jsonb_set. When a source jsonb - is NULL, assignment via subscripting will proceed as if - it was an empty JSON object: + value is NULL, assignment via subscripting will proceed + as if it was an empty JSON value of the type (object or array) implied by the + subscript key: -- Where jsonb_field was NULL, it is now {"a": 1} @@ -680,17 +682,19 @@ UPDATE table_name SET jsonb_field[2] = '2'; A jsonb value will accept assignments to nonexistent subscript - paths as long as the last existing path key is an object or an array. Since - the final subscript is not traversed, it may be an object key. Nested arrays - will be created and NULL-padded according to the path until - the value can be placed appropriately. + paths as long as the last existing element to be traversed is an object or + array, as implied by the corresponding subscript (the element indicated by + the last subscript in the path is not traversed and may be anything). Nested + array and object structures will be created, and in the former case + null-padded, as specified by the subscript path until the + assigned value can be placed. -- Where jsonb_field was {}, it is now {'a': [{'b': 1}]} UPDATE table_name SET jsonb_field['a'][0]['b'] = '1'; --- Where jsonb_field was [], it is now [{'a': 1}] -UPDATE table_name SET jsonb_field[0]['a'] = '1'; +-- Where jsonb_field was [], it is now [null, {'a': 1}] +UPDATE table_name SET jsonb_field[1]['a'] = '1'; -- 2.30.0