Java postgresql查询where子句中的json子元素或postgresql中json_exists()oracle函数的替代项

Java postgresql查询where子句中的json子元素或postgresql中json_exists()oracle函数的替代项,java,json,oracle,postgresql,Java,Json,Oracle,Postgresql,我在postgres json列中存储了一些类似于下面json的json。我必须准备json上的where条件,比如类型为mobile的entrySource是否等于true,timeSheetEntry对象是否存在。 Json示例: [ { "calculationMethodID": "TimeEntryCommon", "inputs": { "defaultStartTime": "08:00", "supportedEntryTypes":

我在postgres json列中存储了一些类似于下面json的json。我必须准备json上的where条件,比如类型为mobile的entrySource是否等于true,timeSheetEntry对象是否存在。

Json示例:

[
  {
    "calculationMethodID": "TimeEntryCommon",
    "inputs": {
      "defaultStartTime": "08:00",
      "supportedEntryTypes": {
        "timeSheetEntry": {
          "type": "TotalHours"
        }
      },
      "maxShift": {
        "breakDuration": "PT2H",
        "length": "PT14H"
      }
    }
  }
]

[
  {
    "calculationMethodID": "TimeEntryCommon",
    "inputs": {
      "defaultStartTime": "08:00",
      "supportedEntryTypes": {
        "punchEntry": {
          "entryCodes": [
            "In",
            "TakeMeal",
            "Out"
          ],
          "entrySource": {
            "mobile": true,
            "clock": true,
            "ivr": false,
            "web": true
          }
        }
      },
      "maxShift": {
        "breakDuration": "PT2H",
        "length": "PT14H"
      }
    }
  }
]
WHERE (json_exists(client_policy_js, '$?(@.inputs.supportedEntryTypes.punchEntry.entrySource.clock == true)') 
   OR json_exists(client_policy_js, '$.inputs.supportedEntryTypes.timeSheetEntry'))
我编写此sql条件是为了检查json数据是否包含特定的对象/值,这里的“client_policy_js”是json类型的列名:

查询

SELECT p.pol_name,p.pol_oid FROM PYR_CLIENT_POLICY_OPTION p
WHERE p.status    =1
AND (json_exists(client_policy_js, '$?        
(@.inputs.supportedEntryTypes.punchEntry.entrySource.clock == true)')
OR json_exists(client_policy_js, 
'$.inputs.supportedEntryTypes.timeSheetEntry'))
AND ((p.eff_date BETWEEN TRUNC(sysdate) AND TRUNC(sysdate))
OR (p.eff_date_end BETWEEN TRUNC(sysdate) AND TRUNC(sysdate))
OR (p.eff_date     < TRUNC(sysdate)
AND p.eff_date_end > TRUNC(sysdate)))

如果您使用的是Postgres 12,只需将
json_exists()
替换为
jsonb_path_exists()

如果使用的是旧版本,则需要使用
#>
运算符来提取指定路径上的值。使用
->
运算符获取第一个数组元素:

WEHERE(客户端策略->0#>{inputs,supportedEntryTypes,punchEntry,entrySource,clock}}):text[]='true'
或客户端_policy_js->0#>'{inputs,supportedEntryTypes,timeSheetEntry}'::text[]不为空)

请写出更好的问题
WHERE (jsonb_path_exists(client_policy_js, '$?(@.inputs.supportedEntryTypes.punchEntry.entrySource.clock == true)') 
       OR jsonb_path_exists(client_policy_js, '$.inputs.supportedEntryTypes.timeSheetEntry'))