0

I have the following sql code:

SELECT 
definitions = JSON_QUERY('{"ID": "INT)", "ID": "VARCHAR(23)"}'),
data = 
(
    SELECT ID, ID_FOR_DISPLAY
    FROM (SELECT TOP 1 ID, dbo.A2F_0012_ReturnIDforDisplay(1) as ID_FOR_DISPLAY FROM RateReviewBaseTBL) v(ID, ID_FOR_DISPLAY)
    FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
)
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;

This came from an example I found which utilized the following: https://sqlfiddle.com/sql-server/online-compiler?id=ce2d52e2-d1f7-4d9c-8d02-a7d6fc062b7d

I copied this structure almost exactly except for some reason my code returns the object in a string format:

{"definitions":{"ID": "INT)", "ID_FOR_DISPLAY": "VARCHAR(23)"},"data":"{\"ID\":128,\"ID_FOR_DISPLAY\":\"NM-00000001\"}"}

Its not clear why its making my object a string rather than the example which seems to show the object as an object with {} and no strings around it.

However, I want the results to show this:

{"definitions":{"ID": "INT)", "ID_FOR_DISPLAY": "VARCHAR(23)"},"data":{"ID":128,"ID_FOR_DISPLAY":"NM-00000001"}}
12
  • 2
    Include all the relevant information we need in the question. There's no sample data in your question, nor do we have the definition for your function A2F_0012_ReturnIDforDisplay, so we can't run your query.
    – Thom A
    Commented Apr 2 at 13:00
  • I tried to include the one fiddle to show the exactly same formatted code in that example works. How could my data effect whether the node becomes a string or not if I follow the same syntax?
    – Qiuzman
    Commented Apr 2 at 13:02
  • Without an minimal reproducible example, I cannot comment on why your code does not work as you expect.
    – Thom A
    Commented Apr 2 at 13:02
  • 3
    without_array_wrapper does this, you have to wrap it in JSON_QUERY Commented Apr 2 at 13:04
  • 1
    Something like: select ..., JSON_QUERY((select ... json path,without_array_wrapper))... Commented Apr 2 at 13:12

1 Answer 1

0

As siggemannen notes just wrap your subquery in JSON_QUERY. This is the query from the fiddle, as I cannot run your query due to no sample data or definitions

SELECT JSON_QUERY('{"NAME": "VARCHAR(23)", "DESCRIPTION": "VARCHAR(23)"}') AS definitions,
       JSON_QUERY((SELECT NAME,
                          DESCRIPTION
                   FROM (SELECT TOP (1)
                                Name,
                                Description
                         FROM dbo.Product
                         ORDER BY <Expression(s)>) v(NAME, DESCRIPTION)
                  FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)) AS data
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;

db<>fiddle

2
  • So json_query just makes sure the node remains a json object?
    – Qiuzman
    Commented Apr 2 at 13:38
  • 2
    @IrishRedneck Yes JSON_QUERY with no path forces it to assume it's valid JSON and not escape it. You only need to do this if you have WITHOUT_ARRAY_WRAPPER Commented Apr 2 at 14:10

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