10

I need to get the following JSON object as an output:

"{"table" : "test", "column 1" : {"pos" : 1, "name" : "col1", "type" : "integer"}, 
"column 2" : {"pos" : 2, "name" : "col2", "type" : "date"}}"

When I use

build_json_object('table','test', 'column 1', build_json_object('pos',1,'name','col1','type','integer'), 'column 2', build_json_object('pos',2,'name','col2','type','date'));

it throws an error: ERROR: function build_json_object(unknown,integer, unknown,unknown, unknown,unknown) does not exist

when I use

build_json_object('table','test', 'column 1',('pos',1,'name','col1','type','integer'), 'column 2', ('pos',2,'name','col2','type','date'));

it just produces the wrong result:

"{"table" : "test", "column 1" : {"f1":"pos", "f2": 1, "f3":"name", "f4": "col1", "f5":"type", "f6": "integer"}, 
    "column 2" : {"f1":"pos", "f2": 2, "f3":"name", "f4": "col2", "f5":"type", "f6": "date"}}"

How to generate nested JSON object with build_json_object function?

2 Answers 2

15

A hint - build the expression step by step starting from the most nested objects. Use proper indents. Note that the expression is somehow similar to the result. (I've wrapped the main expression with jsonb_pretty() to get a nice output):

select 
    jsonb_pretty(
        json_build_object(
            'table', 'test',
            'column 1', json_build_object('pos', 1, 'name', 'col1', 'type', 'integer'),
            'column 2', json_build_object('pos', 2, 'name', 'col2', 'type', 'date')
        )::jsonb
    );


       jsonb_pretty        
---------------------------
 {                        +
     "table": "test",     +
     "column 1": {        +
         "pos": 1,        +
         "name": "col1",  +
         "type": "integer"+
     },                   +
     "column 2": {        +
         "pos": 2,        +
         "name": "col2",  +
         "type": "date"   +
     }                    +
 }
(1 row)
Sign up to request clarification or add additional context in comments.

2 Comments

Great, it works! Thanks a lot. Looks like changing build_json_object to json_build_object really matters.
Only now I've noticed that your first attempt was ok, with the exception of the function name. This is because in principle I don't read texts that don't fit on the page ;) Use indents!!
1

You can nest json objects in constructs via SQL like this

SELECT jsonb_pretty(
    data || json_build_object(
        'field', sub.field, 
        'construct', json_build_object(
            'nested', json_build_object(
                'key', 'value', 
                'moved_data', data -> 'nuke_me' -> moved_data
            ),
            'other_nested_key', json_build_object(
                '0', 2136 
            )                       

        )
    ) :: JSONB 
    #- '{nuke_me,moved_data}' 
    #- '{nuke_all_of_me}'
)
AS data FROM table AS sub
ORDER BY "created_at" DESC

https://www.postgresql.org/docs/9.6/static/functions-json.html

Comments

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.