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.
JSON Parse Command for a JSON that starts with [
-
- Posts: 6
- Joined: Mon Dec 26, 2011 1:20 pm
- Skillset: COBOL, DB2, JCL, CICS, REXX
- Referer: Internet
JSON Parse Command for a JSON that starts with [
Anand Biradar
-
- Global moderator
- Posts: 3006
- Joined: Fri Apr 18, 2008 11:25 pm
- Skillset: tso,rexx,assembler,pl/i,storage,mvs,os/390,z/os,
- Referer: www.ibmmainframes.com
Re: JSON Parse Command for a JSON that starts with [
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
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
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
-
- Posts: 6
- Joined: Mon Dec 26, 2011 1:20 pm
- Skillset: COBOL, DB2, JCL, CICS, REXX
- Referer: Internet
Re: JSON Parse Command for a JSON that starts with [
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.
{"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
-
- Posts: 6
- Joined: Mon Dec 26, 2011 1:20 pm
- Skillset: COBOL, DB2, JCL, CICS, REXX
- Referer: Internet
Re: JSON Parse Command for a JSON that starts with [
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
-
- Posts: 3
- Joined: Tue Mar 28, 2023 1:42 pm
- Skillset: Cobol, JCL,CICS,DB2,VSAM
- Referer: Internet
Re: JSON Parse Command for a JSON that starts with [
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.
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.
- sergeyken
- Posts: 458
- Joined: Wed Jul 24, 2019 10:12 pm
- Skillset: Assembler, JCL, Utilities, PL/I, C/C++, DB2, SQL, REXX, COBOL, etc. etc. etc.
- Referer: Internet search
Re: JSON Parse Command for a JSON that starts with [
2023 - 2019 = 4 years old topic
Javas and Pythons come and go, but JCL and SORT stay forever.
-
- Global moderator
- Posts: 3720
- Joined: Sat Dec 19, 2009 8:32 pm
- Skillset: Systems programming, SAS, COBOL, CICS, JCL, SMS, VSAM, etc.
- Referer: other forum
- Location: Dubuque, Iowa, USA
Re: JSON Parse Command for a JSON that starts with [
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.
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.
-
- Posts: 3
- Joined: Tue Mar 28, 2023 1:42 pm
- Skillset: Cobol, JCL,CICS,DB2,VSAM
- Referer: Internet
Re: JSON Parse Command for a JSON that starts with [
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.
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.
-
- Global moderator
- Posts: 3720
- Joined: Sat Dec 19, 2009 8:32 pm
- Skillset: Systems programming, SAS, COBOL, CICS, JCL, SMS, VSAM, etc.
- Referer: other forum
- Location: Dubuque, Iowa, USA
Re: JSON Parse Command for a JSON that starts with [
Try making JSONP-REC a structure. The code I tested with is:and the results are: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.
Code: Select all
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) .
Code: Select all
COUNTRYCODE FR
FORMSTATUSCHANGEREASON
ID-N XXX-XX-XXXX
SIGNATUREDATE 2018-01-01
STATUS-C C
TYPE-C AB
-
- Global moderator
- Posts: 3720
- Joined: Sat Dec 19, 2009 8:32 pm
- Skillset: Systems programming, SAS, COBOL, CICS, JCL, SMS, VSAM, etc.
- Referer: other forum
- Location: Dubuque, Iowa, USA
Re: JSON Parse Command for a JSON that starts with [
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.
-
- Similar Topics
- Replies
- Views
- Last post
-
- 1
- 4024
-
by Leixner
View the latest post
Thu Sep 15, 2022 3:33 am
-
- 1
- 2985
-
by oliver07
View the latest post
Sat Oct 30, 2021 2:32 pm
-
- 5
- 2448
-
by willy jensen
View the latest post
Tue Oct 12, 2021 2:02 pm
-
- 6
- 2489
-
by MFDEV
View the latest post
Fri Sep 11, 2020 9:04 am
-
- 4
- 1400
-
by futohomok
View the latest post
Mon Oct 16, 2023 9:01 pm