JSON与现有json列的行配对

时间:2018-10-05 20:32:50

标签: sql json tsql sql-server-2016

借助@UnhandledExcepSean(Json pairs to rows),我得以了解如何将json键/值对转换为行。这并不像我想的那么容易。但是,我仍然有一个问题。我当时能够在这里看到如何完成此操作:

我希望它看起来像这样:

+-----------+-------+------------+-----------+-------+------------------+---------+-----+-------+-------+---------+
| DateTime  | Side  | Identifier |   Color   | Level |       SDSa       | Current | LEF |  Sys  | Membr |   SDS   |
+-----------+-------+------------+-----------+-------+------------------+---------+-----+-------+-------+---------+
| 10/1/2018 | 15:01 | Left       | 107604736 | Red   | 0.65 5.053539157 |       0 |   0 | BOBJ7 |     5 |       1 |
| 10/2/2018 | 15:01 | Left       | 107604737 | Red   | 0.65 5.053539157 |  0.3035 |   1 | BOBJ8 |     6 | 0.98962 |
| 10/3/2018 | 15:01 | Left       | 107604738 | Red   | 0.65 5.053539157 |  0.6070 |   2 | BOBJ9 |     7 | 0.96576 |
+-----------+-------+------------+-----------+-------+------------------+---------+-----+-------+-------+---------+

我发现了将文档存储在变量中后如何对其进行操作。我只是找不到平衡或回答如何对存储在列中db中的json使用同一查询。

这是文档的示例:

            {
              "DateTime": "10/1/2018 3:01:01 PM",
              "Side": "Left",
              "Identifier": "107604736",
              "Color": "Red",
              "Level": 0.65,
              "SDSa": 5.0535391569137573,
              "LEF": 0.0,
              "System": "BOBJ7",
              "Membrane": 5,
              "Current": [
                0.0,
                0.303535521,
                0.607071042,
                0.910606563,
                1.21414208,
                1.51767755,
                1.82121313,
                2.12474871,
                2.42828417,
                2.73181963,
                3.035355,
                3.33889079,
                3.64242625,
                3.94596171,
                4.24949741,
                4.553033,
                4.85656834,
                5.160104,
                5.46363926,
                5.76717472,
                6.07071,
                6.374246,
                6.67778158,
                6.981317,
                7.2848525,
                7.588388,
                7.89192343,
                8.195459,
                8.498995,
                8.80253,
                9.106066,
                9.409601,
                9.713137,
                10.0166721,
                10.3202076,
                10.6237431,
                10.9272785,
                11.230814,
                11.5343494,
                11.8378849,
                12.14142,
                12.4449568,
                12.7484922,
                13.0520277,
                13.3555632,
                13.6590986,
                13.9626341,
                14.26617,
                14.569705,
                14.87324,
                15.1767759,
                15.4803114,
                15.7838469,
                16.0873833,
                16.3909187,
                16.6944542,
                16.99799,
                17.3015251,
                17.60506,
                17.908596,
                18.2121315,
                18.515667,
                18.8192024,
                19.1227379,
                19.4262733,
                19.7298088,
                20.0333443,
                20.33688,
                20.6404152,
                20.94395,
                21.2474861,
                21.5510216,
                21.854557,
                22.1580925,
                22.461628,
                22.7651634,
                23.0686989,
                23.3722343,
                23.67577,
                23.9793053,
                24.28284,
                24.5863781,
                24.8899136,
                25.193449,
                25.4969845,
                25.80052,
                26.1040554,
                26.40759,
                26.7111263,
                27.0146618,
                27.3181973,
                27.6217327,
                27.9252682,
                28.2288036,
                28.53234,
                28.8358746,
                29.13941,
                29.4429455,
                29.74648,
                30.0500164
              ],
              "Measurements": [
                {
                  "FPOS": 1,
                  "Orient": "H",
                  "SDS50": 6.8644590377807617,
                  "ImageN": 0,
                  "SDS": [
                    1.0,
                    0.9896224,
                    0.9657684,
                    0.937525332,
                    0.9123899,
                    0.888601959,
                    0.8634422,
                    0.8382806,
                    0.8161909,
                    0.7984247,
                    0.7806436,
                    0.7606637,
                    0.739305735,
                    0.720110357,
                    0.7029224,
                    0.6836812,
                    0.660055459,
                    0.6333771,
                    0.6086391,
                    0.5865341,
                    0.5649619,
                    0.540699542,
                    0.5150109,
                    0.4912444,
                    0.470531732,
                    0.4510031,
                    0.429589,
                    0.407328546,
                    0.386515945,
                    0.369066834,
                    0.353259325,
                    0.336980551,
                    0.3197548,
                    0.302164435,
                    0.2844041,
                    0.264812022,
                    0.2432023,
                    0.221351326,
                    0.203265771,
                    0.190894246,
                    0.183355331,
                    0.178263873,
                    0.17374523,
                    0.169159114,
                    0.1635048,
                    0.15626812,
                    0.148360759,
                    0.141854659,
                    0.1383282,
                    0.136285663,
                    0.133377567,
                    0.127650827,
                    0.119844966,
                    0.111289464,
                    0.103005029,
                    0.09480265,
                    0.0866060555,
                    0.07927561,
                    0.0740468055,
                    0.07167137,
                    0.07141984,
                    0.07215432,
                    0.07259899,
                    0.07151956,
                    0.0681445152,
                    0.06242689,
                    0.05562524,
                    0.0492420681,
                    0.04441181,
                    0.0410645567,
                    0.0388742052,
                    0.0380086526,
                    0.03860841,
                    0.04033492,
                    0.04170464,
                    0.04166763,
                    0.0399215743,
                    0.0370087326,
                    0.0335897,
                    0.0301406663,
                    0.02666917,
                    0.023069948,
                    0.0193837862,
                    0.0161479469,
                    0.0140230907,
                    0.0133305294,
                    0.0137383873,
                    0.0145777585,
                    0.0155265825,
                    0.0166243,
                    0.01805606,
                    0.01932042,
                    0.0196347721,
                    0.01826151,
                    0.0154753188,
                    0.012052347,
                    0.008844916,
                    0.00625393027,
                    0.004384732,
                    0.00334442779
                  ],
                  "Processed": false
                },
                {
                  "FPOS": 2,
                  "Orient": "H",
                  "SDS50": 3.2426192760467529,
                  "ImageN": 0,
                  "SDS": [
                    1.0,
                    0.984369457,
                    0.940739632,
                    0.8834237,
                    0.8255927,
                    0.7736917,
                    0.7277459,
                    0.681687,
                    0.6329853,
                    0.582352459,
                    0.5328522,
                    0.48483035,
                    0.436798245,
                    0.388586164,
                    0.3424556,
                    0.302069068,
                    0.267472,
                    0.237092659,
                    0.209047124,
                    0.184123,
                    0.163114548,
                    0.144511625,
                    0.126265839,
                    0.107269846,
                    0.08942619,
                    0.07420107,
                    0.06193066,
                    0.0517099164,
                    0.0435060449,
                    0.0383287631,
                    0.0352067538,
                    0.03245497,
                    0.0286534447,
                    0.0242379941,
                    0.0201460086,
                    0.016266821,
                    0.0121985339,
                    0.008013846,
                    0.00560544431,
                    0.00502379332,
                    0.004533774,
                    0.004487538,
                    0.00551742641,
                    0.007546712,
                    0.009179788,
                    0.009129186,
                    0.007475236,
                    0.005305866,
                    0.004060863,
                    0.00462530646,
                    0.00631512143,
                    0.00748074846,
                    0.007632819,
                    0.006844715,
                    0.00562061928,
                    0.005166012,
                    0.006103046,
                    0.00768285431,
                    0.009006567,
                    0.009526576,
                    0.009514096,
                    0.009059127,
                    0.008065625,
                    0.006526997,
                    0.00434135273,
                    0.00191119267,
                    0.00254829624,
                    0.006861757,
                    0.0108742379,
                    0.0131905386,
                    0.013858974,
                    0.0132871456,
                    0.012235119,
                    0.01142512,
                    0.01094334,
                    0.0107232835,
                    0.0107062105,
                    0.010995524,
                    0.0112270387,
                    0.0102753676,
                    0.00612402242,
                    0.0010008571,
                    0.00318561238,
                    0.009038351,
                    0.0119382814,
                    0.0132263815,
                    0.013723935,
                    0.0133564435,
                    0.0129926438,
                    0.0127558094,
                    0.011888911,
                    0.009955382,
                    0.00670363754,
                    0.00246145763,
                    0.000897339836,
                    0.0037357898,
                    0.006457583,
                    0.007566943,
                    0.007532189,
                    0.006015837
                  ],
                  "Processed": false
                }}

1 个答案:

答案 0 :(得分:0)

您的JSON对象在第一级中具有一些易于查询的值。

此外,还有一个名为Current的值数组,还有更多:有一个名为Measurements的对象,它是一个对象数组。还有更多:Measurements中的对象本身包含一个值数组,称为SDS。 大概您不会对平坦的 give-me-everything 查询完全满意。这将导致很多行,并且到处都有重复的列...

尝试一下(我创建了一个模型表来模拟您的需求)

DECLARE @mockupTable TABLE (ID INT IDENTITY,YourJson NVARCHAR(MAX));
INSERT INTO @mockupTable VALUES
(N'{
    "DateTime": "10/1/2018 3:01:01 PM",
    "Side": "Left",
    "Identifier": "107604736",
    "Color": "Red",
    "Level": 0.65,
    "SDSa": 5.0535391569137573,
    "LEF": 0.0,
    "System": "BOBJ7",
    "Membrane": 5,
    "Current": [
    0.0,
    0.303535521,
    0.607071042,
    0.910606563,
    1.21414208,
    1.51767755,
    1.82121313,
    2.12474871,
    2.42828417,
    2.73181963,
    3.035355,
    3.33889079,
    3.64242625,
    3.94596171,
    4.24949741,
    4.553033,
    4.85656834,
    5.160104,
    5.46363926,
    5.76717472,
    6.07071,
    6.374246,
    6.67778158,
    6.981317,
    7.2848525,
    7.588388,
    7.89192343,
    8.195459,
    8.498995,
    8.80253,
    9.106066,
    9.409601,
    9.713137,
    10.0166721,
    10.3202076,
    10.6237431,
    10.9272785,
    11.230814,
    11.5343494,
    11.8378849,
    12.14142,
    12.4449568,
    12.7484922,
    13.0520277,
    13.3555632,
    13.6590986,
    13.9626341,
    14.26617,
    14.569705,
    14.87324,
    15.1767759,
    15.4803114,
    15.7838469,
    16.0873833,
    16.3909187,
    16.6944542,
    16.99799,
    17.3015251,
    17.60506,
    17.908596,
    18.2121315,
    18.515667,
    18.8192024,
    19.1227379,
    19.4262733,
    19.7298088,
    20.0333443,
    20.33688,
    20.6404152,
    20.94395,
    21.2474861,
    21.5510216,
    21.854557,
    22.1580925,
    22.461628,
    22.7651634,
    23.0686989,
    23.3722343,
    23.67577,
    23.9793053,
    24.28284,
    24.5863781,
    24.8899136,
    25.193449,
    25.4969845,
    25.80052,
    26.1040554,
    26.40759,
    26.7111263,
    27.0146618,
    27.3181973,
    27.6217327,
    27.9252682,
    28.2288036,
    28.53234,
    28.8358746,
    29.13941,
    29.4429455,
    29.74648,
    30.0500164
    ],
    "Measurements": [
    {
        "FPOS": 1,
        "Orient": "H",
        "SDS50": 6.8644590377807617,
        "ImageN": 0,
        "SDS": [
        1.0,
        0.9896224,
        0.9657684,
        0.937525332,
        0.9123899,
        0.888601959,
        0.8634422,
        0.8382806,
        0.8161909,
        0.7984247,
        0.7806436,
        0.7606637,
        0.739305735,
        0.720110357,
        0.7029224,
        0.6836812,
        0.660055459,
        0.6333771,
        0.6086391,
        0.5865341,
        0.5649619,
        0.540699542,
        0.5150109,
        0.4912444,
        0.470531732,
        0.4510031,
        0.429589,
        0.407328546,
        0.386515945,
        0.369066834,
        0.353259325,
        0.336980551,
        0.3197548,
        0.302164435,
        0.2844041,
        0.264812022,
        0.2432023,
        0.221351326,
        0.203265771,
        0.190894246,
        0.183355331,
        0.178263873,
        0.17374523,
        0.169159114,
        0.1635048,
        0.15626812,
        0.148360759,
        0.141854659,
        0.1383282,
        0.136285663,
        0.133377567,
        0.127650827,
        0.119844966,
        0.111289464,
        0.103005029,
        0.09480265,
        0.0866060555,
        0.07927561,
        0.0740468055,
        0.07167137,
        0.07141984,
        0.07215432,
        0.07259899,
        0.07151956,
        0.0681445152,
        0.06242689,
        0.05562524,
        0.0492420681,
        0.04441181,
        0.0410645567,
        0.0388742052,
        0.0380086526,
        0.03860841,
        0.04033492,
        0.04170464,
        0.04166763,
        0.0399215743,
        0.0370087326,
        0.0335897,
        0.0301406663,
        0.02666917,
        0.023069948,
        0.0193837862,
        0.0161479469,
        0.0140230907,
        0.0133305294,
        0.0137383873,
        0.0145777585,
        0.0155265825,
        0.0166243,
        0.01805606,
        0.01932042,
        0.0196347721,
        0.01826151,
        0.0154753188,
        0.012052347,
        0.008844916,
        0.00625393027,
        0.004384732,
        0.00334442779
        ],
        "Processed": false
    },
    {
        "FPOS": 2,
        "Orient": "H",
        "SDS50": 3.2426192760467529,
        "ImageN": 0,
        "SDS": [
        1.0,
        0.984369457,
        0.940739632,
        0.8834237,
        0.8255927,
        0.7736917,
        0.7277459,
        0.681687,
        0.6329853,
        0.582352459,
        0.5328522,
        0.48483035,
        0.436798245,
        0.388586164,
        0.3424556,
        0.302069068,
        0.267472,
        0.237092659,
        0.209047124,
        0.184123,
        0.163114548,
        0.144511625,
        0.126265839,
        0.107269846,
        0.08942619,
        0.07420107,
        0.06193066,
        0.0517099164,
        0.0435060449,
        0.0383287631,
        0.0352067538,
        0.03245497,
        0.0286534447,
        0.0242379941,
        0.0201460086,
        0.016266821,
        0.0121985339,
        0.008013846,
        0.00560544431,
        0.00502379332,
        0.004533774,
        0.004487538,
        0.00551742641,
        0.007546712,
        0.009179788,
        0.009129186,
        0.007475236,
        0.005305866,
        0.004060863,
        0.00462530646,
        0.00631512143,
        0.00748074846,
        0.007632819,
        0.006844715,
        0.00562061928,
        0.005166012,
        0.006103046,
        0.00768285431,
        0.009006567,
        0.009526576,
        0.009514096,
        0.009059127,
        0.008065625,
        0.006526997,
        0.00434135273,
        0.00191119267,
        0.00254829624,
        0.006861757,
        0.0108742379,
        0.0131905386,
        0.013858974,
        0.0132871456,
        0.012235119,
        0.01142512,
        0.01094334,
        0.0107232835,
        0.0107062105,
        0.010995524,
        0.0112270387,
        0.0102753676,
        0.00612402242,
        0.0010008571,
        0.00318561238,
        0.009038351,
        0.0119382814,
        0.0132263815,
        0.013723935,
        0.0133564435,
        0.0129926438,
        0.0127558094,
        0.011888911,
        0.009955382,
        0.00670363754,
        0.00246145763,
        0.000897339836,
        0.0037357898,
        0.006457583,
        0.007566943,
        0.007532189,
        0.006015837
        ],
        "Processed": false}
    ]}');

-这是获取所有第一级元素的查询

SELECT ID
      ,A.*
FROM @mockupTable 
CROSS APPLY OPENJSON(YourJson) A
WHERE ID=1;

-但是有WITH子句,它允许您将此列列表转换为带有类型化列的单个结果行:

SELECT ID
      ,A.*
FROM @mockupTable 
CROSS APPLY OPENJSON(YourJson)
WITH
(
     DateTime       DATETIME        N'$.DateTime'
    ,Side           NVARCHAR(50)    N'$.Side'
    --add all variables of the first level
    ,Membrane       INT             N'$.Membrane'
) A
WHERE ID=1;

-通过此方法,我们可以掌握current的值:

SELECT ID
      ,A.*
      ,C.*
FROM @mockupTable 
CROSS APPLY OPENJSON(YourJson)
WITH
(
     DateTime       DATETIME        N'$.DateTime'
    ,Side           NVARCHAR(50)    N'$.Side'
    --add all variables of the first level
    ,Membrane       INT             N'$.Membrane'
) A
CROSS APPLY OPENJSON(YourJson,'$.Current') C
WHERE ID=1;

-下一个查询允许获取Measurement中两个对象的一级元素

SELECT ID
      ,A.*
      ,M.*
FROM @mockupTable 
CROSS APPLY OPENJSON(YourJson)
WITH
(
     DateTime       DATETIME        N'$.DateTime'
    ,Side           NVARCHAR(50)    N'$.Side'
    --add all variables of the first level
    ,Membrane       INT             N'$.Membrane'
) A
CROSS APPLY OPENJSON(YourJson,'$.Measurements') 
WITH
(
     FPOS            INT             N'$.FPOS'
    ,Orient          VARCHAR(10)     N'$.Orient'
    --Add more
) M
WHERE ID=1;

-最后一个查询将返回两个具有SDS中所有值的测量值。 (请查看AS JSON子句中的WITH!)

SELECT ID
      ,A.*
      ,M.*
      ,V.*
FROM @mockupTable 
CROSS APPLY OPENJSON(YourJson)
WITH
(
     DateTime       DATETIME        N'$.DateTime'
    ,Side           NVARCHAR(50)    N'$.Side'
    --add all variables of the first level
    ,Membrane       INT             N'$.Membrane'
) A
CROSS APPLY OPENJSON(YourJson,'$.Measurements') 
WITH
(
     FPOS            INT              N'$.FPOS'
    ,Orient          NVARCHAR(10)     N'$.Orient'
    --Add more
    ,[SDS]           NVARCHAR(MAX)    AS JSON --allows to return the JSON reflecting the array
) M
CROSS APPLY OPENJSON(M.SDS) V
WHERE ID=1;

现在,您知道了如何获取所有内容。您将如何处理此事取决于您...