0

I have data that looks like

{
    "Attributes": [
        {
            "values": [
                {
                    "value": "20003"
                },
                {
                    "value": "30075"
                },
                {
                    "value": "40060"
                }
            ],
            "name": "price"
        }
    ],
    "attr2" : "val"
}

The output I want is concat all the values in the nested json array

price, "20003, 30075, 40060"

I tried some queries but failed to get the correct output.

1 Answer 1

1

You can use JSON_EXTRACT_ARRAY and ARRAY_TO_STRING:

WITH test_json AS (
  SELECT
    '''{
        "Attributes": [
            {
                "values": [
                    {
                        "value": "20003"
                    },
                    {
                        "value": "30075"
                    },
                    {
                        "value": "40060"
                    }
                ],
                "name": "price"
            }
        ],
        "attr2" : "val"
    }''' AS json_string
),
values_concatenated AS (
  SELECT ARRAY_TO_STRING(
    ARRAY(
      SELECT JSON_VALUE(json_values, '$.value')
      FROM UNNEST((SELECT JSON_EXTRACT_ARRAY(json_string, '$.Attributes[0].values') AS json_values FROM test_json)) as json_values
    ),
    ', '
  ) as values
)
SELECT
  (select json_value(json_string, '$.Attributes[0].name') from test_json),
  (select values from values_concatenated)

enter image description here

Sign up to request clarification or add additional context in comments.

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.