Roshan
Roshan

Reputation: 159

Parse JSON using APEX_JSON in PL SQL

I am trying to parse JSON in PL/SQL using APEX_JSON

My JSON response is like..

[
  {
    "NAME": "SAMPLE123",
    "SECOND_NAME": "MIDWEST MEDIA GROUP, INC",
    "ADDRESS": null,
    "PHONE_NUM": "050149603"
  },
  {
    "PHONE_NUM": "010609568",
    "ADDRESS": "BETTER VENDORS ASSOCIATION",
    "SECOND_NAME": "B V A CO OP INC",
    "NAME": "SAMPLE123"
  },
  {
    "PHONE_NUM": "111942970",
    "ADDRESS": null,
    "NAME": "SAMPLE123",
    "SECOND_NAME": "BALDWIN'S BUSINESS SYSTEMS, INC."
  },
  {
    "SECOND_NAME": "INNOVATIVE SALES TECHNOLO",
    "NAME": "SAMPLE123",
    "ADDRESS": null,
    "PHONE_NUM": "626904713"
  },
  {
    "ADDRESS": null,
    "PHONE_NUM": "050717132",
    "SECOND_NAME": "JEFFREY A. AVNY ATTORNEY AT LAW",
    "NAME": "SAMPLE123"
  },
  {
    "PHONE_NUM": "079203229",
    "ADDRESS": null,
    "SECOND_NAME": "3CLOUDS INC",
    "NAME": "SAMPLE123"
  },
  {
    "SECOND_NAME": "ARTHUR N SKLADMAN MD SC",
    "NAME": "SAMPLE123",
    "PHONE_NUM": "792034886",
    "ADDRESS": "SKLADMAN, ARTHUR"
  }]

I have written the following code in PL/SQL

sJsonIndex         APEX_JSON.t_values;

APEX_JSON.parse(sJsonIndex, clob_buff);

DBMS_OUTPUT.PUT_LINE(clob_buff);

sCount := APEX_JSON.get_count(p_path => '.' , p_values => sJsonIndex);

DBMS_OUTPUT.PUT_LINE('sCount ' || sCount);

IF sCount > 0 THEN

    FOR i in 1 .. sCount LOOP

        A_id := APEX_JSON.get_varchar2(
                            p_values => sJsonIndex, 
                            p_path => 'NAME['|| i ||']');

        --A_id := APEX_JSON.get_varchar2('NAME['|| i ||']');

        DBMS_OUTPUT.PUT_LINE('sCount i ' || i);

        DBMS_OUTPUT.PUT_LINE('A_id i ' || A_id);                   


    END LOOP;

END IF;

I am getting the output below:

sCount i 1
A_id i 
sCount i 2
A_id i 
sCount i 3
A_id i 
sCount i 4
A_id i 
sCount i 5
A_id i 
sCount i 6
A_id i 
sCount i 7
A_id i 
sCount i 8
A_id i 
sCount i 9
A_id i 
sCount i 10
A_id i 

Can anyone please help?.. I am new to APEX_JSON

Upvotes: 2

Views: 9893

Answers (2)

moose56
moose56

Reputation: 119

A slight tweak to Jeffrey Kemp answer:

APEX_JSON.get_varchar2(p_path => '[%d].NAME', p0 => i, p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '[%d].SECOND_NAME', p0 => i, p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '[%d].ADDRESS', p0 => i, p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '[%d].PHONE_NUM', p0 => i, p_values => sJsonIndex);

Upvotes: 1

Jeffrey Kemp
Jeffrey Kemp

Reputation: 60312

You're close - your json contains an array of records, and the array is unnamed:

APEX_JSON.get_varchar2(p_path => '['|| i ||'].NAME', p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '['|| i ||'].SECOND_NAME', p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '['|| i ||'].ADDRESS', p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '['|| i ||'].PHONE_NUM', p_values => sJsonIndex);

Upvotes: 4

Related Questions