如何处理包含json/数组数据的postgres文本列

如何处理包含json/数组数据的postgres文本列,json,postgresql,arraylist,Json,Postgresql,Arraylist,我有一个postgres(版本11)表,它保存车辆数据。数据从CSV文件填充。其中一个名为mot_history的字段是文本字段,有时为空或值为“无mot数据可用”,但通常如下所示: [{“completedDate”:“2019-05-08T13:38:21+00:00”,“expiryDate”:“2020-11-07T00:00:00+00:00”,“testNumber”:9999999999,“odomoter”:{“unit”:“mi”,“value”:89178},“passed”

我有一个postgres(版本11)表,它保存车辆数据。数据从CSV文件填充。其中一个名为mot_history的字段是文本字段,有时为空或值为“无mot数据可用”,但通常如下所示:

[{“completedDate”:“2019-05-08T13:38:21+00:00”,“expiryDate”:“2020-11-07T00:00:00+00:00”,“testNumber”:9999999999,“odomoter”:{“unit”:“mi”,“value”:89178},“passed”:true,“rfcAndComments”:[{“text”:“Nearside Non-responsible mirror discovered()”,“type”:“advice”}},{“completedDate”:“2019-04-29T08:39:45+00:00”,“expiryDate”:null,“testNumber”:999999999,“odomoter”:{“单位”:“mi”,“值”:88909},“通过”:false,“rfcAndComments”:[{“文本”:“左侧前轮胎的切口深度超过要求,足以达到帘布层或帘布层(5.2.3(d)(i)),“类型”:“失败”},{“文本”:”越位前轮胎切口深度超过要求,足以达到帘布层或帘线(5.2.3(d)(i)),“类型”:“不合格”},{“文本”:“左侧后轮胎切口深度超过要求,足以达到帘布层或帘线(5.2.3(d)(i)),“类型”:“不合格”},{“文本”:”左侧强制后视镜严重损坏,影响后视(3.3(b)(ii)),“类型”:“失败”},{“完成日期”:“2018-05-01T14:33:15+00:00”,“到期日期”:“2019-04-30T00:00:00+00:00”,“测试编号”:99999999,“ODOMOTOR”:{“单位”:“mi”,“值”:77880},“通过”:真,“rfcAndComments”:[“文本”:左侧强制后视镜损坏,但未严重影响后视(8.1.2a)”,“类型”:“警告”},{“文本”:“越位后顶升点丢失”,“类型”:“警告”}],{“完成日期”:“2017-04-26T08:41:07+00:00”,“到期日期”:“2018-04-26T00:00:00+00:00”,“测试编号”:99999999,“奥多莫特”:{“单位”:“mi”,“值”:66658}”,通过“:true,“rfcAndComments:”[{“text:”左侧非强制性后视镜损坏()”,“type:”咨询“},{“text:”挡风玻璃损坏的区域小于'a'区(8.3.1d)“,”type:”咨询“}]},{“completedDate:”2016-04-27T09:46:44+00:00“,”到期日期“:”2017-04-26T00:00+00:00”,“testNumber:”999999,“odomoter:”{“单位“:“mi”,“value”:54490},“passed”:true,“rfcAndComments”:[{“text”:“左侧强制后视镜损坏,但不会严重影响后视镜(8.1.2a)”,“type”:“advical”},{“text”:“前制动盘磨损、凹陷或刻痕,但不会严重减弱(3.5.1i)”,“type”:“advical”},{“text”:“制动警告灯点亮”,“type”:”咨询“}],{“completedDate”:“2015-04-20T10:45:28+00:00”,“expiryDate”:“2016-04-19T00:00:00+00:00”,“testNumber”:99999999,“odomoter”:{“unit”:“mi”,“value”:42143},“passed”:true,“rfcAndComments”:[{“文本”:“挡风玻璃对区域a”(8.3.1a)内小于10毫米的圆圈造成损坏”,“类型”:“咨询”,{“文本”:左侧强制后视镜严重损坏,影响后视镜,“,”类型“:”用户输入“},“{”文本“:”停车制动器在应用时发出噪音“,”类型“:”用户输入“}],”,“{”完成日期“:”2015-04-15T12:42:31+00:00”,“过期日期“:”null,“测试编号“:999999999”,“odomoter:“{”单位“:”mi“,”值“:41921}”,通过“:”假,“RFC注释“:[{”文本“:“左侧内前制动片厚度小于1.5 mm(3.5.1g)”,“类型”:“失效”},{“文本”:“左侧内后制动片厚度小于1.5 mm(3.5.1g)”,“类型”:“失效”},{“文本”:“挡风玻璃在区域‘a’(8.3.1a),“类型”:“建议”},{“文本”:”“左侧强制后视镜严重损坏,影响后视镜”,“类型”:“用户输入”},{“文本”:“驻车制动器在应用时发出噪音”,“类型”:“用户输入”},{“完成日期”:“2014-04-09T17:17:55+00:00”,“到期日期”:“2015-04-08T00:00:00+00:00”,“测试编号”:99999999,“odomoter”:{“单位”:“mi”,“值”:29857},“通过”:真,”RFC注释“:[{“文本”:“后轮胎磨损接近法定极限(4.1.E.1)”,“类型”:“建议”},{“文本”:“两个前轮胎内缘磨损”,“类型”:“用户输入”}]}]
[{“completedDate”:“2017-06-22T07:40:58+00:00”,“expiryDate”:“2018-07-01T00:00:00+00:00”,“testNumber”:9999999,“odomoter”:{“unit”:“mi”,“value”:23985},“passed”:true,“rfcAndComments”:[]}]
[{“completedDate”:“2020-03-05T12:11:13+00:00”,“ExpiredDate”:“2021-03-25T00:00+00:00”,“testNumber”:9999999,“odomoter”:{“unit”:“mi”,“value”:58804},“passed”:true,“rfcAndComments”:[},{“completedDate”:“2019-03-26T13:56:21+00:00”,“ExpiredDate”:“2020-03-25T00:00+00:00”,“testNumber”:999999,“Odor”:“unit”“mi”,“value”:“mi”,“value”:“:53674},“通过”:true,“rfcAndComments”:[],{“completedDate”:“2018-10-27T10:39:06+00:00”,“expiryDate”:“2019-10-29T00:00:00+00:00”,“testNumber”:99999999,“odomoter”:“{”单位“:“mi”,“value”:52580},”通过:true,“rfcAndComments”:[{“文本”:“后制动片磨损薄1.5毫米(1.1.13(a)(ii)),”类型:“,”咨询“,{”文本:”“左侧前轮胎轻微损坏/开裂或报废轮胎开裂。(5.2.3(d)(ii)),“类型”:“建议”},{“文本”:“越位前轮胎轻微损坏/开裂或报废轮胎开裂。(5.2.3(d)(ii)),“类型”:“建议”},{“文本”:“左侧前轮胎磨损接近法定极限/边缘磨损2.5 mm(5.2.3(e)),“类型”:“建议”},{“文本”:“越位前轮胎磨损接近法定极限/边缘磨损2.9 mm(5.2.3(e)),“类型”:“建议”},{“文本”:“近侧前外胎磨损接近法定极限/边缘磨损外缘。(5.2.3(e)),“类型”:“建议”}]
下面是稍微格式化一点的,让您更好地了解结构是什么

[
    {"completedDate": "2019-05-08T13:38:21+00:00", "expiryDate": "2020-11-07T00:00:00+00:00", "testNumber": 9999999999, "odomoter": {"unit": "mi", "value": 89178}, "passed": true, "rfcAndComments": 
        [
            {"text": "Nearside Non obligatory mirror damaged ()", "type": "ADVISORY"}
        ]
    }, 
    {"completedDate": "2019-04-29T08:39:45+00:00", "expiryDate": null, "testNumber": 999999999, "odomoter": {"unit": "mi", "value": 88909}, "passed": false, "rfcAndComments": [{"text": "Nearside Front Tyre has a cut in excess of the requirements deep enough to reach the ply or cords (5.2.3 (d) (i))", "type": "FAIL"}, {"text": "Offside Front Tyre has a cut in excess of the requirements deep enough to reach the ply or cords (5.2.3 (d) (i))", "type": "FAIL"}, {"text": "Nearside Rear Tyre has a cut in excess of the requirements deep enough to reach the ply or cords (5.2.3 (d) (i))", "type": "FAIL"}, {"text": "Nearside Obligatory mirror seriously damaged, affecting the rear view (3.3 (b) (ii))", "type": "FAIL"}]}, 
    {"completedDate": "2018-05-01T14:33:15+00:00", "expiryDate": "2019-04-30T00:00:00+00:00", "testNumber": 999999999, "odomoter": {"unit": "mi", "value": 77880}, "passed": true, "rfcAndComments": [{"text": "Nearside Obligatory mirror damaged, but not seriously affecting the rear view (8.1.2a)", "type": "ADVISORY"}, {"text": "Offside Rear  jacking  point  missing", "type": "ADVISORY"}]}, 
    {"completedDate": "2017-04-26T08:41:07+00:00", "expiryDate": "2018-04-26T00:00:00+00:00", "testNumber": 999999999, "odomoter": {"unit": "mi", "value": 66658}, "passed": true, "rfcAndComments": [{"text": "Nearside Non obligatory mirror damaged ()", "type": "ADVISORY"}, {"text": "Windscreen has damage to an area less than a 40mm circle outside zone 'A' (8.3.1d)", "type": "ADVISORY"}]}, 
    {"completedDate": "2016-04-27T09:46:44+00:00", "expiryDate": "2017-04-26T00:00:00+00:00", "testNumber": 999999999, "odomoter": {"unit": "mi", "value": 54490}, "passed": true, "rfcAndComments": [{"text": "Nearside Obligatory mirror damaged, but not seriously affecting the rear view (8.1.2a)", "type": "ADVISORY"}, {"text": "Front brake disc worn, pitted or scored, but not seriously weakened (3.5.1i)", "type": "ADVISORY"}, {"text": "brake warning lamp illuminated", "type": "ADVISORY"}]}, 
    {"completedDate": "2015-04-20T10:45:28+00:00", "expiryDate": "2016-04-19T00:00:00+00:00", "testNumber": 999999999, "odomoter": {"unit": "mi", "value": 42143}, "passed": true, "rfcAndComments": [{"text": "Windscreen has damage to an area less than a 10mm circle within zone 'A' (8.3.1a)", "type": "ADVISORY"}, {"text": "nearside obligatory mirror seriously damaged, affecting the rear view", "type": "USER ENTERED"}, {"text": "parking brake noisy on application", "type": "USER ENTERED"}]}, 
    {"completedDate": "2015-04-15T12:42:31+00:00", "expiryDate": null, "testNumber": 999999999, "odomoter": {"unit": "mi", "value": 41921}, "passed": false, "rfcAndComments": [{"text": "Nearside Inner Front Brake pad(s) less than 1.5 mm thick (3.5.1g)", "type": "FAIL"}, {"text": "Nearside Inner Rear Brake pad(s) less than 1.5 mm thick (3.5.1g)", "type": "FAIL"}, {"text": "Windscreen has damage to an area less than a 10mm circle within zone 'A' (8.3.1a)", "type": "ADVISORY"}, {"text": "nearside obligatory mirror seriously damaged, affecting the rear view", "type": "USER ENTERED"}, {"text": "parking brake noisy on application", "type": "USER ENTERED"}]}, 
    {"completedDate": "2014-04-09T17:17:55+00:00", "expiryDate": "2015-04-08T00:00:00+00:00", "testNumber": 999999999, "odomoter": {"unit": "mi", "value": 29857}, "passed": true, "rfcAndComments": [{"text": "Rear Tyre worn close to the legal limit (4.1.E.1)", "type": "ADVISORY"}, {"text": "both front tyres worn on inner edges", "type": "USER ENTERED"}]}
]
我无法控制外部输入数据的格式,但我想知道是否可以使用SQL获取存储在这个类似JSON/数组的结构中的值。例如,我想返回一辆通过MOT的车辆的有效期。我知道我可以通过
with data as
(
    select mot_history::jsonb as x 
    from my_table where length(mot_history) > 50

)   
select item_object->'passed',item_object->'expiryDate',position from data,jsonb_array_elements(x) with ordinality arr(item_object, position) 
where item_object->'passed' = 'true'