I would like to use a JSON path like $[*].name
or $[..].name
on a JSON like this one:
[
{"id": 1, "name": "John"},
{"id": 2, "name": "Mary"},
{"id": 3, "name": "Peter"}
]
To get as result:
["John", "Mary", "Peter"]
But when I try this on SQL Server2019 using:
SELECT JSON_VALUE(json_data, '$[*].name')
FROM users
I got this error:
JSON path is not properly formatted. Unexpected character '*' is found at position 2.
So how can I get the expected result using a jsonpath
compatible with SQL Server?
2条答案
按热度按时间hgncfbus1#
Something like this.
SQL
Output
quhf5bfb2#
A somewhat clearer and more accurate method
OPENJSON
to immediately parse out properties into columns.STRING_ESCAPE
rather thanQUOTENAME
for correct quoting.db<>fiddle