0

I have a table like this:

TABLE myTable(identifier nvarchar(10) primary key, jsonfield nvarchar(max))

In the table there is 1 record like this:

Example of data within the table

Can you help to findout a single query to output something like this within a view? It is SQL Server. Example of output from the query

Zhorov
  • 28,486
  • 6
  • 27
  • 52

1 Answers1

3

You need to use OPENJSON() with explicit schema (the WITH clause with columns definitions) to parse the Cities JSON array stored in the jsonfield column:

SELECT t.identifier, j.Name, j.Population 
FROM myTable t
CROSS APPLY OPENJSON(t.jsonfield, '$.Cities') WITH (
   Name varchar(50) '$.Name',
   Population int '$.Population'
) j
Zhorov
  • 28,486
  • 6
  • 27
  • 52