Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying json key name in SQL Server

Given json like this...

{"setting1":"A","setting2":"B","setting3":"C"}

I would like to see results like...

+----------+-------+
|   name   | value |
+----------+-------+
| setting1 | A     |
| setting2 | B     |
| setting3 | C     |
+----------+-------+

My struggle is I'm trying to find out how to extract the key's name (i.e., "setting1", "setting2", "setting3", etc.)

I could do something like the following query, but I don't know how many settings there will be and what their names will be, so I'd like something more dynamic.

SELECT
    B.name,
    B.value
FROM OPENJSON(@json) WITH
    (
        setting1 varchar(50) '$.setting1',
        setting2 varchar(50) '$.setting2',
        setting3 varchar(50) '$.setting3'
    ) A
CROSS APPLY
    (
        VALUES
            ('setting1', A.setting1),
            ('setting2', A.setting2),
            ('setting3', A.setting3)
    ) B (name, value)

With XML, I could do something simple like this:

DECLARE @xml XML = '<settings><setting1>A</setting1><setting2>B</setting2><setting3>C</setting3></settings>'
SELECT
    A.setting.value('local-name(.)', 'VARCHAR(50)') name,
    A.setting.value('.', 'VARCHAR(50)') value
FROM @xml.nodes('settings/*') A (setting)

Any way to do something similar with SQL Server's json functionality?

like image 796
Eilert Hjelmeseth Avatar asked May 29 '17 01:05

Eilert Hjelmeseth


People also ask

How do I use JSON in SQL query?

You can use values from JSON text in any part of a Transact-SQL query (including WHERE, ORDER BY, or GROUP BY clauses, window aggregates, and so on). JSON functions use JavaScript-like syntax for referencing values inside JSON text.

What is the difference between JSON_value and JSON_query in SQL Server?

JSON_VALUE (Transact-SQL) extracts a scalar value from a JSON string. JSON_QUERY (Transact-SQL) extracts an object or an array from a JSON string. JSON_MODIFY (Transact-SQL) changes a value in a JSON string. In the following example, the query uses both relational and JSON data (stored in a column named jsonCol) from a table:

Can I modify the key part of a JSON document in SQL?

If you’ve been using the JSON_MODIFY () function to modify JSON documents in SQL Server, you might be used to modifying the value part of a key/value property. But did you know that you can also modify the key part?

What are the key JSON capabilities of SQL Server?

Key JSON capabilities of SQL Server and SQL Database 1 Extract values from JSON text and use them in queries. ISJSON (Transact-SQL) tests whether a string contains valid JSON. ... 2 Change JSON values. ... 3 Convert JSON collections to a rowset. ... 4 Convert SQL Server data to JSON or export JSON. ...


1 Answers

Aaron Bertrand has written about json key value in Advanced JSON Techniques

SELECT x.[Key], x.[Value] 
FROM OPENJSON(@Json, '$') AS x;

Return

Key         Value
------------------
setting1    A
setting2    B
setting3    C
like image 192
Jonasr Avatar answered Sep 28 '22 13:09

Jonasr