Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: JSON with json_name  (Read 6380 times)
Candy M.
Posts: 139


« on: July 12, 2018, 03:24:15 am »

I'm in the process of integrating with a shipping webservice.

The JSON I get in the response looks like this:

{"poMasterIds":[],"errorMap":{"0":["Unit weight for POLine 0 is null"]},"isSuccess":false,"errorCode":601}

So I have a little program to try to interpret it.   I used util.JSON.proposeType and it said to use this:
Code
  1. RECORD
  2.    poMasterIds DYNAMIC ARRAY OF STRING,
  3.    errorMap RECORD
  4.        0 DYNAMIC ARRAY OF STRING
  5.    END RECORD,
  6.    isSuccess BOOLEAN,
  7.    errorCode FLOAT
  8. END RECORD
  9.  

So I did that and this is my little program:
Code
  1. IMPORT util
  2. SCHEMA fms
  3. #
  4. MAIN
  5.  
  6.        DEFINE
  7.        result_json     STRING          # Resulting JSON
  8. #
  9. #-------Data structure for JSON
  10. #
  11.        DEFINE pt_response RECORD
  12.                poMasterIds DYNAMIC ARRAY OF STRING,
  13.                errorMap RECORD
  14.                        dummy DYNAMIC ARRAY OF STRING ATTRIBUTES (json_name="0")
  15.                END RECORD,
  16.                isSuccess BOOLEAN,
  17.                errorCode INTEGER
  18.                END RECORD
  19.        DEFINE i,j INTEGER
  20.  
  21.        LET result_json = '{"poMasterIds":[],"errorMap":{"0":["Unit weight for POLine 0 is null"]},"isSuccess":false,"errorCode":601}'
  22.  
  23.        DISPLAY util.JSON.proposeType(result_json)
  24.        CALL util.JSON.parse(result_json,pt_response)
  25.        DISPLAY "poMasterIds:"
  26.        LET i = pt_response.poMasterIDs.getLength()
  27.        FOR j = 1 TO i
  28.                DISPLAY pt_response.poMasterIds[j]
  29.        END FOR
  30.        DISPLAY "errorMap:"
  31.        LET i = pt_response.errorMap.dummy.getLength()
  32.        DISPLAY "dummy length",i
  33.        FOR j = 1 TO i
  34.                DISPLAY pt_response.errorMap.dummy[j]
  35.        END FOR
  36.        DISPLAY "isSuccess ",pt_response.isSuccess
  37.        DISPLAY "errorCode ",pt_response.errorCode
  38.  
  39. END MAIN
  40.  
  41.  
Here is my output from DISPLAY statements:
Code
  1. RECORD
  2.    poMasterIds DYNAMIC ARRAY OF STRING,
  3.    errorMap RECORD
  4.        0 DYNAMIC ARRAY OF STRING
  5.    END RECORD,
  6.    isSuccess BOOLEAN,
  7.    errorCode FLOAT
  8. END RECORD
  9. poMasterIds:
  10. errorMap:
  11. dummy length          0
  12. isSuccess      0
  13. errorCode         601
  14.  
  15.  

So I'm unable to display ["Unit weight for POLine 0 is null"]
So I think I have something wrong in my structure.

Does anyone have any ideas?

Thanks!
Candy
Gary C.
Posts: 109


« Reply #1 on: July 12, 2018, 09:02:02 am »

Hi Candy

I faced a very similar issue where the resulting JSON string contained a dictionary for the errors which I think may help.

It is in the GWS section of the forum:

http://4js.com/fjs_forum/index.php?topic=1247

Gary

Candy M.
Posts: 139


« Reply #2 on: July 12, 2018, 02:55:17 pm »

Thanks so much Gary.   Delane was able to provide me modified code using dictionary and that appears to be the solution.   I'm going to get more "error" output and be sure it works correctly.

Candy
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines