PostgreSQL - 查询 JSONB 数据中键的第 N 个实例的值
PostgreSQL - query the value for Nth instance of key in JSONB data
很抱歉这里的格式。文本太多,无法创建可读性 table,请耐心等待。
我有一个叫 'declines' 的 table。它有两列:'id' 和 'dec_reasons'.
对于 id = 34254,这是 JSONB 格式的 'dec_reasons' 值:
[
{
"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 = 34257,这是 JSONB 格式的 'dec_reasons' 值:
[
{
"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' 值中,'reason' 键有多个实例。如何在每个 'dec_reasons' 值中查询与 'reason' 的第一个实例配对的值?
如果我查询 'reason' 键的第一个实例,我希望看到:
'Lead Fico Threshold is not Greater Than Or Equal To 500'
如果我查询 'reason' 键的第二个实例,我希望看到:
'Fico score is very low'
要在 jsonb 结构中查询,您可以使用 ->
向下钻取。这会在第二个条目中获取 key
的值。
# 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)
请参阅 Postgres 文档中的 json and jsonb Operators。
很抱歉这里的格式。文本太多,无法创建可读性 table,请耐心等待。
我有一个叫 'declines' 的 table。它有两列:'id' 和 'dec_reasons'.
对于 id = 34254,这是 JSONB 格式的 'dec_reasons' 值:
[
{
"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 = 34257,这是 JSONB 格式的 'dec_reasons' 值:
[
{
"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' 值中,'reason' 键有多个实例。如何在每个 'dec_reasons' 值中查询与 'reason' 的第一个实例配对的值?
如果我查询 'reason' 键的第一个实例,我希望看到:
'Lead Fico Threshold is not Greater Than Or Equal To 500'
如果我查询 'reason' 键的第二个实例,我希望看到:
'Fico score is very low'
要在 jsonb 结构中查询,您可以使用 ->
向下钻取。这会在第二个条目中获取 key
的值。
# 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)
请参阅 Postgres 文档中的 json and jsonb Operators。