Analysis of the json function in SQL server2016, server2016json

Source: Internet
Author: User

Analysis of the json function in SQL server2016, server2016json

Test the basic structure and construct a json format from the query results.

Create table t1 (ID int identity, name nvarchar (50), Chinese int, Math int) insert into t1 values ('zhang san', 90,80), ('Li si', 75,90 ), ('wang wu', 68,100) select * from t1select * from t1 for json auto -- Query Result ID name Chinese Math ----------- Jun ----------- 1 Zhang San 90 802 Li Si 75 903 Wang Wu 68 100 -- json format [{"ID ": 1, "name": "Zhang San", "Chinese": 90, "Math": 80 },{ "ID": 2, "name": "Li Si ", "Chinese": 75, "Math": 90 },{ "ID": 3, "name": "Wang Wu", "Chinese": 68, "Math ": 100}]

This is the json query result in the default mode. Is it very clear?

Then we make persistent efforts to make the second wave of paper. If we want to continue to engage in hierarchical relationships. We can also write it like this. For example, placing scores in a node named points can also be layered.

Select ID, name, Chinese as [Points. chinese], Math as [Points. math] from t1 for json path -- result json [{"ID": 1, "name": "Zhang San", "Points": {"Chinese": 90, "Math": 80 },{ "ID": 2, "name": "Li Si", "Points": {"Chinese": 75, "Math ": 90 },{ "ID": 3, "name": "Wang Wu", "Points": {"Chinese": 68, "Math": 100}]

Their scores are placed in json and wrapped by a point.

If I want to add a header in the result to wrap it, of course, I can use each column as an alias [root. col], but it is a bit cool. So we can use this root keyword to add a top node.

Select ID, name, Chinese as [Points. chinese], Math as [Points. math] from t1 for json path, root ('root') -- returned json result {"root": [{"ID": 1, "name": "James ", "Points": {"Chinese": 90, "Math": 80 }}, {"ID": 2, "name": "Li Si", "Points ": {"Chinese": 75, "Math": 90 }}, {"ID": 3, "name": "Wang Wu", "Points": {"Chinese ": 68, "Math": 100}]}

Of course, when querying data, it is inevitable that null values are input. In this regard, how does for json process? I added a piece of data to the test table for query.

Insert into t1 values ('zhao liu', 100, null) select ID, name, Chinese as [Points. chinese], Math as [Points. math] from t1 where id in (3, 4) for json auto -- return result of json [{"ID": 3, "name": "", "Points. chinese ": 68," Points. math ": 100}, {" ID ": 4," name ":" Zhao Liu "," Points. chinese ": 100}]

In auto mode, this attribute is ignored if it is null. In this way, it is easy to see that the number of attributes returned by each set is not the same. To cope with this situation, we can use the incluede_null_values keyword, even if it is a null value

Select ID, name, Chinese as [Points. chinese], Math as [Points. math] from t1 where id in (3, 4) for json auto, include_null_values -- return result of json [{"ID": 3, "name": "Wang Wu ", "Points. chinese ": 68," Points. math ": 100}, {" ID ": 4," name ":" Zhao Liu "," Points. chinese ": 100," Points. math ": null}]

With this keyword, the Null value can be taken out, and the value in it is Null.

Okay. This experiment ends ~ Then I try to parse the json syntax and try again ~

Actually, the syntax is almost the same as the xml type ~ However, the database previously supported the xml data type, but json can only be converted and parsed using strings.

The preceding section describes the json function in SQL server2016. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.