首页 > 解决方案 > 如何使用 sqlite json-extract 提取嵌套的 json

问题描述

如何使用 sqlite json-extract 或其他 sqlite json 命令提取嵌套的 json?

在这里我想提取given_id

"invoices": [{

........


    "items": [{


    "given_id": "TBC0003B",


    ...


        }


    ]

   }

]

谢谢。

标签: jsonsqlitenestedjson-extractsqlite-json1

解决方案


在 SQLite 中,您可以json_extract()按如下方式使用:

select json_extract(my_json_col, '$.invoices[0].items[0].given_id') my_given_id from mytable

这为您提供了数组第一个元素下数组第一个元素的given_id属性。itemsinvoices

DB Fiddle 上的演示

with mytable as (select '{
    "invoices": [{
        "items": [{ "given_id": "TBC0003B" }] 
    }]
}' my_json_col)
select json_extract(my_json_col, '$.invoices[0].items[0].given_id') my_given_id from mytable
| my_given_id |
| :------------ |
| TBC0003B |

推荐阅读