Title: Searching a JSON array Post by: Paul M. on February 03, 2022, 07:30:21 pm I have a program that reads in a nested JSON string like below:
{"Value":[{"DomainId":23,"DomainName":"PU","Description":"Public Utilities","WebTitle":"","ImageUrl":"","EditButtonsOn":false,"MapServiceId":1,"MobileMapCacheId":0},{"DomainId":21,"DomainName":"PW","Description":"Public Works","WebTitle":"","ImageUrl":"","EditButtonsOn":false,"MapServiceId":2,"MobileMapCacheId":0}],"Status":0,"Message":null,"ErrorMessages":[],"WarningMessages":[],"SuccessMessages":[]} I set up a bdl record to catch this: mycatch RECORD Value DYNAMIC ARRAY of util.JSONObject , # util.JSONArray Status STRING, Message STRING, ErrorMessages DYNAMIC ARRAY OF STRING, WarningMessages DYNAMIC ARRAY OF STRING, SuccessMessages DYNAMIC ARRAY OF String END RECORD In the code I can do. for i = 1 to mycatch.value.getlength() display mycatch.value.get("DomainId")," - ", mycatch.value.get("DomainName") END FOR but when I ask let i = mycatch.value.search("DomainId","PU") it returns 0 util.JSONArray does not have a search function, so I thought a dynamic array of JSONObjects would work. Am I missing something simple? I know I can substring the JSON array part of the string and push it into a dynamic array, but that seems like over kill. Any ideas are appreciated. Title: Re: Searching a JSON array Post by: Sebastien F. on February 04, 2022, 08:20:11 am Hello,
The ARRAY.search() method does not apply to util.JSONObject properties. I would expect a runtime error if you use the search() method on a DYNAMIC ARRAY OF <class> (looks like a bug) If your JSON data has a static structure, you can define the complete RECORD with all fields of a "Value" element, and then use the ARRAY.search() method: Code
You wrote: Quote In the code I can do. for i = 1 to mycatch.value.getlength() display mycatch.value.get("DomainId")," - ", mycatch.value.get("DomainName") END FOR This cannot work if mycatch.value is a DYNAMIC ARRAY, there is no such method like get(): Code
See: https://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_fgl_Arrays_ARRAY_methods.html I assume you have mixed up your code, trying different types util.JSONArray / util.JSONObject / DYNAMIC ARRAY OF util.JSONObject ... Note also that util.JSONArray.get() takes an index as argument, not a property name as when using util.JSONObject.get(): https://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_fgl_ext_util_JSONArray_get.html https://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_fgl_ext_util_JSONObject_get.html If the JSON data structure is variable, you can define the Value member as a util.JSONArray and then write generic code to search properties in the JSON structure. If this is the case, send a request to the support channel and we'll follow up and provide a sample. Seb Title: Re: Searching a JSON array Post by: Leo S. on February 04, 2022, 11:16:17 am You are not forced btw to have all attributes/data structures in your target record.
The util.JSON.parse function is smart and only fills the matching members. So if you aren't interested in some details of the JSON string transmitted you can simply omit them in your target RECORD structure. The snippet below is working too. Code Regards, Leo Title: Re: Searching a JSON array Post by: Leo S. on February 04, 2022, 11:20:32 am Ah, and I wanted to add that you should always
try Code to ease your work. it would have displayed Code
Title: Re: Searching a JSON array Post by: Sebastien F. on February 04, 2022, 11:38:37 am About ARRAY.search(), in fact you don't get a runtime error because you can search for object references:
Code
Then: Code
I have some doubts about the behavior when passing a field name, thought: Code
That one could raise a runtime error IMO. |