JSON Parse Command for a JSON that starts with [



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

JSON Parse Command for a JSON that starts with [

Postby AnandB » Fri Sep 06, 2019 5:18 pm

Hi,

I have a query regarding JSON PARSE statement; and corresponding COBOL declaration.

For the following JSON the corresponding COBOL declaration would be

JSON
client-data":{
"account-num":123456789012,
"balance":-125.53,
"billing-info":{
"name-first":"John",
"name-last":"Smith",
"addr-street":"12345 First Avenue",
"addr-city":"New York",
"addr-region":"New York",
"addr-code":"10203"
}
}

COBOL Declaration.
01 client-data.
03 account-num pic 999,999,999,999.
03 balance pic $$$9.99CR.
03 billing-info.
05 name-first pic X(20).
05 name-last pic X(20).
05 addr-street pic X(20).
05 addr-city pic X(20).
05 addr-region pic X(20).
05 addr-code pic X(10).

What would be the corresponding COBOL declaration if the JSON started with a "[" i,e. if the JSON looks like

client-data":[
"account-num":123456789012,
"balance":-125.53,
"billing-info":{
"name-first":"John",
"name-last":"Smith",
"addr-street":"12345 First Avenue",
"addr-city":"New York",
"addr-region":"New York",
"addr-code":"10203"
}
]

Thank you
Anand B.
Anand Biradar
AnandB
 
Posts: 6
Joined: Mon Dec 26, 2011 1:20 pm
Has thanked: 1 time
Been thanked: 0 time

Re: JSON Parse Command for a JSON that starts with [

Postby enrico-sorichetti » Fri Sep 06, 2019 10:14 pm

meditating on the cobol manual at the section "Handling JSON arrays"
will tell all You might want to know about the relative cobol declaration

https://www.ibm.com/support/knowledgece ... ml04d.html
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2999
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: JSON Parse Command for a JSON that starts with [

Postby AnandB » Sat Sep 07, 2019 7:36 am

Thank you Enrico.

{"some-data":{"msg":[{"ver":5,"uid":10,"txt":"Hello"},{"ver":5,"uid":11,"txt":"World"},{"ver":5,"uid":12,"txt":"!"}]}}

Working-storage section.
1 some-data.
2 msg occurs 3.
4 ver usage comp-1.
4 uid pic 9999 usage display.
4 txt pic x(32).

The JSON I am working with has "[" before "msg"; something like
{"some-data":["msg":[{"ver":5,"uid":10,"txt":"Hello"},{"ver":5,"uid":11,"txt":"World"},{"ver":5,"uid":12,"txt":"!"}]]}.

some-data should be an array in this case, we cannot have occurs at 01 level in COBOL. The JSON is well within JSON standards.
Anand Biradar
AnandB
 
Posts: 6
Joined: Mon Dec 26, 2011 1:20 pm
Has thanked: 1 time
Been thanked: 0 time

Re: JSON Parse Command for a JSON that starts with [

Postby AnandB » Sat Sep 07, 2019 9:07 am

I used a dummy variable at level 01; followed by JSON data variables with occurs clause. In the Parse command I parsed JSON in the dummy variable with OMIT option. It looks to be working now. Thank you.
Anand Biradar
AnandB
 
Posts: 6
Joined: Mon Dec 26, 2011 1:20 pm
Has thanked: 1 time
Been thanked: 0 time

Re: JSON Parse Command for a JSON that starts with [

Postby Pvernekar » Tue Mar 28, 2023 1:55 pm

Hello team,

I am trying to parse the response form an API which looks as below.
01 jsonp-rec PIC X(1000).
[{"countryCode": "FR","formStatusChangeReason": "","id": "XXX-XX-XXXX","signatureDate": "2018-01-01","status": " ","type": "AB"}]

01 resp-rec.
05 countryCode PIC X(02).
05 formStatusChangeReason PIC X(40).
05 id-n PIC X(12).
05 signatureDate PIC X(10).
05 status-c PIC X(01).
05 type-c PIC X(02).

JSON PARSE JSONP-REC INTO resp-rec WITH DETAIL
NAME resp-rec IS OMITTED.

IGZ0335W During execution of the JSON PARSE statement on line 372 of program JPARPGM, the JSON text in JSONP-REC was found to be invalid. At offset 0, X'BA' was found, but one of LEFT CURLY BRACKET was expected.

and data is not parsed . Could you please suggest.
Pvernekar
 
Posts: 3
Joined: Tue Mar 28, 2023 1:42 pm
Has thanked: 0 time
Been thanked: 0 time

Re: JSON Parse Command for a JSON that starts with [

Postby sergeyken » Wed Mar 29, 2023 12:15 am

2023 - 2019 = 4 years old topic
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 419
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 7 times
Been thanked: 40 times

Re: JSON Parse Command for a JSON that starts with [

Postby Robert Sample » Wed Mar 29, 2023 12:32 am

First, you'll need to figure out a way to remove that leading [ and the trailing ] since JSON PARSE won't deal with them.

Second, JSON PARSE works with UTF-8 data -- and PIC X is NOT UTF-8. In COBOL, PIC U is UTF-8.

Third, your COBOL code has ID-N, STATUS-C, TYPE-C and none of these variables are in the JSON input so none of them will be parsed out. You need to either change the COBOL names or the JSON variables.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: JSON Parse Command for a JSON that starts with [

Postby Pvernekar » Wed Mar 29, 2023 11:10 pm

Thanks Robert.
I tried with as mentioned above changing the [, UTF-8 and json variables matching teh cobol declarations.

01 jsonp-rec PIC U(1000).
{"countryCode": "FR","formStatusChangeReason": "","id-n": "XXX-XX-XXXX","signatureDate": "2018-01-01","status-c": " ","type-c": "AB"}

During compilation I got this error
IGYPA3365-S Operand "JSONP-REC (UTF-8 ITEM)" was found in the "JSON" statement b
"UTF-8" operands are not supported for the "JSON" statement. The
statement was discarded.
Pvernekar
 
Posts: 3
Joined: Tue Mar 28, 2023 1:42 pm
Has thanked: 0 time
Been thanked: 0 time

Re: JSON Parse Command for a JSON that starts with [

Postby Robert Sample » Wed Mar 29, 2023 11:49 pm

Try making JSONP-REC a structure. The code I tested with is:
01  JSONP-REC.                                          
    05                          PIC U(58)   VALUE        
    '{"COUNTRYCODE": "FR","FORMSTATUSCHANGEREASON": "",'.
    05                          PIC U(58)   VALUE        
    '"ID-N": "XXX-XX-XXXX",'.                            
    05                          PIC U(58)   VALUE        
    '"SIGNATUREDATE": "2018-01-01","STATUS-C": "C",'.    
    05                          PIC U(58)   VALUE        
    '"TYPE-C": "AB"}'.                                  
    05                          PIC U(58)   VALUE SPACE.
    05                          PIC U(768)  VALUE SPACE.
                                                         
01  OUTPUT-REC.                                          
    05 RESP-REC                   OCCURS 01 .            
        10 COUNTRYCODE            PIC X(02).            
        10 FORMSTATUSCHANGEREASON PIC X(40).            
        10 ID-N                   PIC X(12).            
        10 SIGNATUREDATE          PIC X(10).            
        10 STATUS-C               PIC X(01).            
        10 TYPE-C                 PIC X(02).            

     JSON PARSE JSONP-REC                  
     INTO RESP-REC (01) WITH DETAIL        
     NAME RESP-REC IS OMITTED.            
                                           
     DISPLAY 'COUNTRYCODE '                
         COUNTRYCODE(01) .                
     DISPLAY 'FORMSTATUSCHANGEREASON '    
         FORMSTATUSCHANGEREASON(01) .      
     DISPLAY 'ID-N '                      
         ID-N(01) .                        
     DISPLAY 'SIGNATUREDATE '              
         SIGNATUREDATE(01) .              
     DISPLAY 'STATUS-C '                  
         STATUS-C(01) .                    
     DISPLAY 'TYPE-C '                    
         TYPE-C(01) .                      
and the results are:
COUNTRYCODE FR                                
FORMSTATUSCHANGEREASON                        
ID-N XXX-XX-XXXX                              
SIGNATUREDATE 2018-01-01                      
STATUS-C C                                    
TYPE-C AB                                      
RESP-REC is an array because I was testing to see if there's a way to handle the leading [ -- I couldn't find one.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: JSON Parse Command for a JSON that starts with [

Postby Robert Sample » Thu Mar 30, 2023 12:29 am

I was just doing some research and found out that the PIC U clause is new to Enterprise COBOL 6.3. Earlier versions of COBOL may have a toleration PTF for this (if there is one) but 6.3 is the only release I know for sure allows PIC U.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post