如何使用postgres在jsonb列中提取特定对象

时间:2017-08-24 06:07:05

标签: postgresql jsonb

我的Jsonb列看起来像这样:

table name:jsonb and column name:json_test

[
  {
    "run_id": "EXE20170822172151192",
    "user_id": "12",
    "log_level": "1",
    "time_stamp": "2017-08-22T10:03:38.083Z",
    ***"test_case_id": "1073",
    "test_suite_id": "null",
    "test_case_name": "Gmail Flow",***
    "test_suite_name": "",
    "test_suite_abort": "",
    "capture_screenshots": "Y",
    "abort_at_step_failure": "Y"
    "teststeps": [
      {
        "UItype": " UI ",
        "action": " UI_Open_Browser ",
        "param1": "Chrome",
        "step_id": " 1",
        "skip_to_step": " 0 ",
        "skip_from_step": " 0 ",
        "step_output_value": "true",
        "step_execution_time": " 0:0:12:154 ",
        "step_execution_status": "success",
        "step_execution_end_time": " 2017-08-22 17:22:35:813 IST+0530 ",
        "step_execution_start_time": " 2017-08-22 17:22:23:967 IST+0530 ",
        "use_previous_step_output_data": " N ",
        "execute_next_step_after_failure": " N ",
        "skip_execution_based_on_prv_step_status": " F "
      },

我想从json中提取对象,例如" test_case_id" " test_case_name"等。

我尝试使用" jsonb_array_elements"但由于jsonb的开始是一个数组,我无法获取数组中的对象,有人可以帮助这个吗

1 个答案:

答案 0 :(得分:0)

如果您修复了json(在&#34之前缺少逗号;测试步骤"),它可以工作:

s=# with j as (select '[
  {
    "run_id": "EXE20170822172151192",
    "user_id": "12",
    "log_level": "1",
    "time_stamp": "2017-08-22T10:03:38.083Z",
    "test_case_id": "1073",
    "test_suite_id": "null",
    "test_case_name": "Gmail Flow",
    "test_suite_name": "",
    "test_suite_abort": "",
    "capture_screenshots": "Y",
    "abort_at_step_failure": "Y",
    "teststeps": [
      {
        "UItype": " UI ",
        "action": " UI_Open_Browser ",
        "param1": "Chrome",
        "step_id": " 1",
        "skip_to_step": " 0 ",
        "skip_from_step": " 0 ",
        "step_output_value": "true",
        "step_execution_time": " 0:0:12:154 ",
        "step_execution_status": "success",
        "step_execution_end_time": " 2017-08-22 17:22:35:813 IST+0530 ",
        "step_execution_start_time": " 2017-08-22 17:22:23:967 IST+0530 ",
        "use_previous_step_output_data": " N ",
        "execute_next_step_after_failure": " N ",
        "skip_execution_based_on_prv_step_status": " F "
      }
]
}
]'::jsonb b)
select jsonb_array_elements(b)->'test_case_id' from j;
 ?column?
----------
 "1073"
(1 row)