Search code examples
db2

DB2 Build Json document with about two columns


I want to generate a json file based on the result of this query. But as a result I will get exactly 10 rows as CLOB. But I want only one.

select json_object (
'ID' value id, 'NAME' value name) 
FROM OBJECTS
FETCH FIRST 10 ROWS ONLY

It should has an output like this

{
objects: 
{id:1, name: a
},
{id:2, name: b
}
}

Solution

  • Just some guessing:

    values (json_array(select json_object ('ID' value id, 'NAME' value name)
                       from objects
                       fetch first 10 rows only
                      )
           )
    

    AFAIK, you need an array to store a collection of things:

    values json_object( 'objects' value (json_array
           (select json_object ('ID' value id, 'NAME' value name)
            from objects
            fetch first 10 rows only
           )
       ));