3

I am having a problem with union operation on json results as for some reason my nested json queries results get their double quotes escaped which causes parse errors in my application. Here Is the example:

SELECT DISTINCT s.*,
JSON_QUERY((select * from SomeTable WHERE Id = 'some ID' FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)) AS ExampleColumn from [SomeOtherTable] sot where sot.Id = 'some other ID' 
UNION ALL
SELECT DISTINCT S1.*, JSON_QUERY((select * from SomeTable WHERE Id = 'some other other ID' FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)) AS ExampleColumn from [SomeOtherTable] sot1 where sot1.Id = 'some other other other ID' FOR JSON PATH

The result is correct, only the nested JSON_QUERY results have escaped double quotes. Can anyone help me please?

1
  • Could you show the whole query? Can you reproduce the issue with some sample data? It will be really easy to get answer if you do so. Commented Oct 10, 2017 at 6:25

1 Answer 1

2

Your query converts json to text, that's why second json threads them as string and adds escaped double quotes.

You can try this. I hope it helps you.

SELECT STRING_AGG (JsonText,',') FROM (
    SELECT ( SELECT DISTINCT sot.*, ExampleColumn.*
             FROM 
                [SomeOtherTable] sot 
                OUTER APPLY(select * from SomeTable WHERE Id = 'some ID') AS ExampleColumn 
             WHERE 
                sot.Id = 'some other ID' FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER ) JsonText

    UNION ALL

    SELECT ( SELECT DISTINCT sot1.*, ExampleColumn.*
             FROM 
                [SomeOtherTable] sot1 
                OUTER APPLY((select * from SomeTable WHERE Id = 'some other other ID')) AS ExampleColumn 
             WHERE sot1.Id = 'some other other other ID' FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER ) JsonText
)
Sign up to request clarification or add additional context in comments.

Comments

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.