【问题标题】:How to query Json field so the keys are the column names and the values are the column fields如何查询 Json 字段,以便键是列名,值是列字段
【发布时间】:2021-05-27 10:01:22
【问题描述】:

PostgreSQL 数据库名称为contacts_contact,我感兴趣的列名为“fields”

“字段”列在我的数据库中如下所示:

{
   "Monthly_household_income":{
      "text":"< $1500"
   },
   "Survey_consent":{
      "text":"Yes"
   },
   "Are_you_working":{
      "text":"No"
   },
   "Network_Provider":{
      "text":"Vodacom"
   },
   "Do_you_own_a_phone":{
      "text":"Family Member"
   },
   "Whatsapp_consent":{
      "text":"TRUE"
   },
   "Highest_education":{
      "text":"< 12"
   },
   "Preferred_channel":{
      "text":"SMS"
   },
   "Identification_type":{
      "text":"sa_id"
   },
   "7d91a89f-6cf7-41e3-817e-718caab32b38":{
      "text":"+27729535770",
      "number":2777777777
   },
   "e26acde6-87b3-43eb-bc8d-68df3747e206":{
      "text":"Single"
   },
   "What_would_make_you_stay_on_whatsapp":{
      "text":"Free-Wifi"
   },
   "Why_did_you_switch_from_wa_to_sms":{
      "text":"Cost"
   },
   "How_do_you_charge_your_phone":{
      "text":"Electricity"
   },
   "Prebirth_messaging":{
      "text":"1",
      "number":1
   },
   "Completed_research_survey":{
      "text":"TRUE"
   },
   "Estimated_due_date":{
      "text":"2021-06-07T00:00:00Z",
      "datetime":"2021-06-07T00:00:00.000000Z"
   },
   "Started_research_survey":{
      "text":"TRUE"
   },
   "Whatsapp_undelivered_timestamp":{
      "text":"2020-11-11T15:07:28.925133+00:00",
      "datetime":"2020-11-11T17:07:28.925133+02:00"
   },
   "Info_consent":{
      "text":"TRUE"
   },
   "How_much_airtime_do_you_use_for_momconnect_on_whatsapp":{
      "text":"<R0"
   },
   "Facility_code":{
      "text":"328642",
      "number":328642
   },
   "What_type_of_house_do_you_live_in":{
      "text":"Government Housing"
   },
   "ID_number":{
      "text":"9999999999",
      "number":999999999
   },
   "Income_from_grants":{
      "text":"Childcare"
   },
   "Date_of_birth":{
      "text":"1994-04-14T00:00:00Z",
      "datetime":"1994-04-14T00:00:00.000000Z"
   },
   "Messaging_consent":{
      "text":"TRUE"
   },
   "Research_consent":{
      "text":"TRUE"
   },
   "Registration_date":{
      "text":"2020-10-28T10:48:06.771820+02:00",
      "datetime":"2020-10-28T10:48:06.771820+02:00"
   }
}

还有第二条记录:

 {
   "Monthly_household_income":{
      "text":"< $2000"
   },
   "Survey_consent":{
      "text":"Yes"
   },
   "Are_you_working":{
      "text":"No"
   },
   "Network_Provider":{
      "text":"MTN"
   },
   "Do_you_own_a_phone":{
      "text":"Family Member"
   },
   "Whatsapp_consent":{
      "text":"TRUE"
   },
   "Highest_education":{
      "text":"< 12"
   },
   "Preferred_channel":{
      "text":"SMS"
   },
   "Identification_type":{
      "text":"sa_id"
   },
   "7d91a89f-6cf7-41e3-817e-718caab32b38":{
      "text":"+27729535770",
      "number":2777777777
   },
   "e26acde6-87b3-43eb-bc8d-68df3747e206":{
      "text":"Single"
   },
   "What_would_make_you_stay_on_whatsapp":{
      "text":"Free-Wifi"
   },
   "Why_did_you_switch_from_wa_to_sms":{
      "text":"Cost"
   },
   "How_do_you_charge_your_phone":{
      "text":"Electricity"
   },
   "Prebirth_messaging":{
      "text":"1",
      "number":1
   },
   "Completed_research_survey":{
      "text":"TRUE"
   },
   "Estimated_due_date":{
      "text":"2021-06-07T00:00:00Z",
      "datetime":"2021-06-07T00:00:00.000000Z"
   },
   "Started_research_survey":{
      "text":"TRUE"
   },
   "Whatsapp_undelivered_timestamp":{
      "text":"2020-11-11T15:07:28.925133+00:00",
      "datetime":"2020-11-11T17:07:28.925133+02:00"
   },
   "Info_consent":{
      "text":"TRUE"
   },
   "How_much_airtime_do_you_use_for_momconnect_on_whatsapp":{
      "text":"<R0"
   },
   "Facility_code":{
      "text":"328642",
      "number":328642
   },
   "What_type_of_house_do_you_live_in":{
      "text":"Government Housing"
   },
   "ID_number":{
      "text":"9999999999",
      "number":999999999
   },
   "Income_from_grants":{
      "text":"Childcare"
   },
   "Date_of_birth":{
      "text":"1994-04-14T00:00:00Z",
      "datetime":"1994-04-14T00:00:00.000000Z"
   },
   "Messaging_consent":{
      "text":"TRUE"
   },
   "Research_consent":{
      "text":"TRUE"
   },
   "Registration_date":{
      "text":"2020-10-28T10:48:06.771820+02:00",
      "datetime":"2020-10-28T10:48:06.771820+02:00"
   }
}

“字段”列的字段看起来像我共享的两个 JSON 数据。这几乎是对一项调查的回应。例如,{u'Monthly_household_income': {u'text': u'

我希望查询输出中每个问题都有一列,回复作为字段。

应该是这样的:

Monthly_household_income | Survey_consent | Are you working | Network provider as so on as columns
$1500                    | Yes            | No              | Vodacom          
$2000                    | Yes            | No              | MTN

【问题讨论】:

  • @a_horse_with_no_name 很抱歉。我在其他地方处理过它,所以它有一些额外的东西。它现在是一个有效的 json。

标签: sql json postgresql


【解决方案1】:

由于实际值嵌套在另一个键中,最简单的方法是使用#&gt;&gt; 运算符:

select fields #>> '{Monthly_household_income, text}' as Monthly_household_income,
       fields #>> '{Survey_consent, text}'  as Survey_consent
       fields #>> '{Are_you_working, text}' as Are_you_working
       ....
from the_table;

如果您不想一直输入,请创建一个执行此操作的视图。

【讨论】:

  • 感谢@a_horse_with_no_name。它解决了我的问题。
猜你喜欢
  • 2011-10-09
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 2016-09-19
  • 2022-10-01
  • 2020-06-18
相关资源
最近更新 更多