Rotem87
Rotem87

Reputation: 105

Parse JSON array APEX_JSON in PL SQL

I have the following JSON object -

{
  "items": [
    {
      "tableName": "contacts",
      "count": 1,
      "columnNames": [
        "id"
      ],
      "rows": [
        [
          "45"
        ]
      ]
    }
  ],
  "links": [
    {
      "rel": "self",

    },
    {
      "rel": "describedby",

    }
  ]
}

I am trying to extract the value from rows- I need the value 45.

I tried to extract using -

row_id := apex_json.get_number ('rows[%d]', 1);

How I can extract it? Thanks in advance.

Upvotes: 1

Views: 2164

Answers (1)

Dan McGhan
Dan McGhan

Reputation: 4659

Here's an example:

declare

  l_json_val apex_json.t_values;
  l_clob     clob;
  l_row_val  varchar2(255);

begin

  l_clob := q'-
    {
      "items": [
        {
          "tableName": "contacts",
          "count": 1,
          "columnNames": [
            "id"
          ],
          "rows": [
            [
              "45"
            ]
          ]
        }
      ],
      "links": [
        {
          "rel": "self",

        },
        {
          "rel": "describedby",

        }
      ]
    }
  -';

  apex_json.parse(p_values => l_json_val, p_source => l_clob, p_strict => false); 

  l_row_val := apex_json.get_varchar2(p_path => 'items[1].rows[1][1]', p_values => l_json_val);

  dbms_output.put_line(l_row_val);

end;

Note that the path expressions with apex_json use a 1 base array index, not 0.

Upvotes: 1

Related Questions