I have a table source
| id | json |
|---|---|
| 1 | {"some_id":{"some_text_1":"90","some_text_2":"50","some_text_3":"30"}} |
| 2 | {"some_id":{"some_text_3":"90","some_text_4":"50"}} |
I would like to get something like this
| text | value |
|---|---|
| "some_text_1" | 90 |
| "some_text_2" | 50 |
| "some_text_3" | 30 |
| "some_text_3" | 90 |
| "some_text_4" | 50 |
How can I achieve this? Thanks
You can use OPENJSON function in SQL Server. To transform the JSON data into the format you specified:
OPENJSON parses the JSON within the "some_id" property, creating a table-valued function that produces rows with the "key" and "value" columns.
To be able to use OPENJSON, ensure your database has a minimum compatibility level of 130.
If not you may see the following error:
Then you need to do the following:
1/ Check the compatibility of your database:
2/ If it is not >= 130 then:
Here is the result: