I have a JSON file that I want to store fields of it in a database. When those fields are at a first level, access to it is very simple. However, accessing different fields of the JSON file when they have 3 levels, the insert SQL script when making that call gives me an error. I have this piece of JSON file
"alexa": 282,
"pointsData": {
"2727": {
"id": "2727",
"needModeration": false,
"quoteDoc": "Help us ...",
"quoteEnd": 590,
"quoteStart": 200,
"quoteText": "We always use ...",
"services": [
"Amazon"
],
...
With the following SQL query to insert in a created table service, having those two attributes that I try to insert:
DECLARE @json NVARCHAR(max) = N'{
"alexa": 282,
"pointsData": {
"2727": {
"id": "2727",
"needModeration": false,
"quoteDoc": "Help us ...",
"quoteEnd": 590,
"quoteStart": 200,
"quoteText": "We always use ...",
"services": [
"Amazon"
]
}
}
}';
INSERT INTO SERVICE
SELECT *
FROM OPENJSON(@json)
WITH (
pointsData.2727.id varchar(30),
pointsData.2727.services NVARCHAR(50)
)
I get an error:
Incorrect syntax near '.2727'.
If I put it like [pointsData].[2727].[id] varchar(30), the error is:
Incorrect syntax near '.'.
To access the field "alexa": 282, this script with the alexa query varchar (30) does it perfectly