0

My database table is created as :

declare @library table (dependencies varchar(max))

insert into @library values('{"jar":"azure.com","maven":"azure.com","maven":"azure.com","maven":"azurebook.com"}')

I am trying to have JSON as

{
 "libraries":[
    {
      "jar" : { "coordinates":"azure.com"}
    },
    {
      "maven" : {"coordinates":"azure.com" }
    },
    {
      "maven" : {"coordinates":"azurebook.com" }
    }
 ]
}

And my SQL code looks like

SELECT
  (
      select
        j.jar as [jar.coordinates],
        j.maven as [maven.coordinates]
      FROM OPENJSON(l.dependencies)
        WITH (
          jar varchar(100),
          maven varchar(100)
        ) j
      FOR JSON PATH
  ) as libraries
  
FROM @library l
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;

And my output which is not same as desired json

 {
 "libraries":[
    {
      "jar" : { "coordinates":"azure.com"}
    },
    {
      "maven" : {"coordinates":"azure.com" }
    }
 ]
}

As in my output, my other property or column maven does not including. Can somebody help me out?

Thank you

3
  • 2
    Sample data will help us help you.
    – Thom A
    Commented Mar 28, 2022 at 15:07
  • @Larnu , I have edited, can you check
    – blac040
    Commented Mar 28, 2022 at 15:45
  • Do ensure that's really your input and it cannot be changed on the producing end, because duplicate property names are a bad idea (the standard doesn't outright forbid it, but notes the behavior of implementations on receiving it is not consistent). You'd normally expect an array of values, as per your output, but not your input. This would typically indicate a fault in the process generating this output (it could easily happen if the output is manually generated rather than serialized, for example). Commented Mar 28, 2022 at 16:04

1 Answer 1

1

You can't do this using OPENJSON with a schema, because JSON normally expects only unique property names. You can use OPENJSON without a schema, but you also cannot use FOR JSON to create such a JSON value. You would need STRING_AGG again

SELECT
  JSON_QUERY((
      SELECT
        '[' + STRING_AGG(
          '{"' +
          STRING_ESCAPE(j.[key], 'json') +
          '":"' +
          STRING_ESCAPE(j.value, 'json') +
          '"}',
          ','
        ) + ']'
      FROM OPENJSON(l.dependencies) j
  )) as libraries
FROM @library l
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;

db<>fiddle

5
  • If I have null , so It is not working. Then what should we do ? i want to make it more generic
    – blac040
    Commented Mar 29, 2022 at 15:22
  • 1
    You can use CONCAT instead, which ignores nulls Commented Mar 29, 2022 at 15:30
  • But If values are null, then i don't that property to include in my JSON. Like I tested, with null values, It is not property null. But I don't want that property in json if null
    – blac040
    Commented Mar 29, 2022 at 15:35
  • 1
    OK then use the original +, it works fine for me? dbfiddle.uk/… Can you construct a fiddle showing it not working and showing what you would like to see Commented Mar 29, 2022 at 15:37
  • Okay got it. Its working now
    – blac040
    Commented Mar 29, 2022 at 15:39

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