COBOL

COBOL

COBOL

COBOL is responsible for the efficient, reliable, secure, and unseen day-to-day operations of the world's economy.

 View Only
Expand all | Collapse all

Converting returned list to json with JSON PARSE command

  • 1.  Converting returned list to json with JSON PARSE command

    Posted Tue September 26, 2023 10:53 PM

    I'm trying to convert data from an api that is being called by the cobol program, when I try to convert the json of ex:

    {"msg":[{"ver":5,"uid":1000,"txt":" Hello World!"}]}, I get conversion error.

    What would be the best way to define the data following the json I have in return?

    Below is a model of variables that I used in tests:

    03 msg.
           05 uid pic 9(4).
           05 txt pic x(40).

    Another example:

    03 msg occurs 1 times indexed by ind.
          05 uid pic 9(4).
           05 txt pic x(40).



    ------------------------------
    Leandro Alves da Silva
    ------------------------------


  • 2.  RE: Converting returned list to json with JSON PARSE command

    Posted Thu September 28, 2023 10:50 AM

    Hello Leandro, 

    The following code works for me.  Could you try it?

    ACTUAL COBOL CODE

           CBL CODEPAGE(1047)
           IDENTIFICATION DIVISION.
           PROGRAM-ID. jp.
    
           ENVIRONMENT DIVISION.
    
           DATA DIVISION.
           WORKING-STORAGE SECTION.
    
           01 JDOC pic x(60) value
                '{"msg":[{"ver":5,"uid":1000,"txt":" Hello World!"}]}'.
           01 JDOC-UTF8 pic x(60).
    
    
           01 top1.
             03 msg occurs 1 times indexed by ind.
              05 uid pic 9(4).
              05 txt pic x(40).
    
           PROCEDURE DIVISION.
    
    
          * initialize top1 to space
               move space to top1
    
          * convert JOC
          *    1) from 1047   to UTF-16
          *    2) from UTF-16 to UTF-8
          *    3) save to JDOC-UTF8
               move function display-of(
                      function national-of(JDOC 1047)
                          1208) to JDOC-UTF8
    
          * call JSON
               set ind to 1
               json parse JDOC-UTF8 into msg with detail
    
          * display the result
               display "UID: " UID(1)
               display "TXT: " TXT(1)
    
               stop run.
    

    ACTUAL OUTPUT:

    IGZ0322I During execution of the JSON PARSE statement on line 35 of program JP, no data item matched JSON name "ver" at
             offset 9.
    UID: 1000
    TXT:  Hello World!
    


    ------------------------------
    Roy Bae
    ------------------------------



  • 3.  RE: Converting returned list to json with JSON PARSE command

    Posted Fri September 29, 2023 02:23 AM

    Hello, 

    I think you should add a "ver" data item in your Cobol structure "top1". Something like that :

           01 top1.
             03 msg occurs 1 times indexed by ind.
              05 ver pic x.
              05 uid pic 9(4).
              05 txt pic x(40).
    

    the IGZ0322I message says no data item matched what's in you Json text at position 9

                       

    {"msg":[{"ver":5,"uid":1000,"txt":" Hello World!"}]}

                       ^--- this one

    Give it a try and let me know

    /Loic



    ------------------------------
    Loic Vital-Durand
    ------------------------------