[ardoJSON] Having trouble converting JSON to Record List

[ardoJSON] Having trouble converting JSON to Record List

  
Forge Component
(21)
Published on 30 Aug by Ricardo Silva
21 votes
Published on 30 Aug by Ricardo Silva
Hi Ricardo,
  I'm using JSON2RecordList and I am not able to get it to convert. The record list is blank. I have included an OML to show the issue.
Thanks,
Gerry
Hello Gerry,

After formatting the JSON.txt in your eSpace I see that there are a few problems with it.

1) It contains 2 objects, and not just one.

The input of ardoJSON's JSON2RecordList needs to be, on itself, a valid JSON object, value or array. In this case it isn't. I haven't contemplated this case. Is this how your REST API is returning the values?

2) your structure does not match the returned objects.

{
  "messages": "",
   "device": { ...}
}

Corresponds to a structure with two attributes, one named messages and one named device, which is of itself a record. Your Device structure corresponds to what you should put in the device attribute (more or less).

3) The $.device.details array is one of those ... things ... which isn't supported: an array with heterogenous types (some values have one type, others have another). I'm not sure you would be able to put this into OutSystems.

4) One of the attributes of the device object has an invalid character for an OutSystems attribute (@id). 


On the weekend I will try and take a look at what I can do to better support the use case of your file, in the meantime you can check the attached eSpace for how the structure should be defined (to counter point 2) ).

I have also separated the JSON.txt you supplied into two files, device1.json and device2.json. I have changed the code to import them seperately into the record list. I haven't had the opportunity to actually test this in runtime, but I believe this should yield 2 records with the two devices now. Let me know if this is not the case and I'll look into it tomorrow.

Notice the Device structure does not have a details attribute. This is to prevent parsing the problematic (3) structure. If you need to access data in that structure, you will need to use the JSONSelect functionality of ardoJSON 1.0.

I will think about what I can do to help people which require access to such freak structures.

To overcome point 1) you may pre-parse the string to divide the two objects. In this particular case you can search for the beginning of the object: "{""messages" and separate the string there.

I hope this helps you in your endeavor.
by the way, I just sent you the semi-raw formated JSON.txt file so you can take a better look at what I mean.
Hello Gerry,

For some reason the editor I was using seems to have duplicated the record and the first point I mentioned is not actually valid. Your JSON.txt is a single object.

(Thanks Lúcio, for pointing it out to me)

I have tried the test.oml I made and there was an error: I still had a details attribute but it was a Text, so it was throwing out an error while deserializing.

In attachment you can find a fixed version without access to the details part of the original object. Like I said, you could use JSONSelect if you wish to access data from there.
Hello once again.

I have tried making the Entry structure of key and value as texts and the JSON processor worked.

This may not work in other situations.

You can find the final code for processing your JSON in attachment.
Thank you very, very much for all of your help with this!
Hi,

I have also a problem with mapping a certain part of JSON:

"geometry" : 
      {
        "rings" : 
        [
          [
            [79567.9645575655, 455059.125479801], 
            [79605.0978736059, 455129.813207569], 
            [79686.5228916518, 455129.813334081], 
            [79676.6804469024, 455059.572946627], 
            [79675.3382545878, 455058.678248712], 
            [79649.8371666145, 454999.175286813], 
            [79567.9645575655, 455059.125479801]
          ]
        ]
      }


how do I map this?

I have now 
geometry
- rings [record list of type rings]

rings
- x [text]
- y [text]

Statler, have you tried using the RestWebServices component ? It should take care of all your structure mapping problems :)

In any case, here's a proposal for this structure:

rings is an array, so you'll need a record list, of which type?

you look at it, and it's another array. So it should be another record list. But what's the name of the attribute? If you're not seeing an explicit record, it's a flattenned list, so the attribute name must be Value for deserialization to succeed.

Rings is a record list with a type with an attribute Value which is a record list of ... another array there. So another record list. This time the array has another array in it and then you have floats.

I hope you get by now that you should really be using RestWebServices component :)
ack, did not spot that component :)

will check it out when I have time :)


Hi,

I have a problem mapping JSON to a structure. It's a very simple peace of JSON but is isn't working. What am I doing wrong?
I have had a look at the RestWebServices but where on JAVA.

Please have a look at the preperation of the only page.
Hello Niek,

You need to give JSON2RecordList a Record List, not a Record. It doesn't work on records :)
Hi,
 
I have the following peace of JSON. The question is how do I create a structure for the types part?
 
Thanks,
Niek.

{
   "results": [
      {
         "address_components": [
            {
               "long_name": "Pater De Grootstraat",
               "short_name": "Pater De Grootstraat",
               "types": [
                  "route"
               ]
            },
            {
               "long_name": "Laakdal",
               "short_name": "Laakdal",
               "types": [
                  "sublocality",
                  "political"
               ]
            },
Hello Niek,

Have you tried using the RestWebServices app to create the structure for you? You don't actually need to install it, you can use it online on the demo !

In any case, that types would be a simple record list of the Text structure.

Best regards,
Ricardo Silva
Hello Ricardo,

We are using Java here. But I found the problem. When creating the structure you have to use Value with a capital V. Now it works fine.

"types": [
                  "sublocality",
                  "political"
               ]
 
Thanks for your response.
 
Best regards,
Niek.