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:
define responseR record
errors dictionary of string,
success dynamic array of string
end record,
keys dynamic array of string,
idx smallint
.
.
.
let info.status = resp.getStatusCode()
if info.status = 200 then
let info.response = resp.getTextResponse()
call util.JSON.parse(info.response, responseR)
let keys = responseR.errors.getKeys()
for idx = 1 to keys.getLength()
display responseR.errors[keys[idx]]
end for
for idx = 1 to responseR.success.getLength()
display responseR.success[idx]
end for
end if
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
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 ...
RECORD
errors RECORD
0 DYNAMIC ARRAY OF STRING
END RECORD,
success DYNAMIC ARRAY OF STRING
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 ...
IMPORT util
MAIN
DEFINE s1, s2 STRING
TYPE errors_type DYNAMIC ARRAY OF STRING
DEFINE responseR RECORD
errors DICTIONARY OF errors_type,
success DYNAMIC ARRAY OF STRING
END RECORD
LET s1 = '{"errors":{},"success":["000014298"]}'
LET s2 = '{"errors":{"0":["identifier: Consignment with identifier already exists"]},"success":[]}'
DISPLAY util.JSON.proposeType(s1)
DISPLAY util.JSON.proposeType(s2)
CALL util.JSON.parse(s1,responseR)
DISPLAY responseR.success[1]
INITIALIZE responseR TO NULL
CALL util.JSON.parse(s2,responseR)
DISPLAY responseR.errors["0"][1]
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
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
Quote from: Reuben B. on May 14, 2018, 12:19:37 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