简体   繁体   English

查询json格式的表

[英]Query a table with json format

I would like to know how can I read with a sql query a table like that: (this is just an example) 我想知道如何使用sql查询读取像这样的表:(这只是一个示例)

id,date,information
1,2018-26-02,{[{"iteration_number":0,"data":{"name":"Toto",values:{"PV":78,"SV":20,"TV":19},"state":"ok"},{"iteration_number":1,"data":{"name":"Baba",values:{"PV":68,"SV":10,"TV":11},"state":"ok"}}}]}

For example, to select the date of the first record, I will write: "SELECT date FROM table1 WHERE id=1; 例如,要选择第一条记录的日期,我将输入:“ SELECT date FROM table1 WHERE id = 1;

But if I just would like the name of the iteration number 0 ? 但是,如果我只想要迭代编号0的名称吗? (Toto) (多多)

Say me if I'm not understandable. 如果我无法理解就说。

Thanks for your response. 感谢您的答复。

Simon 西蒙

In MSSQL there is a JSON_VALUE function you can use for this: 在MSSQL中,您可以使用JSON_VALUE函数:

More documentation can be found here . 这里可以找到更多文档。

DECLARE @temp_table TABLE
(
    id INT NOT NULL IDENTITY(1, 1),
    date DATETIME NOT NULL,
    information VARCHAR(MAX) NOT NULL
);

INSERT INTO @temp_table
    (date, information)
VALUES 
    (GETDATE(), '[{"iteration_number":1,"data":{"name":"Toto",values:{"PV":78,"SV":20,"TV":19},"state":"ok"},{"iteration_number":1,"data":{"name":"Baba",values:{"PV":68,"SV":10,"TV":11},"state":"ok"}}}]');


SELECT date, JSON_VALUE(information,'$[0].data.name') AS Name
FROM @temp_table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM