0
DECLARE @json NVARCHAR(MAX)

SET @json='{"name":"John","surname":"Doe","age":45,"skills":["SQL","C#","MVC"]}';

SELECT *
FROM OPENJSON(@json);

This gives you key, value, and type as Columns.

I want to have Name, surname and age as columns. and the row would be John, Doe, 45.

How do I flip or transpose the columns and rows? I've tried pivot but can't get it to work.

Dale K
  • 25,246
  • 15
  • 42
  • 71

1 Answers1

2

You would use the with clause of openjson():

select * 
from openjson(@json) with (
    name nvarchar(max), 
    surname nvarchar(max), 
    age int
);

Demo on DB Fiddle:

name | surname | age
:--- | :------ | --:
John | Doe     |  45
GMB
  • 216,147
  • 25
  • 84
  • 135