I have a script that has two loops, each doing some appending to a JSON object. Both loops append a json object to a json list. I finally have the formatting figured out, but it is also returning empty json objects. Is there a way in PL/JSON to check for empty JSON objects?
I have tried if (my_json_obj is not null) then
to no avail.
Here is what my expected return result should look like:
{
"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"
}]
}
EDIT: Here's what my code looks like
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;
I don't really know what you mean by null
. All methods in the PL/JSON library return some form of JSON type. If you mean an empty object, this will work:
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;