4
declare @Path as nvarchar(100) 
set @path = '$.path.to."sub-object"'

DECLARE @json NVARCHAR(4000) = N'{  
      "path": {  
            "to":{  
                 "sub-object":["en-GB", "en-UK","de-AT","es-AR","sr-Cyrl"]  
                 }  
              }  
 }';

SELECT [key], value
FROM OPENJSON(@json, @Path)

I got error:

Incorrect syntax near '@Path'.

how to declare @path that i can change it.

2
  • What you posted does not error out on my system. What version of SQL Server are you running? OPENJSON is a 2016 feature. Commented Aug 7, 2017 at 14:13
  • Realy? i'm using ms sql 2016 server Commented Aug 7, 2017 at 14:56

2 Answers 2

5

Passing path as variable to OPENJSON is available from SQL Server 2017 (aka vNext):

In SQL Server 2017 and in Azure SQL Database, you can provide a variable as the value of path.

declare @Path as nvarchar(100) 
set @path = '$.path.to."sub-object"'

DECLARE @json NVARCHAR(4000) = N'{  
      "path": {  
            "to":{  
                 "sub-object":["en-GB", "en-UK","de-AT","es-AR","sr-Cyrl"]  
                 }  
              }  
 }';

SELECT [key], value
FROM OPENJSON(@json, @Path);

DbFiddle Demo

Sign up to request clarification or add additional context in comments.

5 Comments

Thank God, that seemed ridiculously short sighted it was not in 2016.
This is great, thanks for pointing that out! I do have a problem though; in SSMS this works fine but when added in a stored procedure in a SQL project in Visual Studio it doesn't recognize the syntax, marks it as an error thus preventing me to build/deploy. Anyone else having this issue? Am I perhaps missing an add-on to handle new syntax? I've updated everything I can find.
@vsdev Probably incorrect target platform. Please check your project properties and set to SQL Server 2016.
@lad2025 I actually tried 2016, 2017 as well as the Azure once. No change. It always marks it as an error synthax. "SQL46010: Incorrect syntax near @JsonPath"
@vsdev This is still an issue in stored procedures with the latest version of VS2017 (15.9.5) and SSDT. Looks like this is never going to get fixed. How did you get around the issue?
3

Interesting it appears you are following this link: https://learn.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql.

This works when done inline:

DECLARE @json VARCHAR(4000) = N'{  
      "path": {  
            "to":{  
                 "sub-object":["en-GB", "en-UK","de-AT","es-AR","sr-Cyrl"]  
                 }  
              }  
 }';

SELECT [key], value
FROM OPENJSON(@json, '$.path.to."sub-object"')

When you make the change to a reference of a variable it does not:

declare @Path as nvarchar(128)  = '$.path.to."sub-object"'

DECLARE @json nVARCHAR(4000) = N'{  
      "path": {  
            "to":{  
                 "sub-object":["en-GB", "en-UK","de-AT","es-AR","sr-Cyrl"]  
                 }  
              }  
 }';

SELECT [key], value
FROM OPENJSON(@json, @Path)

UPDATED Kind of a hack but it works

declare @Path as nvarchar(128)  = '$.path.to."sub-object"'

DECLARE @json nVARCHAR(4000) = N'{  
      "path": {  
            "to":{  
                 "sub-object":["en-GB", "en-UK","de-AT","es-AR","sr-Cyrl"]  
                 }  
              }  
 }';

DECLARE @SQL NVARCHAR(MAX) = 
'SELECT [key], value
FROM OPENJSON(''' + @json + ''', ''' + @Path + ''')'

EXEC sp_executesql @Sql

3 Comments

the question is, How to do that second example would work?
I can hack it to work with dynamic sql but that seems silly. There has to be a better way to do this. It must be the type of the path it does not like.
I'm glad, happy coding. If you are satisfied with the answer please mark it complete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.