简体   繁体   English

如何在多个 JSON 中找到多个键并仅返回那些在 postgresql9.5 中具有某些值的行

[英]How to find the multiple keys in multiple JSON and Return only those rows that are having some value in postgresql9.5

I have table with multiple JSON rows.我有多个 JSON 行的表。 In this JSON structure stored different different key value like as:在这个 JSON 结构中存储了不同的键值,例如:

"official_form_attributes":{"81459" : "Y", "81460" : " ", "81293" : "1~Yes", "80985" : " ", "80953" : "1", "80952" : " ", "80951" : "8~Forward", "81291" : "1~Yes", "81295" : "1~Yes", "81294" : "1~Yes", "80986" : "1~PRED", "81292" : "1~Yes", "80954" : "4", "80950" : " "}
"official_form_attributes":{"81321" : "6", "81315" : "15/06/2020", "81364" : "Approved", "81320" : "100000", "81466" : " ", "81314" : "1~Pucca", "80958" : "9~Forward to Tahsildar", "81318" : "20", "81325" : "20", "81465" : "Y", "81322" : "20000", "81324" : "1~Partially Damaged", "81323" : "20000", "81317" : "30", "81326" : "5200", "81319" : "600", "81316" : " "}
"official_form_attributes":{"82817" : " ", "82818" : " ", "82835" : "4", "81486" : "1~Yes", "82855" : "4", "83240" : "29/10950004/2020/07/09/29006271/10950004_9356416_3914_1594303859111.pdf", "81487" : " ", "80963" : "approved", "81488" : "5200", "80962" : "11~Approve by Tahsildar"}.

I have to find the key in this table.我必须在这张表中找到钥匙。 the result of that query is return all rows with some value and null value regarding this key.该查询的结果是返回具有某个值的所有行和关于此键的 null 值。 But my requirement is that return only those rows which have some value that key.但我的要求是只返回那些具有该键值的行。

CASE 1 MY query案例 1 我的查询

select application_id, current_process_id, processing_json->'official_form_attributes'->'81488' 
from schm_ka.processing_data_json 
where application_id = 9356416;

Result:结果:

applid      keyvalue
9356416     ""
9356416     ""
9356416     "5200"

But I need only this但我只需要这个

applid      keyvalue
9356416     "5200"

CASE 2案例二

select application_id, current_process_id, processing_json->'official_form_attributes'->'81488',processing_json->'official_form_attributes'->'81315' 
from schm_ka.processing_data_json 
where application_id = 9356416;

Result结果

applid  key1value   key2value
9356416     ""          ""
9356416     ""          "15/06/2020"
9356416     "5200"      ""

But I need only this但我只需要这个

applid      key1value   key2value
9356416     "5200"      "15/06/2020"

How to do this?这该怎么做?

For case 1, just put it into the WHERE clause:对于第一种情况,只需将其放入 WHERE 子句中即可:

select application_id, 
       processing_json->'official_form_attributes' ->> '81488' 
from schm_ka.processing_data_json 
where application_id = 9356416
  and processing_json -> 'official_form_attributes' ? '81488';

The ?? operator tests if a key is present in the JSON value 运算符测试 JSON 值中是否存在键

For case 2 you need aggregation:对于案例 2,您需要聚合:

select application_id, 
       max(processing_json->'official_form_attributes'->>'81488') as key_value_1,
       max(processing_json->'official_form_attributes'->>'81315') as key_value_2 
from schm_ka.processing_data_json 
where application_id = 9356416
  and processing_json->'official_form_attributes' ?| array['81488', '81315']
group by application_id;

The ?| ?| operator tests if any of the provided keys in the array is present in the JSON value. 运算符测试数组中提供的任何键是否存在于 JSON 值中。 As you apparently get multiple rows with that condition, the aggregation is needed to collapse them into one row.当您显然在该条件下获得多行时,需要聚合才能将它们折叠成一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM