解析JSON数据以查找一个元素

时间:2019-01-25 17:16:31

标签: sql json parsing sql-server-2016

我正在筛选JSON电影数据库,并且可以毫无问题地获取大多数数据。对于每部电影,我都将原始JSON放在名为jsondata的nvarchar(max)列中,然后添加其他列以填充索引和快速检索目的。

使用我的UPDATE语句,我可以填充除一列之外的所有列,在这些列中我需要解析并找到一个元素。

这是JSON的示例:

hour_cound = [
    { 
        "hour": datetime,
        "count": 2
    }
]



# code

hours = [
    datetime(2019, 1, 25, 1),
    datetime(2019, 1, 25, 1),
    datetime(2019, 1, 25, 2),
    datetime(2019, 1, 25, 3),
    datetime(2019, 1, 25, 4),
    datetime(2019, 1, 25, 4)
]

existed = []
for hour in hours:
    if hour.hour not in existed:
        existed.append({
            "hour": hour.hour,
            "count": existed[hour.hour] + 1
        })

这是表模式:

{
"title": "Amnesia Love",
"genres": [{
    "id": 35,
    "name": "Comedy"
}],
"id": 508989,   
"original_language": "tl",
"overview": "A guy is trying to discover his true identity after being found unconscious in a remote island.",
"popularity": 2.583,
"release_date": "2018-02-28",   
"credits": {
    "cast": [{
        "cast_id": 2,
        "character": "",
        "credit_id": "5a9af6ecc3a3680b7d024627",
        "gender": 0,
        "id": 1230955,
        "name": "Paolo Ballesteros",
        "order": 1,
        "profile_path": "/8Cey11JPMWBCGuIcKBXvb6OQ7Je.jpg"
    },
    {
        "cast_id": 3,
        "character": "",
        "credit_id": "5a9af6f3c3a3680b57024465",
        "gender": 0,
        "id": 1166094,
        "name": "Yam Concepcion",
        "order": 2,
        "profile_path": "/fiPaXTkq440VHXDqlMRHtOpoLWT.jpg"
    }],
    "crew": [{
        "credit_id": "5a9af6cc0e0a260649024c6a",
        "department": "Directing",
        "gender": 0,
        "id": 1989658,
        "job": "Director",
        "name": "Albert Langitan",
        "profile_path": null
    },
    {
        "credit_id": "5a9af6dec3a3680b2d01f152",
        "department": "Writing",
        "gender": 0,
        "id": 1989658,
        "job": "Screenplay",
        "name": "Albert Langitan",
        "profile_path": null
    }]
},
}

以下是填充列的更新语句:

CREATE TABLE dbo.moviedb(
id int IDENTITY(1,1) NOT NULL,
jsondata nvarchar(max) NULL,
title nvarchar(200) NULL,
movie_id varchar(255) NULL,
original_language char(2) NULL,
overview nvarchar(1000) NULL,   
popularity float NULL,  
release_date datetime NULL, 
genre nvarchar(100) NULL,
director nvarchar(100)
)

我的问题是得到导演的名字。由于$ .credits中存在强制转换和乘员元素,并且每个元素中可能包含许多条目-我不清楚如何通过JSON_QUERY或使用CROSS APPLY来解析,基本上是说,“找到$ .credits.crew。 job =“ Director”,然后给我$ .credits.crew.name'。

1 个答案:

答案 0 :(得分:0)

好的,在评论之后,我想我已经明白了。我对导演使用OUTER APPLY,因为即使没有指定导演,我仍然想获取电影信息。这导致我想到另一个问题-电影中有不止一位导演。但我想我可以解决。

UPDATE t 
SET t.title = j.title, t.movie_id = j.id, t.original_language = j.original_language, 
t.overview = j.overview, t.popularity = j.popularity, t.release_date = j.release_date, 
t.genre = ISNULL(JSON_VALUE(t.jsondata,'$.genres[0].name'),''),
t.director = k.name
FROM tmdb t
  CROSS APPLY OPENJSON(jsondata)
WITH (title nvarchar(200), id int, original_language char(2), 
overview nvarchar(max), popularity float, release_date datetime, 
credits nvarchar(max) as JSON
) AS j
OUTER APPLY OPENJSON(j.credits,'$.crew') 
WITH (job nvarchar(50), name nvarchar(100)) AS k
WHERE k.job = 'Director' OR k.job IS NULL