在postgres 9.3

时间:2015-12-09 18:14:12

标签: json postgresql psql

我已经阅读了有关json列的问题,显然只是不理解。

以下是使用json列的postgresql查询的摘录我的问题是:

我可以查询顶级数据(referralReasons) 但是当我尝试从较低级别查询数据时(TASCReferral ....“referralComments”我得到空白结果 我意识到我没有正确地编写查询,但是我找不到可理解的指令。

感谢您的帮助

下面是json供参考

--------------查询---------------------------

 SELECT 
  groups.name AS group, 
  (to_char (referrals."referraldate", 'MM/DD/YY'))AS DateReferred,
  referrals."referral_age",
  referrals."name" AS Offender,
  referrals.po_name AS Officer,
 -- referrals.jsondata->'referralReasons' AS po_comment,
 referrals.jsondata->'referralComments' AS po_comment,
 referrals.visibility_flag

FROM
groups, referrals
WHERE .........

--------- json数据来自referrals.jsondata专栏---------

{
    "pendingCharges": [],
    "residence": {
        "zip": "12345",
        "phone": "(252)555-1212",
        "county": "BEAUFORT",
        "street": "1313 MOCKING BIRD LN",
        "state": "NC",
        "city": "SOMETOWN"
    },
    "dates": {
        "referralDate": "2015-12-08",
        "convictionDate": "2011-10-20",
        "birthDate": "1909-09-16"
    },
    "html": {
        "zrud_group": "TEST",
        "zzud_referral": "15AC82D5-E743-40A2-8553-ER765DKOPIT11"
    },
    "TASCReferral": {
        "referralKey": "ABCD-EFG-HITK",
        "phone": {},
        "charges": "LARCENY",
        "conviction": "019 years 11 months 10 days",
        "officerName": "BUTLER, SMEDLEY",
        "DrugTestSummaryLink": "https.GOHERE.COM",
        "referralComments": "Appointment Date: Week of 12/08/2015   -- This offender is an out of state case.  He tested positive today for cocaine.",
        "officerEmail": "MEANJOE@PROBATION.COM",
        "supervisionLevel": "L3",
        "gangName": " ",
        "RNASummaryLink": "https://GOTHERE.COM",
        "punishmentType": "NON-N.C. OFF.",
        "OPUSID": "9876543",
        "docketNumber": "CR00000000",
        "officerPhone": "(252)666-8888",
        "name": "CLIENT TEST",
        "age": "18",
        "gangAffiliated": "false",
        "countyOfConviction": "OTHER",
        "gender": "Male",
        "supervisionType": "PRO: PROBATION",
        "TASCOffice": "TESTOFFICE"
    },
    "referralReasons": [
        "Court-Ordered Assessment",
        "Baseline Admission or Positive Drug Screen",
        "Offender admits to using/positive for: cocaine - Date of last use: 10/09/2015"
    ]

1 个答案:

答案 0 :(得分:1)

假设您的data列包含JSON,您可以使用->(获取JSON对象字段)和->>(获取JSON字段作为TEXT)来遍历json结构,相关文档为here

SELECT jt.data->'TASCReferral'->>'referralComments' 
FROM json_test jt;