Page 1 of 2

JSON Parse Command for a JSON that starts with [

PostPosted: Fri Sep 06, 2019 5:18 pm
by AnandB
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.

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

PostPosted: Fri Sep 06, 2019 10:14 pm
by enrico-sorichetti
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

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

PostPosted: Sat Sep 07, 2019 7:36 am
by AnandB
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.

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

PostPosted: Sat Sep 07, 2019 9:07 am
by AnandB
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.

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

PostPosted: Tue Mar 28, 2023 1:55 pm
by Pvernekar
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.

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

PostPosted: Wed Mar 29, 2023 12:15 am
by sergeyken
2023 - 2019 = 4 years old topic

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

PostPosted: Wed Mar 29, 2023 12:32 am
by Robert Sample
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.

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

PostPosted: Wed Mar 29, 2023 11:10 pm
by Pvernekar
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.

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

PostPosted: Wed Mar 29, 2023 11:49 pm
by Robert Sample
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.

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

PostPosted: Thu Mar 30, 2023 12:29 am
by Robert Sample
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.