Instagram JSON导出。带有OPENJSON的数据透视图子数组

时间:2019-12-13 01:13:32

标签: json tsql instagram

我刚刚从Instragram收到了一个扩展名为.json的文件中的喜欢的数据。

{"media_likes": [["2019-12-10T17:49:02", "feojo"], 
                 ["2019-12-10T14:11:53", "sollylun"],
                 ["2016-02-16T17:05:43", "ser_mart"]], 
"comment_likes":[["2019-11-28T18:05:04", "kerslyrikatel"], 
                 ["2019-11-28T18:05:03", "jakar"],
                 ["2019-12-10T14:11:53", "sollylun"]]}

我可以在value列中返回子数组,但不知道是谁将这些数组转换为'date'和'user'列:

   DECLARE @JSON1 NVARCHAR(MAX)
    SELECT @JSON1 = '{
                     "media_likes": [
                                     ["2019-12-10T17:49:02", "feonaleejones"], 
                                     ["2019-12-10T14:11:53", "sollyluna021"],
                                     ["2016-02-16T17:05:43", "serene_martin"]
                                    ], 
                      "comment_likes":[
                                      ["2019-11-28T18:05:04", "kerstinslyrikatelier"], 
                                      ["2019-11-28T18:05:03", "jan.kart"],
                                      ["2019-12-10T14:11:53", "sollyluna021"]
                                      ]
                      }'

SELECT [key],value 
  FROM OPENJSON(@JSON1,'$.media_likes') 

key value
0   ["2019-12-10T17:49:02", "feonaleejones"]
1   ["2019-12-10T14:11:53", "sollyluna021"]
2   ["2016-02-16T17:05:43", "serene_martin"]

如何在值上旋转两个集合数组?

约翰

1 个答案:

答案 0 :(得分:0)

select 
   DECLARE @JSON1 NVARCHAR(MAX)
    SELECT @JSON1 = '{
                     "media_likes": [
                                     ["2019-12-10T17:49:02", "feonaleejones"], 
                                     ["2019-12-10T14:11:53", "sollyluna021"],
                                     ["2016-02-16T17:05:43", "serene_martin"]
                                    ], 
                      "comment_likes":[
                                      ["2019-11-28T18:05:04", "kerstinslyrikatelier"], 
                                      ["2019-11-28T18:05:03", "jan.kart"],
                                      ["2019-12-10T14:11:53", "sollyluna021"]
                                      ]
                      }'

SELECT likedate,userlike 
  FROM OPENJSON(@JSON1,'$.media_likes') 
  WITH (
       likedate datetime '$[0]',
       userlike varchar(128) '$[1]'
       )