0

I'm struggling to combine the contents of a single table in SQL Server that contains both numeric data and JSON-formatted strings into a consolidated JSON output using SQL Server's "FOR JSON" function.

I'm running into issues with the escape characters mainly, doesn't appear to be an easy approach to combining JSON and non-JSON data into a JSON-formatted SQL Server Query output. Can anyone point me to an example of a successful query that's done this?

Sample Data:

CREATE TABLE settings (
    Id INT PRIMARY KEY NOT NULL,
    FileName nvarchar(max) NOT NULL,
    FilePath nvarchar(max) NOT NULL,
    Date datetime NOT NULL,
    Json nvarchar(max)
    );


INSERT INTO settings 
    (
    Id,
    FileName,
    FilePath,
    Date,
    Json
    )
VALUES
    (
    1,
    'contents.json',
    'folder1/folder2/contents.json',
    '2000-01-01T00:00:00.000',
    '{
                "A": 10,
                "B": 20,
                "C": {
                    "setting1": 30,
                    "setting2": 40,
                    "setting3": 50
                },
                "D": {
                    "setting1": 30,
                    "setting2": 40,
                    "setting3": 50
                },
                "E": true,
                "F": false
                "G": "John Doe"
            }'
    );

Sample Query:

SELECT
    FileName
    , FilePath
    , Id
    , Date
    , Json
FROM settings
FOR JSON AUTO;

Expected Output:

    [
        {
            "FileName": "contents.json",
            "FilePath": "folder1/folder2/contents.json",
            "Id": 1,
            "Date": "2000-01-01T00:00:00.000",
            "Json": [
                {
                    "A": 10,
                    "B": 20,
                    "C": {
                        "setting1": 30,
                        "setting2": 40,
                        "setting3": 50
                    },
                    "D": {
                        "setting1": 30,
                        "setting2": 40,
                        "setting3": 50
                    },
                    "E": true,
                    "F": false
                    "G": "John Doe"
                }
            ]
        }
    ]
2

1 Answer 1

1

Your example settings.Json is invalid:

                "F": false

Should be:

                "F": false,

Once you fix that issue you can change your query to nest Json's JSON like this:

SELECT
    FileName
    , FilePath
    , Id
    , Date
    , [Json] = Json_Query(concat('[', Json, ']'))
FROM settings
FOR JSON AUTO;

Which yields the result:

[
    {
        "FileName": "contents.json",
        "FilePath": "folder1\/folder2\/contents.json",
        "Id": 1,
        "Date": "2000-01-01T00:00:00",
        "Json": [
            {
                "A": 10,
                "B": 20,
                "C": {
                    "setting1": 30,
                    "setting2": 40,
                    "setting3": 50
                },
                "D": {
                    "setting1": 30,
                    "setting2": 40,
                    "setting3": 50
                },
                "E": true,
                "F": false,
                "G": "John Doe"
            }
        ]
    }
]

Note that SQL Server has escaped / characters in FilePath using \/ as per the JSON specification.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @AlwaysLearning, I rushed putting together my example data. Sorry about that. The JSON_QUERY() wrapper was the missing secret sauce on this one. Thanks for the quick reply.

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.