Skip to main content

Some of the JSON I have to parse has the same tag names for different segments.  For example

[{"id":10,"order_id":105,"product_id":137,"product_options":[{"id":8,"option_id":24...

where "id" occurs twice.  Is there some technique with the NAME OF/IS phrase that will allow me to give these two tags different names?  Or any other ideas other than manipulating each context-specific "id" to rename them to two different names?

I am at release 10.4


#extend
#AcuCobol

Some of the JSON I have to parse has the same tag names for different segments.  For example

[{"id":10,"order_id":105,"product_id":137,"product_options":[{"id":8,"option_id":24...

where "id" occurs twice.  Is there some technique with the NAME OF/IS phrase that will allow me to give these two tags different names?  Or any other ideas other than manipulating each context-specific "id" to rename them to two different names?

I am at release 10.4


#extend
#AcuCobol

If you look at our example atw-jparse.cbl, it also is parsing data that has same tag names. You may need to install AcuToWeb to see this example. We used multiple 01 items to separate these.

C:\\Users\\Public\\Documents\\Micro Focus\\extend 10.4.0\\sample\\AcuToWeb


If you look at our example atw-jparse.cbl, it also is parsing data that has same tag names. You may need to install AcuToWeb to see this example. We used multiple 01 items to separate these.

C:\\Users\\Public\\Documents\\Micro Focus\\extend 10.4.0\\sample\\AcuToWeb

thanks for your suggestion, but alas, i have no such program in my sample folder.  i do have acuweb, but this program was not anywhere to be found.  any chance you could post it please?  thanks!


thanks for your suggestion, but alas, i have no such program in my sample folder.  i do have acuweb, but this program was not anywhere to be found.  any chance you could post it please?  thanks!

identification division.
program-id. atw-jparse.
remarks. compile using -rw id -rw icon -rw type .
* Copyright (C) 2020 Micro Focus or one
* of its affiliates.
*
* The only warranties for products and services of Micro Focus
* and its affiliates and licensors ("Micro Focus") are set
* forth in the express warranty statements accompanying such
* products and services. Nothing herein should be construed as
* constituting an additional warranty. Micro Focus shall not
* be liable for technical or editorial errors or omissions
* contained herein. The information contained herein is
* subject to change without notice.
*
* Contains Confidential Information. Except as specifically
* indicated otherwise, a valid license is required for possession,
* use or copying. Consistent with FAR 12.211 and 12.212,
* Commercial Computer Software, Computer Software Documentation,
* and Technical Data for Commercial Items are licensed to the U.S.
* Government under vendor's standard commercial license.

* This program shows how to use the atw-script control. The
* atw-script control when executed using AcuToWeb allows the COBOL
* program to executes scripting languages, such as, JavaScript.
* This program when executing using AcuToWeb will leverage
* Javascript to obtain JSON data from a web service, parse
* the JSON data and display the data in a message box.
* together using JavaScript.
special-names.
data division.
working-storage section.
copy "crtvars.def".
copy "acugui.def".
copy "acucobol.def".
01 n pic 9(3) value 1.

01 top-level.
03 coord.
05 lon pic S9(3)v9(4).
05 lat pic S9(3)v9(4).
03 base pic x(10).
03 main.
05 temp pic 9(3)v9(2).
05 feels_like pic s9(3)v9(2).
05 temp_min pic 9(3)v9(2).
05 temp_max pic 9(3)v9(2).
05 pressure pic 9(5).
05 humidity pic 9(3).
03 visibility pic 9(5).
03 wind.
05 speed pic 9(3)v9(2).
05 deg pic 9(3).
03 clouds.
05 all pic 9(3).
03 dt pic 9(10).
03 sys.
05 type pic 9.
05 id pic 9(7).
05 country pic x(2).
05 sunrise pic 9(10).
05 sunset pic 9(10).
03 timezone pic 9(4).
03 id pic 9(7).
03 name pic x(40).
03 cod pic 9(3).
03 weather occurs 1 to 5 depending on n.
05 id pic 9(3).
05 main pic x(10).
05 description pic x(20).
05 icon pic x(3).
*
01 kp is special-names crt status pic 9(4).
01 rc pic s9(4).
01 resu pic x(1014).
01 resu2 pic x(1000).

*
* The openweathermap.org web site has you sign up for an ID (no charge), notice the APPID.
* There are API instructions on the openweathermap.org site.
*
78 myurl value 'api.openweathermap.org/.../weather
*
* Notice the JSON starts with {"coord": and the JSON weather element is an array value, which needs to be parsed into a COBOL table.
* Using the STRING phrase we add a top-level name to the JSON data and
* made a similar COBOL group item allowing parsing all this data with one JSON PARSE command

* The jquery-cmd injects JS code to do an Ajax call to the web service that responds with a JSON record with weather data.
* The CustomJSHelper function/class is part oof AcuToWeb and is used in order to trigger an Event into the COBOL program.
* This is particularly useful as many of the JS calls are asynchronous and the only way to know when
* they are finished is to use a callback function. These callback functions are widely used in Javascript.
* The technique to use is to code a JS callback, and then use the CustomJSHelper to trigger an event in order
* to notify the availability of data to the COBOL program that then will be able to handle it.
*
78 jquery-cmd value 'getData: function(_url) {$.ajax({url:_url}).then( function(data){ getDataObj.dataReturn = JSON.stringify(data); CustomJSHelper.event("getDataObj", CustomJSHelper.DATA_LOADED, 1);});},getReturn: function(){ return getDataObj.dataReturn; }'.
01 hwnd usage handle of window.
01 err-msg pic x(500).
01 varname pic x(30).
01 hfont usage handle.
*
screen section.
01 scr.
03 lb1 label line 2 col title
" JSON data from openweathermap.org web service".
03 e1 entry-field line 5 col 3
size 74 lines 10 multiline max-text 10000.
03 p1 push-button
line 17 col 35
title "Exit"
termination-value 27.
03 aw atw-script event aw-evt.
procedure division.
main-pgh.
accept hfont from standard object "LARGE-FONT"
display standard graphical window lines 20 control font hfont handle in hwnd
display scr
modify aw add("getDataObj", jquery-cmd) giving rc
if rc < 0 go to error-para end-if
modify aw call ("getDataObj.getData", myurl) giving rc
if rc < 0 go to error-para end-if
perform until kp = 27
accept scr on exception
continue
end-accept
end-perform
.
exit-para.
destroy scr
destroy hwnd
exit program
stop run
.

error-para.
inquire aw last-error in err-msg
inspect err-msg replacing trailing spaces by low-value
display message box err-msg
go to exit-para
.
aw-evt.
evaluate event-type
when NTF-ATW-EVENT
evaluate event-data-1
when 3
if event-data-2 = 1
modify aw call("getDataObj.getReturn") giving rc
if rc < 0 go to error-para end-if
if rc = 0 exit paragraph end-if
inquire aw last-result in resu2
modify e1 value = resu

* running in debug and choosing V at this point the JSON data is shown
* Here we string the JSON data, notice resu is larger to handle adding
* extra bytes to the destination of the STRING to account for the extra characters

string '{"top-level":', resu2, '}'
INTO resu
modify e1 value = resu

* running in debug and choosing V at this point the resulting JSON data is shown

if resu(1:1)="{"
json parse resu into top-level with detail
on exception display
"coord" json-status ":" json-code
end-json

display message box
"City: "name x"0d0a"
"Temp: "temp x"0d0a"
"Temp max: " temp_max " Temp min: " temp_min x"0d0a"
"Weather: " main of weather(1) x"0d0a"
"Description " description(1) x"0d0a"
"Lat:"lat " Long:"lon
end-display
end-if
end-if
*
end-evaluate
.


identification division.
program-id. atw-jparse.
remarks. compile using -rw id -rw icon -rw type .
* Copyright (C) 2020 Micro Focus or one
* of its affiliates.
*
* The only warranties for products and services of Micro Focus
* and its affiliates and licensors ("Micro Focus") are set
* forth in the express warranty statements accompanying such
* products and services. Nothing herein should be construed as
* constituting an additional warranty. Micro Focus shall not
* be liable for technical or editorial errors or omissions
* contained herein. The information contained herein is
* subject to change without notice.
*
* Contains Confidential Information. Except as specifically
* indicated otherwise, a valid license is required for possession,
* use or copying. Consistent with FAR 12.211 and 12.212,
* Commercial Computer Software, Computer Software Documentation,
* and Technical Data for Commercial Items are licensed to the U.S.
* Government under vendor's standard commercial license.

* This program shows how to use the atw-script control. The
* atw-script control when executed using AcuToWeb allows the COBOL
* program to executes scripting languages, such as, JavaScript.
* This program when executing using AcuToWeb will leverage
* Javascript to obtain JSON data from a web service, parse
* the JSON data and display the data in a message box.
* together using JavaScript.
special-names.
data division.
working-storage section.
copy "crtvars.def".
copy "acugui.def".
copy "acucobol.def".
01 n pic 9(3) value 1.

01 top-level.
03 coord.
05 lon pic S9(3)v9(4).
05 lat pic S9(3)v9(4).
03 base pic x(10).
03 main.
05 temp pic 9(3)v9(2).
05 feels_like pic s9(3)v9(2).
05 temp_min pic 9(3)v9(2).
05 temp_max pic 9(3)v9(2).
05 pressure pic 9(5).
05 humidity pic 9(3).
03 visibility pic 9(5).
03 wind.
05 speed pic 9(3)v9(2).
05 deg pic 9(3).
03 clouds.
05 all pic 9(3).
03 dt pic 9(10).
03 sys.
05 type pic 9.
05 id pic 9(7).
05 country pic x(2).
05 sunrise pic 9(10).
05 sunset pic 9(10).
03 timezone pic 9(4).
03 id pic 9(7).
03 name pic x(40).
03 cod pic 9(3).
03 weather occurs 1 to 5 depending on n.
05 id pic 9(3).
05 main pic x(10).
05 description pic x(20).
05 icon pic x(3).
*
01 kp is special-names crt status pic 9(4).
01 rc pic s9(4).
01 resu pic x(1014).
01 resu2 pic x(1000).

*
* The openweathermap.org web site has you sign up for an ID (no charge), notice the APPID.
* There are API instructions on the openweathermap.org site.
*
78 myurl value 'api.openweathermap.org/.../weather
*
* Notice the JSON starts with {"coord": and the JSON weather element is an array value, which needs to be parsed into a COBOL table.
* Using the STRING phrase we add a top-level name to the JSON data and
* made a similar COBOL group item allowing parsing all this data with one JSON PARSE command

* The jquery-cmd injects JS code to do an Ajax call to the web service that responds with a JSON record with weather data.
* The CustomJSHelper function/class is part oof AcuToWeb and is used in order to trigger an Event into the COBOL program.
* This is particularly useful as many of the JS calls are asynchronous and the only way to know when
* they are finished is to use a callback function. These callback functions are widely used in Javascript.
* The technique to use is to code a JS callback, and then use the CustomJSHelper to trigger an event in order
* to notify the availability of data to the COBOL program that then will be able to handle it.
*
78 jquery-cmd value 'getData: function(_url) {$.ajax({url:_url}).then( function(data){ getDataObj.dataReturn = JSON.stringify(data); CustomJSHelper.event("getDataObj", CustomJSHelper.DATA_LOADED, 1);});},getReturn: function(){ return getDataObj.dataReturn; }'.
01 hwnd usage handle of window.
01 err-msg pic x(500).
01 varname pic x(30).
01 hfont usage handle.
*
screen section.
01 scr.
03 lb1 label line 2 col title
" JSON data from openweathermap.org web service".
03 e1 entry-field line 5 col 3
size 74 lines 10 multiline max-text 10000.
03 p1 push-button
line 17 col 35
title "Exit"
termination-value 27.
03 aw atw-script event aw-evt.
procedure division.
main-pgh.
accept hfont from standard object "LARGE-FONT"
display standard graphical window lines 20 control font hfont handle in hwnd
display scr
modify aw add("getDataObj", jquery-cmd) giving rc
if rc < 0 go to error-para end-if
modify aw call ("getDataObj.getData", myurl) giving rc
if rc < 0 go to error-para end-if
perform until kp = 27
accept scr on exception
continue
end-accept
end-perform
.
exit-para.
destroy scr
destroy hwnd
exit program
stop run
.

error-para.
inquire aw last-error in err-msg
inspect err-msg replacing trailing spaces by low-value
display message box err-msg
go to exit-para
.
aw-evt.
evaluate event-type
when NTF-ATW-EVENT
evaluate event-data-1
when 3
if event-data-2 = 1
modify aw call("getDataObj.getReturn") giving rc
if rc < 0 go to error-para end-if
if rc = 0 exit paragraph end-if
inquire aw last-result in resu2
modify e1 value = resu

* running in debug and choosing V at this point the JSON data is shown
* Here we string the JSON data, notice resu is larger to handle adding
* extra bytes to the destination of the STRING to account for the extra characters

string '{"top-level":', resu2, '}'
INTO resu
modify e1 value = resu

* running in debug and choosing V at this point the resulting JSON data is shown

if resu(1:1)="{"
json parse resu into top-level with detail
on exception display
"coord" json-status ":" json-code
end-json

display message box
"City: "name x"0d0a"
"Temp: "temp x"0d0a"
"Temp max: " temp_max " Temp min: " temp_min x"0d0a"
"Weather: " main of weather(1) x"0d0a"
"Description " description(1) x"0d0a"
"Lat:"lat " Long:"lon
end-display
end-if
end-if
*
end-evaluate
.

perfect, thanks so much!