Graham
Graham

Reputation: 8181

How to select multiple rows as multiple rows using for json path

If I do this:

SELECT *
FROM someTable
FOR JSON PATH

I get a single result.

I want each row of the table to output as a separate row containing the json for just that row.

Can that be done?

Upvotes: 0

Views: 1401

Answers (2)

Charlieface
Charlieface

Reputation: 72469

You can do this as a nested subquery. WITHOUT_ARRAY_WRAPPER will remove the [] around the JSON also

SELECT
  json = (
    SELECT p.*
    FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
  )
FROM someTable p;

Upvotes: 1

Graham
Graham

Reputation: 8181

Never mind. I figured it out.

SELECT (SELECT *
        FROM someTable
        WHERE someUniqueColumn = p.someUniqueColumn
        FOR JSON PATH)
FROM someTable p

Upvotes: 0

Related Questions