I've searched high and low but can't seem to find anything even remotely like what I'm trying to do but I'm sure this isn't anything new and I'm quite sure I'm having a Homer moment.
Given a table that contains a JSON column:
DECLARE @Table TABLE
(
[VALUE] nvarchar(max) NOT NULL
);
The value within the column can be any valid JSON OBJECT, I don't have control over the data, only it's validity and that all the entries are of the same structure but which I don't know. This is important as it does restrict certain available options.
INSERT INTO @Table
VALUES ( N'{"X":"2020-01-01","Y":"27"}' )
, ( N'{"X":"2020-02-01","Y":"48"}' )
, ( N'{"X":"2020-04-01","Y":"63"}' )
, ( N'{"X":"2020-05-01","Y":"75"}' )
, ( N'{"X":"2020-06-01","Y":"32"}' )
, ( N'{"X":"2020-08-01","Y":"12"}' )
, ( N'{"X":"2020-09-01","Y":"96"}' )
, ( N'{"X":"2020-10-01","Y":"105"}' );
INSERT INTO @Table
VALUES ( N'{"Monkey":"1","Elephant":"9"}' )
, ( N'{"Monkey":"2","Elephant":"8"}' )
, ( N'{"Monkey":"3","Elephant":"7"}' )
, ( N'{"Monkey":"4","Elephant":"6"}' )
, ( N'{"Monkey":"5","Elephant":"5"}' )
, ( N'{"Monkey":"6","Elephant":"4"}' )
, ( N'{"Monkey":"7","Elephant":"3"}' )
, ( N'{"Monkey":"8","Elephant":"2"}' );
I need to be able to convert the contents of all the rows into a single JSON Document:
[{
"VALUES": [
{ "X": "2020-01-01", "Y": "27" },
{ "X": "2020-02-01", "Y": "48" },
{ "X": "2020-04-01", "Y": "63" },
{ "X": "2020-05-01", "Y": "75" },
{ "X": "2020-06-01", "Y": "32" },
{ "X": "2020-08-01", "Y": "12" },
{ "X": "2020-09-01", "Y": "96" },
{ "X": "2020-10-01", "Y": "105" }
]
}]
Given my SELECT statement, I'm able to at least query the data into a table result:
SELECT [VALUES] = JSON_QUERY( [VALUE], N'$' )
FROM @Table;
VALUES
{"X":"2020-01-01","Y":"27"}
{"X":"2020-02-01","Y":"48"}
{"X":"2020-04-01","Y":"63"}
{"X":"2020-05-01","Y":"75"}
{"X":"2020-06-01","Y":"32"}
{"X":"2020-08-01","Y":"12"}
{"X":"2020-09-01","Y":"96"}
{"X":"2020-10-01","Y":"105"}
But when I convert the result to JSON it goes awry:
SELECT [VALUES] = JSON_QUERY( [VALUE], N'$' )
FROM @Table
FOR JSON PATH;
[
{ "VALUES": { "X": "2020-01-01", "Y": "27" } },
{ "VALUES": { "X": "2020-02-01", "Y": "48" } },
{ "VALUES": { "X": "2020-04-01", "Y": "63" } },
{ "VALUES": { "X": "2020-05-01", "Y": "75" } },
{ "VALUES": { "X": "2020-06-01", "Y": "32" } },
{ "VALUES": { "X": "2020-08-01", "Y": "12" } },
{ "VALUES": { "X": "2020-09-01", "Y": "96" } },
{ "VALUES": { "X": "2020-10-01", "Y": "105" } }
]
For the life of me I just can't seem to get the last bit sorted, hopefully another set of smarter eyes will spot my mistake.