

Question_author.first_name AS 'author.first_name', Question_types.created_at AS 'type.created_at', Instructor.last_updated AS '_updated',Ĭourse.created_at AS 'course.created_at',Ĭourse.last_updated AS 'course.last_updated', I've found that I can use subqueries to populate a field (in this case the questions field) with a JSON string that that contains a list of questions.Īuthor.first_name AS 'author.first_name',Īpartment_name AS 'partment_name',Īuthor.created_at AS 'author.created_at',Īuthor.last_updated AS 'author.last_updated',Ĭscription AS 'scription', Object or array cannot be found in the specified JSON path.Īs expected, strict mode results in an error message explaining the error.I am trying to build a query that results in a JSON object generated by SQL Server. Result: Msg 13624, Level 16, State 2, Line 1 SELECT JSON_QUERY('', 'strict $.Name') AS 'Result' Here’s what happens when the path expression contains an error while in lax mode. Here’s an example to demonstrate the difference between these two modes. In strict mode, the function raises an error if the path expression contains an error. For example, if you request the value $.name, and the JSON text doesn’t contain a name key, the function returns null, but does not raise an error. In lax mode, the function returns empty values if the path expression contains an error.The value of the path mode determines what happens when the path expression contains an error. Example 5 – Path ModeĪs mentioned, you also have the option of specifying the path mode. In this example I used JSON_VALUE() to extract various scalar values, but I also used JSON_QUERY() to return a whole array (which JSON_VALUE() can’t do). "Hobbies": ["Eating", "Sleeping", "Base AS AS AS 'Last Hobby' Here’s an example: DECLARE NVARCHAR(4000) However, there’s nothing to stop you combining both functions within a query to return data at various levels of granularity. If you want to return a scalar value, use the JSON_VALUE() function instead. The JSON_QUERY() function is not designed to return scalar values. This assumes that the JSON document is stored in a column called Document, which is in a table called Json_Documents.

JSON_QUERY(Document,'$.Cities') AS 'City 1' If we were to put the data from the previous example into a database, we could rewrite the query as follows: SELECT Here’s what happens when we do that using the same data from the previous examples: DECLARE NVARCHAR(4000) The second argument is optional, so if you omit it, the whole JSON document is returned. Like this: DECLARE NVARCHAR(4000)Įxample 2 – Return the Whole JSON Expression I could access the second item by using Cities. In this case I use Cities to reference the first item in the array (JSON arrays use zero-based numbering). Once I’ve done this, I run a query against that array. In this example, I first declare and set a variable called I then assign an array to this variable. Here’s an example to demonstrate basic usage of the JSON_QUERY() function. The path mode (if supplied) comes before the dollar sign. This value determines what happens in the event the supplied path is invalid. This optional path mode can be a value of either lax or strict. The path argument (if supplied) can include an optional path mode component. The path argument is optional (if you don’t provide it, the whole JSON document is returned). Where expression is the JSON string expression, and path is the object or array that you want to extract from that expression. The syntax goes like this: JSON_QUERY ( expression ) You can also provide a second (optional) argument to specify the object or array to extract. To use this function, you provide the JSON expression as an argument. When using JSON with SQL Server, you can use the JSON_QUERY() function to extract an object or an array from a JSON string.
