PostgreSQL-查询JSONB数据中键的第n个实例的值

PostgreSQL-查询JSONB数据中键的第n个实例的值,postgresql,jsonb,Postgresql,Jsonb,很抱歉,此页的格式设置错误。有太多的文字,无法创建一个可读的表格,所以请容忍我 我有一张叫做“谢绝”的桌子。它有两列:“id”和“dec_原因” 对于id=34254,这是JSONB格式的“dec_Reasions”值: [ { "id": 94748, "reason": "Lead Fico Threshold is not Greater Than Or Equal To 500", "created_at": "2019-05-02

很抱歉,此页的格式设置错误。有太多的文字,无法创建一个可读的表格,所以请容忍我

我有一张叫做“谢绝”的桌子。它有两列:“id”和“dec_原因”

对于id=34254,这是JSONB格式的“dec_Reasions”值:

[
    {
        "id": 94748,
        "reason": "Lead Fico Threshold is not Greater Than Or Equal To 500",
        "created_at": "2019-05-02T07:57:59.706448",
        "decline_code": "fico_too_low",
        "leaf_node_id": 7,
        "decision_type": "credit",
        "triggers_noaa": true,
        "general_description": "FICO score is too low"
    },
    {
        "id": 94747,
        "reason": "Fico score is very low",
        "created_at": "2019-05-02T07:57:59.705578",
        "decline_code": "fico_too_low",
        "leaf_node_id": 5,
        "decision_type": "credit",
        "triggers_noaa": true,
        "general_description": "FICO score is too low"
    }
]
[
    {
        "id": 94772,
        "reason": "Lead Fico Threshold is not Greater Than Or Equal To 500",
        "created_at": "2019-05-02T07:58:05.988900",
        "decline_code": "fico_too_low",
        "leaf_node_id": 7,
        "decision_type": "credit",
        "triggers_noaa": true,
        "general_description": "FICO score is too low"
    },
    {
        "id": 94771,
        "reason": "Fico score is very low",
        "created_at": "2019-05-02T07:58:05.964931",
        "decline_code": "fico_too_low",
        "leaf_node_id": 5,
        "decision_type": "credit",
        "triggers_noaa": true,
        "general_description": "FICO score is too low"
    }
]
对于id=34257,这是JSONB格式的“dec_Reasions”值:

[
    {
        "id": 94748,
        "reason": "Lead Fico Threshold is not Greater Than Or Equal To 500",
        "created_at": "2019-05-02T07:57:59.706448",
        "decline_code": "fico_too_low",
        "leaf_node_id": 7,
        "decision_type": "credit",
        "triggers_noaa": true,
        "general_description": "FICO score is too low"
    },
    {
        "id": 94747,
        "reason": "Fico score is very low",
        "created_at": "2019-05-02T07:57:59.705578",
        "decline_code": "fico_too_low",
        "leaf_node_id": 5,
        "decision_type": "credit",
        "triggers_noaa": true,
        "general_description": "FICO score is too low"
    }
]
[
    {
        "id": 94772,
        "reason": "Lead Fico Threshold is not Greater Than Or Equal To 500",
        "created_at": "2019-05-02T07:58:05.988900",
        "decline_code": "fico_too_low",
        "leaf_node_id": 7,
        "decision_type": "credit",
        "triggers_noaa": true,
        "general_description": "FICO score is too low"
    },
    {
        "id": 94771,
        "reason": "Fico score is very low",
        "created_at": "2019-05-02T07:58:05.964931",
        "decline_code": "fico_too_low",
        "leaf_node_id": 5,
        "decision_type": "credit",
        "triggers_noaa": true,
        "general_description": "FICO score is too low"
    }
]
在每个'dec_reasons'值中,都有'reasons'键的多个实例。如何在每个“dec_reasons”值中查询与第一个“reason”实例配对的值

如果我查询了'reason'键的第一个实例,我希望看到:

'Lead Fico Threshold is not Greater Than Or Equal To 500'
'Fico score is very low'
如果我查询'reason'键的第二个实例,我希望看到:

'Lead Fico Threshold is not Greater Than Or Equal To 500'
'Fico score is very low'

要在jsonb结构中进行查询,您可以使用
->
向下搜索。这将获取第二个条目中的
键的值

# select '[{"key": 10},{"key": 20},{"key": 30}]'::jsonb->1->'key';
 ?column? 
----------
 20
(1 row)
或者您可以使用
#>
直接查询路径

# select '[{"key": 10},{"key": "20"},{"key": 30}]'::jsonb#>'{1,"key"}';
 ?column? 
----------
 "20"
(1 row)
请参阅博士后文档