I am trying grab the JSON object head for each currency and insert it into a SQL Server table.
This is the code I am using, however I get null values:
DECLARE @json NVARCHAR(MAX) = N'{
"currency": {
"0": {
"ISOCode": "USD",
"name": "US Dollar",
"symbol": "$",
"real": "$#,###.##",
"free": "#,###.##"
},
"1": {
"ISOCode": "IEP",
"name": "Irish Pound or Punt",
"symbol": "£",
"real": "£#,###.##",
"free": "#,###.##"
},
"2": {
"ISOCode": "BEF",
"name": "Belgian Franc",
"symbol": "₣",
"real": "#,###.##₣",
"free": "#,###.##"
}
}
}'
SELECT j2.*
INTO #TempCurrencyTable
FROm OPENJSON(@json, '$.currency') j1
CROSS APPLY OPENJSON(j1.[value]) WITH (
currency INT
) j2
SELECT *
FROM #TempCurrencyTable
I get a null value and can't seem to get the header values.

header? JSON has no headers or columns, only arrays and dictionaries. The string you posted has aCurrencydictionary with elements strangely named0,1etc. Each of those elements is a dictionary with its own attributes. Nothing forces those dictionaries to have the same attributes as the otherscurrencydictionary and appear inj1.key. Thej1.valuecolumn contains the inner objects, which have nocurrencyattribute. You can write justSELECT j1.key FROM OPENJSON(@json, '$.currency') j1to get the key