0

I'm trying to chop out some lists from the following json using pljson

my_json := json('{"Order": 
{"no": 1, "batch": 2,"id": 3,"quantity": 10,
"sm_pack": [
        {
            "no": 10,
            "id": 1010,
            "quantity": 2
        },
        {
            "no": 11,
            "id": 1040,
            "quantity": 8
        }
    ],
    "sm_size": [
        { ....etc etc

However, I can't get it to work? I can print the data using this syntax:

  v_myjson.path('Order.sm_pack').print;
  v_myjson.path('Order.sm_pack[1].no').print;

But how can I assing all those different lists to variables for further processing. i tried different versions of "v_list := json_list(my_json.get('Order.sm_pack')) .. my_json.get('sm_pack').. whatever I try its"NULL SELF" and I seem to have turned blind.

Regards

elwis
  • 1,395
  • 2
  • 20
  • 34

1 Answers1

2

Printing json lists and objects differs from assigning them to some variables to manipulate them. I will try to answer your question through your example as follows:

DECLARE
    obj json := json();
    obj_1 json := json();
    arr json_list := json_list();
    val NUMBER;
BEGIN
/*Create your object*/
    obj := json('{"Order":{"no":1,"batch":2,"id":3,"quantity":10,"sm_pack":[{"no":10,"id":1010,"quantity":2},{"no":11,"id":1040,"quantity":8}],"sm_size":[{"no":10,"id":1010,"quantity":2},{"no":11,"id":1040,"quantity":8}]}}');

/*Assign object*/
    obj_1 :=json(obj.get('Order'));

/*Assign list from within the object*/
    arr := json_list(obj_1.get('sm_pack'));
    arr.print;
    --or
    arr := json_list(json(obj.get('Order')).get('sm_pack'));
    arr.print;

/*Get object value from within list*/
    val := json_ext.get_number(json(arr.get(2)), 'id');
    DBMS_OUTPUT.PUT_LINE(VAL);

END;
/

Notice I used get_number function as your values without single quotes, otherwise, I would use get_string.

Hope that helps!

Hawk
  • 5,060
  • 12
  • 49
  • 74