Monday, October 17, 2022

Format Query Results as JSON in SQL

 When you use the FOR JSON clause, you can specify the structure of the JSON output explicitly, or let the structure of the SELECT statement determine the output.


  • To maintain full control over the format of the JSON output, use FOR JSON PATH. You can create wrapper objects and nest complex properties.

  • To format the JSON output automatically based on the structure of the SELECT statement, use FOR JSON AUTO.
Example: 

SELECT name, surname
FROM emp
FOR JSON AUTO;

Result:

[{
    "name": "John"
}, {
    "name": "Jane",
    "surname": "Doe"
}]





No comments:

Post a Comment

How to use SQL pagination using LIMIT and OFFSET

  How to extract just a portion of a larger result set from a SQL SELECT. LIMIT n is an alternative syntax to the FETCH FIRST n ROWS ONLY. T...