Options

JSON Best Way to read them on Dynamics NAV

nav_studentnav_student Member Posts: 175
According to NAVTechDays 2017 Json Meets NAV thre are several ways to Read a JSON file
Manually – but I would not do that • Convert Json to Xml and read with XmlPort and/or DOM Codeunit 6224 • Use JsonTextReader to read Json to a buffer table (Json Buffer, 1236) • Use JObject DotNet to read Json Json readers

I followed this article https://kauffmann.nl/2015/12/03/web-services-examples-part-2-verify-e-mail-address/

but my JSON has a more complex structure similar with the following example
{
    "firstName": "Oscar",
    "lastName": "Smith",
    "gender": "man",
    "age": 76,
    "address": {
        "streetAddress": "999 Udhna",
        "city": "ASurt",
        "state": "GJ",
        "postalCode": "254221"
    },
    "phoneNumbers": [
        { "type": "home", "number": "7383627627" }
    ]
}

Could you give me an example I can I extract for instance the city.

thanks for your help.

Answers

  • Options
    JuhlJuhl Member Posts: 724
    Parse it to a JsonObject, and loop. When getting to address, parse it into a new JsonObject, and loop again.
    Follow me on my blog juhl.blog
  • Options
    ftorneroftornero Member Posts: 522
  • Options
    nav_studentnav_student Member Posts: 175
    edited 2019-03-08
    Thanks for your reply guys. I used Juhl's approach:

    For this type of JSON reply
    {
        "authentication": "windows",
           "A": [
            {
                
                "A_A": [
                    {
                        "A_A_A": {
                            "addressLine": "No. and Street Name",
    
                        },
                        
                    }
                ]
            }
        ],
        "status": 200,
    }
    


    I used the following code:
    Note: Variable counter in this case is always 0 (zero)
    URL:=Text00;
    InitializeWebRequest(HttpWebRequestMgt,URL);
    
    TempBlob.INIT;
    TempBlob.Blob.CREATEINSTREAM(Instr);
    
    IF HttpWebRequestMgt.GetResponseStream(Instr) THEN BEGIN
      JsonText := TempBlob.ReadAsText('',TEXTENCODING::UTF8);
      IF JsonText <> '' THEN BEGIN
        JObject := JObject.Parse(JsonText);
        
        //"statusCode": 200,
        IF JObject.GetValue('status').ToString <> 'OK' THEN
          ERROR(JObject.GetValue('statusDescription').ToString);
        
        //"A": [ 
        IF NOT ISNULL(JObject.GetValue('A'))  THEN BEGIN
          JObject := JObject.GetValue('A');
          IF JObject.HasValues THEN BEGIN
             TempJObject := JObject.Item(counter);
    
        //A_A
               IF NOT ISNULL(TempJObject.GetValue('A_A')) THEN BEGIN
                 JObject2 := TempJObject.GetValue('A_A');
                 IF JObject2.HasValues THEN BEGIN
                   TempJObject2 := JObject2.Item(counter);
        //address
                   TempJObject2:=TempJObject2.GetValue('A_A_A');
                   MESSAGE(TempJObject2.GetValue('addressLine').ToString);
                 END;
               END;
             END;
          END;      
        END;
    
      END;
    END ELSE
      MESSAGE(GETLASTERRORTEXT);
    
    LOCAL InitializeWebRequest(VAR HttpWebRequestMgt : Codeunit "Http Web Request Mgt.";URL : Text)
    HttpWebRequestMgt.Initialize(URL);
    HttpWebRequestMgt.DisableUI;
    HttpWebRequestMgt.SetMethod('GET');
    HttpWebRequestMgt.SetTimeout(60000 * 3);
    HttpWebRequestMgt.SetReturnType('application/json');
    HttpWebRequestMgt.SetContentLength(0);
    
  • Options
    javiernunoariasjaviernunoarias Member Posts: 2
    Do you have an example of POST methode?
Sign In or Register to comment.