访问postgreSQL中的子json元素

时间:2019-02-28 05:51:40

标签: postgresql

我有一个带有jsonb列的表,该表返回我以下对象。

[
  {
    "ele_llist": [
      726,
      144,
      976,
      144,
      976,
      225,
      726,
      225
    ],
    "sgtc": 1,
    "text_detail_list": {
      "alt_ele_list": [
        [
          726,
          203,
          833,
          203,
          833,
          224,
          726,
          223
        ],
        [
          786,
          144,
          917,
          144,
          917,
          182,
          786,
          182
        ],
        [
          835,
          210,
          972,
          211,
          972,
          225,
          835,
          224
        ],
        [
          842,
          182,
          976,
          182,
          976,
          205,
          842,
          205
        ]
      ],
      "sfc": [
        0,
        0,
        0,
        0
      ],
      "sgtc": [
        1,
        1,
        1,
        1
      ],
      "pname": [
        "GOLDEN oy",
        "gallne",
        "OSETTY EESTAUEA",
        "AIsoR"
      ],
      "poidc": [
        0.9,
        0.9,
        0.9,
        0.9
      ]
    },
    "sfc": 0,
    "did": "hC5qbBF2IO0.2018-11-13_17-14-19.0",
    "poidc": 0.9,
    "pname": "GOLDEN oy gallne OSETTY EESTAUEA AIsoR"
  },
  {
    "ele_llist": [
      543,
      26,
      697,
      26,
      697,
      57,
      543,
      57
    ],
    "sgtc": 1,
    "text_detail_list": {
      "alt_ele_list": [
        [
          543,
          34,
          696,
          26,
          697,
          49,
          544,
          57
        ]
      ],
      "sfc": [
        0
      ],
      "sgtc": [
        1
      ],
      "pname": [
        "mloachomgrprgren"
      ],
      "poidc": [
        0.45
      ]
    },
    "sfc": 0,
    "did": "hC5qbBF2IO0.2018-11-13_17-14-19.0",
    "poidc": 0.45,
    "pname": "mloachomgrprgren"
  },
  {
    "ele_llist": [
      1037,
      96,
      1127,
      96,
      1127,
      128,
      1037,
      128
    ],
    "sgtc": 1,
    "text_detail_list": {
      "alt_ele_list": [
        [
          1037,
          96,
          1127,
          96,
          1127,
          128,
          1037,
          128
        ]
      ],
      "sfc": [
        0
      ],
      "sgtc": [
        1
      ],
      "pname": [
        "LD"
      ],
      "poidc": [
        0.45
      ]
    },
    "sfc": 0,
    "did": "hC5qbBF2IO0.2018-11-13_17-14-19.1",
    "poidc": 0.45,
    "pname": "LD"
  },
  {
    "ele_llist": [
      538,
      25,
      699,
      25,
      699,
      58,
      538,
      58
    ],
    "sgtc": 1,
    "text_detail_list": {
      "alt_ele_list": [
        [
          538,
          34,
          698,
          25,
          699,
          49,
          539,
          58
        ]
      ],
      "sfc": [
        0
      ],
      "sgtc": [
        1
      ],
      "pname": [
        "a@lokomgrergimi"
      ],
      "poidc": [
        0.45
      ]
    },
    "sfc": 0,
    "did": "hC5qbBF2IO0.2018-11-13_17-14-19.1",
    "poidc": 0.45,
    "pname": "a@lokomgrergimi"
  },
  {
    "ele_llist": [
      729,
      145,
      993,
      145,
      993,
      229,
      729,
      229
    ],
    "sgtc": 1,
    "text_detail_list": {
      "alt_ele_list": [
        [
          729,
          202,
          993,
          209,
          993,
          229,
          729,
          223
        ],
        [
          843,
          183,
          980,
          184,
          980,
          207,
          843,
          206
        ],
        [
          792,
          146,
          918,
          145,
          918,
          178,
          792,
          179
        ]
      ],
      "sfc": [
        0,
        0,
        0
      ],
      "sgtc": [
        1,
        1,
        1
      ],
      "pname": [
        "GOLDON ory OSETTY ESTAUEANT",
        "AIENISORSSA",
        "gal"
      ],
      "poidc": [
        0.9,
        0.9,
        0.9
      ]
    },
    "sfc": 0,
    "did": "hC5qbBF2IO0.2018-11-13_17-14-19.1",
    "poidc": 0.9,
    "pname": "GOLDON ory OSETTY ESTAUEANT AIENISORSSA gal"
  }
]

我想访问内部text_detail_list,我可以使用以下查询来访问它

select obj->'text_detail_list'->'pname'->0 as "pname" from 
ele_tbl, json_array_elements(ele_json) obj

在这里,我需要以数字形式提供0、1、2..。是否可以通过其他方法不使用数字计数器来获取子元素?

1 个答案:

答案 0 :(得分:0)

是的,您可以在提取的jsonb数组上再次应用func saveToCloud(record: String, recordType: String, recordTypeField: String, reference: CKRecord?, referenceType: String?) { let zoneID = CKRecordZone.ID(zoneName: Zone.test, ownerName: CKRecordZone.ID.default.ownerName) let recordID = CKRecord.ID(recordName: UUID().uuidString, zoneID: zoneID) let newRecord = CKRecord(recordType: recordType, recordID: recordID) if let reference = reference, let referenceType = referenceType { let newReference = CKRecord.Reference(record: reference, action: .none) newRecord[referenceType] = newReference } newRecord[recordTypeField] = record database.save(newRecord) { (_,error) in if let err = error as? CKError { print("ERROR =" , err.userInfo ) } } }

jsonb_array_elements_text

输出

select jsonb_array_elements_text(obj->'text_detail_list'->'pname') 
  as pname
  from ele_tbl cross join lateral
jsonb_array_elements(ele_json) obj

如果要为每个提取的组分配ID,请在 pname | :-------------------------- | GOLDEN oy | gallne | OSETTY EESTAUEA | AIsoR | mloachomgrprgren | LD | a@lokomgrergimi | GOLDON ory OSETTY ESTAUEANT | AIENISORSSA | gal | 子句中使用with ordinality

FROM

输出

select j.id,j.elem FROM
(
select obj->'text_detail_list'->'pname' as pname_array
  from ele_tbl cross join lateral
jsonb_array_elements(ele_json) obj
)s cross join lateral jsonb_array_elements_text(pname_array)
 with ordinality as j(id,elem)

Demo