Converting table with Varchar columns to custom JSON in Snowflake

I have a table that has the following data

START_DATE NAME ID
01/18/2022 JOHN 10
01/19/2022 ADAM 20

I am trying to convert this to JSON in a custom format like below -

{
 "labels":{
           "name":"JOHN",
           "id":[10]
          }
 "values":{
           "startDate":"01/18/2022"
          }
}

PARSE_JSON way of

SELECT parse_json('{"values": {startDate: A.STARTDATE}}')
FROM TABLE_A A;

resulted in error

Error parsing JSON: unknown keyword "A", pos 25

OBJECT_CONSTRUCT results in converting column name as key and column value as value.

Please advise how to have custom field names in JSON conversion in Snowflake.


Solution 1:

Renamed objects names as per data given and changed Id to array:

create table test1 values(START_DATE date, NAME string,ID number);
insert into test1(START_DATE, NAME,ID ) values('01/18/2022','JOHN', 10);
insert into test1(START_DATE, NAME,ID ) values('01/19/2022','ADAM', 20);
    Select
        OBJECT_CONSTRUCT('ID',id::array,'NAME',name) as label_obj,
        OBJECT_CONSTRUCT(
            'start_date',
            START_DATE::string) as start_dt_obj,
            object_insert(object_construct('labels', label_obj), 'values', start_dt_obj) as final_json
            from
                Test1;