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

5
  • 2
    Thank God, that seemed ridiculously short sighted it was not in 2016.
    – djangojazz
    Commented Aug 8, 2017 at 14:44
  • 3
    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.
    – Misbit
    Commented Nov 2, 2017 at 10:05
  • @vsdev Probably incorrect target platform. Please check your project properties and set to SQL Server 2016. Commented Nov 2, 2017 at 16:28
  • 1
    @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"
    – Misbit
    Commented Nov 3, 2017 at 3:07
  • @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?
    – Jargon
    Commented Jan 10, 2019 at 14:39
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
  • the question is, How to do that second example would work? Commented Aug 7, 2017 at 15:01
  • 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.
    – djangojazz
    Commented Aug 7, 2017 at 16:47
  • I'm glad, happy coding. If you are satisfied with the answer please mark it complete.
    – djangojazz
    Commented Aug 7, 2017 at 17:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.