json generate


I'm just starting with JSON GENERATE in Visual COBOL for Eclipse 5.0.

I have a question is how to represent the JSON string shown below in the DATA DIVISION..

The JSON example in the SAMPLE folder uses the NAME entity a 01 level data name for the FROM identifier in the JSON GENERATE statement but my JSON starts with 3 items at the same level.

So what do I use as the "FROM" identifier. If I make the 3 name pairs 05 levels won't the dummy  01 level item be generated in the JSON?

{ "jsonrpc": "2.0", "message": "GarmentInfo", "params": { "general": { "ID": "ni1", "0-0": "ni1", "0-1": "Prod variable", "1-0": "Sylte Variable", "1-1": "Color var", "1-2": "Fabric Arcticle Var", "1-3": "Size Var", "units": 1 }, "sections": [ { "name": "section 1", "expected": "10.0", "min": "9.5", "max": "10.5", "sensor": 1, "laser1": 0, "laser2": 0 }, { "name": "section 2", "expected": "", "min": "", "max": "", "sensor": 4, "laser1": 0, "laser2": 0 }, { "name": "section 3", "expected": "20.0", "min": "19.0", "max": "21.0", "sensor": 10, "laser1": 0, "laser2": 0 } ] } }


  • Verified Answer

    It is true that there does not seem to be a way to generate a JSON data item without the 01 level data-name as you can only use SUPPRESS on items that are subordinate to the top level data item.

    You can however, extract the relevant JSON data record from the generated data by ignoring the generated data-name along with its starting brace "{" and then replacing the trailing brace "}" with a space.

    The following demo program will generate the JSON including the 01 level "my-data" and will then isolate the record starting with the "jsonrpc" field and will then replace the last "}" with a space by using the count-in data item which will contain the value of the number of characters generated.

    Perhaps you could use something like this?

    identification division. program-id. Program1. data division. working-storage section. 01 my-data. 05 jsonrpc pic x(3). 05 mymessage pic x(15). 05 params. 10 general. 15 myID pic x(5). 15 0-0 pic x(5). 15 0-1 pic x(20). 15 1-0 pic x(20). 15 1-1 pic x(20). 15 1-2 pic x(20). 15 1-3 pic x(20). 15 units pic 9(3). 10 sections occurs 3 times. 15 myname pic x(10). 15 expected pic x(4). 15 min pic x(4). 15 max pic x(4). 15 sensor pic 9(3). 15 laser1 pic 9(3). 15 laser2 pic 9(3). 01 my-json value spaces. 05 01-level pic x(11). 05 real-json pic x(1000). 01 char-count pic 9(4) value zeroes. procedure division. perform 100-populate-data json generate my-json from my-data count in char-count name mymessage "message" myID "ID" myname "name" end-json move " " to my-json(char-count:1) display real-json goback. 100-populate-data. move "2.0" to jsonrpc move "GarmentInfo" to mymessage move "ni1" to myID, 0-0 move "Prod variable" to 0-1 move "Sylte Variable" to 1-0 move "Color var" to 1-1 move "Fabric Arcticle Var" to 1-2 move "Size Var" to 1-3 move 1 to units move "section 1" to myname(1) move "section 2" to myname(2) move "section 3" to myname(3) move "10.0" to expected(1) move " " to expected(2) move "20.0" to expected(3) move "9.5" to min(1) move " " to min(2) move "19.0" to min(3) move "10.5" to max(1) move " " to max(2) move "21.0" to max(3) move 1 to sensor(1) move 4 to sensor(2) move 10 to sensor(3) move 0 to laser1(1) laser1(2) laser1(3) laser2(1) laser2(2) laser2(3). end program Program1.
  • Thanks so much Chris. I just stepped through your demo and it worked perfectly.
    I had developed a program using extended string and unstring statements but the new JSON GENERATE is a much better solution for me. It's much cleaner.  So I owe you one :)))