0

Here is the table definition:

key_en, text_en, html_text_en, key_fr, text_fr, html_text_fr

Here is my query:

SELECT  
    [key_en] AS 'en.key'
    ,[text_en] AS 'en.text'
    ,[html_text_en] AS 'en.htmltext'
    ,[key_fr] AS 'fr.key'
    ,[text_fr] AS 'fr.text'
    ,[html_text_fr] AS 'fr.htmltext'
FROM [MyContent]
FOR JSON PATH;

The result I get as JSON is correct.

[
    {
        "en": {
            "key": "key en",
            "text": "text en",
            "htmltext": "html en"
        },
        "fr": {
            "key": "key fr",
            "text": "text fr",
            "htmltext": "html fr"
        }
    },
    {
        "en": {
            "key": "key2 en",
            "text": "text2 en",
            "htmltext": "html2 en"
        },
        "fr": {
            "key": "key2 fr",
            "text": "text2 fr",
            "htmltext": "html2 fr"
        }
    }
]

However the result set is in a single row (very long string).

I'd like to spread the result set across as many rows as the data in the table. The expected result should be:

    ROW 1 
    {
        "en": {
            "key": "key en",
            "text": "text en",
            "htmltext": "html en"
        },
        "fr": {
            "key": "key fr",
            "text": "text fr",
            "htmltext": "html fr"
        }
    }

    ROW 2 
    {
        "en": {
            "key": "key2 en",
            "text": "text2 en",
            "htmltext": "html2 en"
        },
        "fr": {
            "key": "key2 fr",
            "text": "text2 fr",
            "htmltext": "html2 fr"
        }
    }

What changes do I have to bring to reach my goal?

4
  • Can you post the expected result for one row? Thanks. Commented Dec 17, 2019 at 12:38
  • I just edited my question. Commented Dec 17, 2019 at 12:43
  • 1
    Probably FOR JSON PATH, WITHOUT_ARRAY_WRAPPER will help. I think that the answers are correct. Commented Dec 17, 2019 at 12:44
  • Yes, using that parameter on Martin's answer works. Thanks Commented Dec 17, 2019 at 12:55

2 Answers 2

3

You can use this modified query which will return a row of JSON per row of your table:

SELECT  (
        SELECT  [key_en] AS 'en.key',
                [text_en] AS 'en.text',
                [html_text_en] AS 'en.htmltext',
                [key_fr] AS 'fr.key',
                [text_fr] AS 'fr.text',
                [html_text_fr] AS 'fr.htmltext'
          FOR JSON PATH
        )
  FROM  [MyContent]
Sign up to request clarification or add additional context in comments.

1 Comment

Your solution returns an array per row. It should be an array of rows. I have updated my question to reflect that.
1

You can use apply:

SELECT j.j
FROM [MyContent] c CROSS APPLY
     (SELECT c.[key_en] AS 'en.key',
             c.[text_en] AS 'en.text',
             c.[html_text_en] AS 'en.htmltext',
             c.[key_fr] AS 'fr.key',
             c.[text_fr] AS 'fr.text',
             c.[html_text_fr] AS 'fr.htmltext'
      FOR JSON PATH
     ) j(j);

1 Comment

Your solution returns an array per row. It should be an array of rows. I have updated my question to reflect that.

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.