Results 1 to 10 of 10

Thread: Problems with catching received JSON-data

  1. #1
    Join Date
    Feb 2018
    Posts
    9

    Default Problems with catching received JSON-data

    Hi!

    First of all, I'm pretty new to API integrations.

    The issue I have is simple to move the received JSON-data into a string. I can do this with the fields: "_embedded", "enheter" and "organisasjonsnummer". The issue i am facing is to get the data received under "postadresse". Like "postnummer", "poststed" and "adresse". I'm pretty sure there is a easy way to handle this, but I haven't found the answer so far.


    Any help would be appreciated

    Click image for larger version. 

Name:	postman_json_response.jpg 
Views:	59 
Size:	45.6 KB 
ID:	12575
    Click image for larger version. 

Name:	df_code.jpg 
Views:	80 
Size:	93.8 KB 
ID:	12576

  2. #2
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    1,332

    Default Re: Problems with catching received JSON-data

    Hi

    There are a couple of ways to do this. The first (and easiest) is to define a struct (or series of structs) with the same names and data-types (the actual struct names don't matter, but the member names need to match) as appear in the JSON and use the function of a cJsonObject JsonToDataType to move all of the JSON into that to deal with as you require. So:
    Code:
    Struct tSelf
        String href
    End_Struct
    
    Struct t_links
        tSelf self
    End_Struct
    
    Struct tOrganisasjsonform
        String kode
        String beskrivelse
        t_links links
    End_Struct
    
    Struct tEnheter
        String organisasjsonnummer
        String navn
        tOrganisasjsonform organisasjsonform
        tPostadresse postaddress  // Need to define this struct...
    // etc.
    End_Struct
    
    Struct t_embedded
        tEnheter[] enheter
    // maybe more stuff - screen shot does not show
    End_Struct
    
    ...
    
    t_embedded tEmbedded
    
    Set pbRequireAllMembers of hoJson to False // Not required, but will avoid errors if not all the members you expect are present
    Get JsonToDataType of hoJson to tEmbedded
    // And all the data should be in the tEmbedded stuct variable
    I created a program - RESTGen - which will allow you to paste sample JSON into it from which it will generate the required struct packages. You can find a copy here in this Forum, but an updated 19.0 version is: RESTgenv19.zip - this is not as well tested as I'd like, but it may save you a load of work.

    The second approach is the one you have started with, although for simple values you don't have to get the object to get its value, so:
    Code:
        // NAVN
        Get Member of hoJson "navn" to hoBrregFirmName
        Get MemberValue of hoBrregFirmName to sFirmname
        Send Destroy of hoBrregFirmName
        // Should just be written:
        Get MemberValue of hoJson "navn" to sFirmname
    Making life much easier.

    For objects with children, you would have to get the object (and later Destroy it) in order to access the children.

    NOTE: I just wrote all that by hand, so don't expect it to be bug-free!

    Mike

  3. #3
    Join Date
    Feb 2018
    Posts
    9

    Default Re: Problems with catching received JSON-data

    Thanks for helping me, Mike!

    I have tried to change the code to the struct-based setup. Seems, as you say, a lot easier that way. So after editing the code I have ended up with this:

    Code:
            Procedure OnClick
    
    
                Object oBrregJsonTransfer is a cJsonHttpTransfer
                    Set piRemotePort to rpHttpSSL
                    Set peTransferFlags to ifSecure
                    Set pbClearHeaders to False
                    Set psAcceptTypes to ""
                    Set pbShowErrorDialog to False
                End_Object
                            
                Struct tPostadresse
                    Integer postnummer
                    String poststed
                    String adresse
                End_Struct
    
    
                Struct tEnheter
                    Integer organisasjonsnummer
                    String navn
                    tPostadresse[] postadresse
                End_Struct
                            
                Struct t_embedded
                    tEnheter[] enheter
                End_Struct
                
                t_embedded[] tEmbedded
                            
                Get Value of oFirmname to sFirmname
                
                Move ("/enhetsregisteret/api/enheter?navn="*String(sFirmname)) to sPath
                
                Get Create (RefClass(cJsonHttpTransfer)) to oBrregJsonTransfer
                Get HttpGetJson of oBrregJsonTransfer "data.brreg.no" sPath (&bOK) to hoJson
                Set pbRequireAllMembers of hoJson to False            
                
                If (bOK) Begin
                   Get JsonToDataType of hoJson to tEmbedded
    I receive an error: "Invalid Json Object. Expected array!" when testing. Is there something I am missing here?

  4. #4
    Join Date
    Feb 2009
    Location
    South Florida
    Posts
    3,781

    Default Re: Problems with catching received JSON-data

    you are telling it you are expecting an array of t_embedded types and that has an array of tEnheter objects inside

    you need to make sure to define your structs the same way the json data is defined

    This btw is were modern languages support anonymous types. No need to define anything

    simply get the json into an anon object and then use dot notation to access the members
    Michael Salzlechner
    StarZen Technologies, Inc
    http.://www.starzen.com

    Development Blog
    http://www.salzlechner.com/dev

    DataFlex Package Manager (aka Nuget for DataFlex)
    http://windowsdeveloper.com/dfPackage

  5. #5
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    1,332

    Default Re: Problems with catching received JSON-data

    You have t_embedded[] tEmbedded, when I think you want just t_embedded tEmbeded (i.e. not an array).

    Mike

  6. #6
    Join Date
    Feb 2018
    Posts
    9

    Default Re: Problems with catching received JSON-data

    Sorry for the late response.
    Again, thanks for helping me out.

    I removed the [] and I'm now able to run without any error messages. There is still something wrong. The structs isn't getting any data. The screenshot from the first post of the output doesn't show the complete JSON data and I suspect that it perhaps has something to do with the headers occurring multiple times ("organisasjonsnummer", "navn"...) but I'm not sure how to solve it. I am only after the data from the first organization.

    I have taken another screenshot with some more of the received JSON data.

    Click image for larger version. 

Name:	output1.jpg 
Views:	33 
Size:	144.1 KB 
ID:	12595
    Click image for larger version. 

Name:	output2.jpg 
Views:	27 
Size:	150.6 KB 
ID:	12596

  7. #7
    Join Date
    Feb 2009
    Location
    South Florida
    Posts
    3,781

    Default Re: Problems with catching received JSON-data

    your json data starts with an object that contains a field called _embedded that is of type object { ... }

    so your structs would be like this

    Code:
    Struct tEnheter
        String organisasjsonnummer
        ...
    End_Struct
    
    Struct tEmbedded
        tEnheter[] enheter    
    
    End_Struct
    
    Struct tEnhetsRegData
        tEmbedded _embedded
    
    End_Struct
    a tip here is fill your structs with data and then convert your test struct to json and compare the data to what you are receiving. this will show the difference right away

    btw if i havent mentioned this before this is a great example of why anonymous types would be an important addition to DF
    Michael Salzlechner
    StarZen Technologies, Inc
    http.://www.starzen.com

    Development Blog
    http://www.salzlechner.com/dev

    DataFlex Package Manager (aka Nuget for DataFlex)
    http://windowsdeveloper.com/dfPackage

  8. #8
    Join Date
    Feb 2018
    Posts
    9

    Default Re: Problems with catching received JSON-data

    I do recognize that I have a hard time to understand this completely

    My struct does now look like this:

    Code:
    Struct tForretningsadresse
        Integer postnummer
        String poststed
        String[] adresse
    End_Struct
                
    Struct tPostadresse
        Integer postnummer
        String poststed
        String[] adresse
    End_Struct
    
    
    Struct tEnheter
        Integer organisasjonsnummer
        String navn
        tPostadresse postadresse
        tForretningsadresse forretningsadresse
    End_Struct
                            
    Struct tEmbedded
        tEnheter[] enheter
    End_Struct
    
    
    Struct tEnhetsRegData
        tEmbedded _embedded
    End_Struct
    While my code that moves the received data into strings looks like this:

    Code:
    Move _embedded.enheter.navn to sFirmname
    Move _embedded.enheter.organisasjonsnummer to iOrgnr
    Move _embedded.enheter.postadresse.adresse to sAdresse
    Move _embedded.enheter.postadresse.postnummer to iPostnr
    Move _embedded.enheter.postadresse.poststed to sSted
    When I add [] behind the tEnheter, the Studio expects an array. And it seems like it has to be there since the JSON received contains it. But then I get this error message: "Undefined symbol in argument _EMBEDDED.ENHETER.NAVN" and on the other "move lines".

    Do I have to move the JSON data another way, or is there something obvious I'm overlooking?

  9. #9
    Join Date
    Feb 2009
    Location
    South Florida
    Posts
    3,781

    Default Re: Problems with catching received JSON-data

    tEnhetsRegData contains an object of type tEmbedded called _embedded

    tEmbedded contains an array of type tEnheter

    Code:
    tEnhetsRegData myData
    
    .. code to get data
    
    integer iCount iIndex
    
    // get size of array
    move (sizeofarray(myData._embedded.enheter)) to iCount
    decrement icount
    
    // loop array
    for iIndex from 0 to icount
       move myData._embedded.enheter[index].organisasjonsnummer
    
    loop
    
    
    Michael Salzlechner
    StarZen Technologies, Inc
    http.://www.starzen.com

    Development Blog
    http://www.salzlechner.com/dev

    DataFlex Package Manager (aka Nuget for DataFlex)
    http://windowsdeveloper.com/dfPackage

  10. #10
    Join Date
    Feb 2018
    Posts
    9

    Default Re: Problems with catching received JSON-data

    Wooohooo!

    It works
    Thanks a lot for helping a beginner with (probably) some newbie questions!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •