SELECT - FOR#

Use the FOR clause to specify one of the following options for query results.

  • Format query results as JSON by specifying FOR JSON.

Syntax#

[ FOR { <JSON> } ]

<JSON> ::=
JSON
{
    { AUTO | PATH }
    [
        [ , ROOT [ ( 'RootName' ) ] ]
        [ , INCLUDE_NULL_VALUES ]
        [ , WITHOUT_ARRAY_WRAPPER ]
    ]
}

FOR JSON#

JSON Specify FOR JSON to return the results of a query formatted as JSON text. You also have to specify one of the following JSON modes : AUTO or PATH. For more information about the FOR JSON clause, see Format results as JSON with FOR JSON.

AUTO Format the JSON output automatically based on the structure of the SELECT statement by specifying FOR JSON AUTO. For more info and examples, see Format with AUTO Mode.

PATH Get full control over the format of the JSON output by specifying FOR JSON PATH. PATH mode lets you create wrapper objects and nest complex properties. For more info and examples, see Format Nested JSON Output with PATH Mode.

INCLUDE_NULL_VALUES Include null values in the JSON output by specifying the INCLUDE_NULL_VALUES option with the FOR JSON clause. If you don’t specify this option, the output does not include JSON properties for null values in the query results.

ROOT [ (‘RootName’) ] Add a single, top-level element to the JSON output by specifying the ROOT option with the FOR JSON clause. If you don’t specify the ROOT option, the JSON output doesn’t have a root element.

WITHOUT_ARRAY_WRAPPER Remove the square brackets that surround the JSON output by default by specifying the WITHOUT_ARRAY_WRAPPER option with the FOR JSON clause. If you don’t specify this option, the JSON output is enclosed within square brackets. Use the WITHOUT_ARRAY_WRAPPER option to generate a single JSON object as output. For more info, see Remove Square Brackets from JSON Output with the WITHOUT_ARRAY_WRAPPER Option.

For more info, see Format results as JSON with FOR JSON.

See Also#