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 Return  (Read 18658 times)
Gary C.
Posts: 109


« on: May 11, 2018, 01:41:58 pm »

Hi

Not sure if this belongs here or the BDL section but, I am trying to work with a restful web service from a carrier integration firm and I am struggling to manage their return results and am looking for some advice. Their documentation states:

The response has 2 entries: one is a dictionary for errors and one a list of successful imports.

Here are two examples of the response, the first is a successful call and the second an unsuccessful one:

{"errors":{},"success":["000014298"]}

{"errors":{"0":["identifier: Consignment with identifier already exists"]},"success":[]}


In BDL, I am struggling to handle the dictionary element. Here is a snippet of my code:

Code
  1. define responseR record
  2.        errors dictionary of string,
  3.        success dynamic array of string
  4.    end record,
  5.    keys dynamic array of string,
  6.    idx smallint
  7.  
  8. .
  9. .
  10. .
  11.  
  12.    let info.status = resp.getStatusCode()
  13.    if info.status = 200 then
  14.        let info.response = resp.getTextResponse()
  15.        call util.JSON.parse(info.response, responseR)
  16.        let keys = responseR.errors.getKeys()
  17.        for idx = 1 to keys.getLength()
  18.            display responseR.errors[keys[idx]]
  19.        end for
  20.        for idx = 1 to responseR.success.getLength()
  21.            display responseR.success[idx]
  22.        end for
  23.    end if
  24.  

In the event that the errors dictionary has any entries, I get a BDL error:

JSON parse error: Unexpected '[', target is not an ARRAY

I can see it is not, but what is the best construct to parse such a return value into?

Thanks

Gary
Reuben B.
Four Js
Posts: 1116


« Reply #1 on: May 14, 2018, 12:19:37 am »

There was a similar discussion here http://4js.com/support/forum/?topic=1186

You have not mentioned the proposeType method so I will just make sure you are aware of it http://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_fgl_ext_util_JSON_proposeType.html

If you have used it, you will notice that it would return ...

Code
  1. RECORD
  2.    errors RECORD
  3.        0 DYNAMIC ARRAY OF STRING
  4.    END RECORD,
  5.    success DYNAMIC ARRAY OF STRING
  6. END RECORD

... which is not valid syntax. proposeType was added in 3.00, DICTIONARY was added in 3.10, wether proposeType will ever propose DICTIONARY is a valid question? 

If you replace the 0 with DICTIONARY then you get a syntax error as DICTIONARY is expecting a data-type.

I then tried creating a type as in the code below ...

Code
  1. IMPORT util
  2. MAIN
  3. DEFINE s1, s2 STRING
  4. TYPE errors_type DYNAMIC ARRAY OF STRING
  5. DEFINE responseR RECORD
  6.    errors DICTIONARY OF errors_type,
  7.    success DYNAMIC ARRAY OF STRING
  8. END RECORD
  9.  
  10.  
  11.    LET s1 = '{"errors":{},"success":["000014298"]}'
  12.    LET s2 = '{"errors":{"0":["identifier: Consignment with identifier already exists"]},"success":[]}'
  13.  
  14.    DISPLAY util.JSON.proposeType(s1)
  15.    DISPLAY util.JSON.proposeType(s2)
  16.  
  17.    CALL util.JSON.parse(s1,responseR)
  18.    DISPLAY responseR.success[1]
  19.  
  20.    INITIALIZE responseR TO NULL
  21.    CALL util.JSON.parse(s2,responseR)
  22.    DISPLAY responseR.errors["0"][1]
  23.  
  24. END MAIN

... which seemed to work OK.  So you could try that.

Two possible issues for us to look at ...

1. Does proposeType propose DICTIONARY?  Should it in the case where the proposed record name (in this case "0") is not valid Genero syntax?
2. Should DICTIONARY OF DYNAMIC ARRAY OF ... be valid syntax?

Reuben
 



Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Gary C.
Posts: 109


« Reply #2 on: May 14, 2018, 09:27:07 am »

Hi Reuben

I was not aware of the proposeType method so thanks for that as it will come in very handy.

Your workaround of using a type has worked perfectly so again, thanks for the suggestion.

Gary
Reuben B.
Four Js
Posts: 1116


« Reply #3 on: May 22, 2018, 02:14:45 am »

...

Two possible issues for us to look at ...

1. Does proposeType propose DICTIONARY?  Should it in the case where the proposed record name (in this case "0") is not valid Genero syntax?
2. Should DICTIONARY OF DYNAMIC ARRAY OF ... be valid syntax?

Reuben
 

I left two hanging questions which I will wrap up...

1. Does proposeType propose DICTIONARY? Should it in the case where the proposed record name (in this case "0") is not valid Genero syntax?

It won't.  With the proposeType method, view the result as a starting point for you to refine, don't expect the result to be a 100% solution.

It maybe that you need to replace a RECORD with DICTIONARY, or it maybe that you need to utilise the json_name attribute http://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_fgl_json_utils_names.html to map a JSON element name to a 4gl record element name.



2. Should DICTIONARY OF DYNAMIC ARRAY OF ... be valid syntax?

Short answer is it should, for the moment the work around is to use TYPE.  We'd consider the better practise would be to use TYPE in any event.

More interesting points to note.  In general the compiler with ...
DEFINE var-name collation-type OF element-type should not limit element-type to non-collation type

So any combination of collation-type and lengths of combinations could be allowed, without having to resort to types.  (the lengths being interesting, DEFINE ddd DICTIONARY OF DICTIONARY OF DICTIONARY ... might be useful for sparse data ...)

Reuben




Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines