在PL / JSON中检查JSON对象是空还是空

时间:2015-03-13 19:04:21

标签: oracle11g pljson

我有一个脚本有两个循环,每个循环都附加一个JSON对象。两个循环都将json对象附加到json列表。我终于找到了格式,但它也返回了空的json对象。 PL / JSON中有没有办法检查空的JSON对象?

我试过if (my_json_obj is not null) then无济于事。

以下是我的预期回报结果:

{
  "Outer List": [{
    "Inner List": [{
      "Val1": "2015-03-13T13:30:13.5593965Z",
      "Val2": "0001-01-01T00:00:00",
      "Val3": "BS2345"
    }, {
      "Val1": "2015-03-14T13:30:13.5593965Z",
      "Val2": "0001-01-01T00:00:00",
      "Val3": "5678B"
    }],
    "Inner Object Value": "prk1"
  }, {
    "Inner List 2": [{
      "Val1": "2015-03-13T13:30:13.5593965Z",
      "Val2": "0001-01-01T00:00:00",
      "Val3": "1234A"
    }],
    "Inner Object Value": "prk2"
  }]
}

编辑:这是我的代码的样子

declare
outer_json_obj json := json();-- Main JSON object. Contains all permit zone list 
outer_json_list json_list := json_list(); -- List inside outer json obj. Contains list of permits per zone
inner_json_obj json := json(); -- Object inside permit zone list. Contains list of permits per zone id
inner_json_list json_list := json_list(); -- innermost list. Contains list of permits per zone
search_param1 json_value;
search_param2 json_value;

json_body json := my_package.parse_body(:body);
search_list2 json_list := json_ext.get_json_list(json_body,'list1');
search_list1 json_list := json_ext.get_json_list(json_body,'list2');

begin
  for i in 1..search_list1.count loop
    search_param1 := search_list1.get(i);
    for i in 1..search_list2.count loop
        search_param2 := search_list2.get(i);
        inner_json_list.append(my_package.get_permit(search_param1, search_param2).to_json_value); --do not append until check for not null return
    end loop;
    inner_json_obj.put('Inner List', inner_json_list);
    inner_json_obj.put('Inner Object Value', search_param1);
    outer_json_list.append(inner_json_obj.to_json_value()); --put if around this to check inner_json_list for null
  end loop;
  outer_json_obj.put('Outer List', outer_json_list);
  htp.p(outer_json_obj.to_char());
end;

1 个答案:

答案 0 :(得分:2)

我真的不知道null你的意思。 PL / JSON库中的所有方法都返回某种形式的JSON类型。如果你的意思是一个空对象,这将起作用:

declare
  json_obj json := json('{}');
  list json_list;
begin
  list := json_obj.get_keys;

  if list.count = 0 then
    dbms_output.put_line('Empty object');
  else
    dbms_output.put_line('Non-empty object');
  end if;
end;