Handle partial Stringified Partial Key-value JSON VARIANT on SQL

I have a variant object like the below:

  {
  "build_num": "111",
  "city_name": "Paris",    
  "rawData": "\"sku\":\"AAA\",\"price\":19.98,\"currency\":\"USD\",\"quantity\":1,\"size\":\"\"}",
  "country": "France"
}

So you can see that parts of it are regular key-value pairs like build num and city name, but then we have raw data which its value is a stringified version of a JSON

I would like to create a variant from this that will look like:

       {
          "build_num": "111",
          "city_name": "Paris",    
          "rawData": {
    
              "sku":"AAA",
              "price":19.98,
              "currency":"USD",
               "quantity":1}

         "country": "France"
    }

AND I would like to do this all in SQL (Snowflake) - is that possible?


so to poke the "data" into I have used a CTE, and escaped the sub json, so it is in the DB as you describe. I also had to add the missing start of object token {, to make rawData valid.

WITH data AS (
    SELECT parse_json(json) as json
    FROM VALUES 
        ('{"build_num": "111","city_name": "Paris","country": "France","rawData": "{\\"sku\\":\\"AAA\\",\\"price\\":19.98,\\"currency\\":\\"USD\\",\\"quantity\\":1,\\"size\\":\\"\\"}"}')
    v( json)
)
SELECT 
    json,
    json:rawData as raw_data,
    parse_json(raw_data) as sub_json,
    OBJECT_INSERT(json, 'rawData', sub_json, true) as all_json
FROM data;

so this show step by step transforming the data, parsing it via PARSE_JSON, and re inject via OBJECT_INSERT the result into the original object.

WITH data AS (
    SELECT parse_json(json) as json
    FROM VALUES 
        ('{"build_num": "111","city_name": "Paris","country": "France","rawData": "{\\"sku\\":\\"AAA\\",\\"price\\":19.98,\\"currency\\":\\"USD\\",\\"quantity\\":1,\\"size\\":\\"\\"}"}')
    v( json)
)
SELECT 
    OBJECT_INSERT(json, 'rawData', parse_json(json:rawData), true) as all_json
FROM data;