json - 在 Postgres 中从 JSON 获取嵌套对象值

标签 json postgresql object

这是我的 Postgres 数据库中的 JSON 列:

{
  "objekt_art": {
    "86": {
      "code": "86",
      "bezeichnung_de": "Kino",
      "bezeichnung_fr": "Cinéma",       
      "bezeichnung_it": "Cinema",
      "bezeichnung_en": null,
      "kurz_bezeichnung_de": "Kino",
      "relevant_fuer_berechnung_steuerquote": true
    },
    "27": {
      "code": "27",
      "bezeichnung_de": "Kiosk",
      "bezeichnung_fr": "Kiosque",
      "bezeichnung_it": "Chiosco",
      "bezeichnung_en": null,
      "kurz_bezeichnung_de": "Kiosk",
      "relevant_fuer_berechnung_steuerquote": true
    }
  }
}

我需要能够查询 bezechnung_de,例如代码 = 86。 我可以从另一个表传递的代码数。

例如,我如何使用两列进行查询。第一个是数字,第二个是 bezeichnung_de。

像这样:

Code Bez
86   Kino

最佳答案

连接数据的示例数据结构和示例表:dbfiddle

select
  je.value -> 'code' as "Code",
  je.value -> 'bezeichnung_de' as "Bez"
from
  test t
  cross join jsonb_each((data::jsonb ->> 'objekt_art')::jsonb) je
  -- In table test_join I insert value 86 for join record
  inner join test_join tj on je.key::int = tj.json_id

关于json - 在 Postgres 中从 JSON 获取嵌套对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69820653/

相关文章:

尝试从双链表打印数据时 C++ 程序崩溃

Php 不能使用对象 stdClass 作为数组

postgresql - TimescaleDB/PostgreSQL : how to use unique constraint when creating hypertables?

c - "cacheline aligned"是什么意思?

php - 在 php7-fpm Docker 容器中安装 Postgres 驱动程序

javascript - 奇怪的是,window.document 没有返回理想情况下应该返回的正确对象。为什么?

c# - 将具有对象列表的 json 绑定(bind)到模型

javascript - 使用 jquery 读取嵌套 json 返回未定义

java - java中如何将json数据转换为字符串

java - 将一个对象的 ArrayList 内容传输到另一个对象