簡體   English   中英

如何從postgresql中的jsonb object獲取多個鍵值對?

[英]How to get the multiple key value pairs from jsonb object in postgresql?

我在表響應中有列 response_payload 如下

{"studentName":"Karate","studentStream":"API Testing", "studentAge":18}

現在我需要根據我的密鑰獲取新的 json object。 例如,如果我的鍵是 studentName 和 studentStream 我必須得到

{"studentName":"Karate","studentStream":"API Testing"}

如果我提供無效密鑰,它不應該帶來與無效密鑰相關的任何內容。

我在 postgresql 中嘗試了 json_build_object 內置 function,它帶來了無效的鍵和值,如下所示

select json_build_object('invalidKey', payload -> 'invalidValue') 
from response

O/P:- {"invalidKey":"null"}

我想要 output 當提供無效的鍵和值時,它不應該帶來如下任何東西

運營商:- {}

您可以使用jsonb_strip_nulls

select 
  jsonb_strip_nulls(jsonb_build_object('invalidKey', payload -> 'invalidValue')) 
from response

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM