Options

JSON Objects Supported?

parambparamb Member Posts: 85
edited 2015-03-01 in NAV Three Tier
Does Nav dynamic version <> support JSON processing? I have a scenario to call external web service(REST), which takes in JSON and responds back in JSON. How to construct and parse JSON data in NAV.

Comments

  • Options
    kinekine Member Posts: 12,562
    I expect you can use DotNet assembly to read the data as in common .NET environment. But I never worked with JSON in NAV.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    deV.chdeV.ch Member Posts: 543
    Use .net Interop and do the processing (request, Response etc) in an external assembly. This could be a starting point: http://www.codeproject.com/Articles/233698/Consuming-a-Json-WebService-from-a-Csharp-or-VB-Ap
  • Options
    SiStSiSt Member Posts: 46
    Creating JSON data in NAV is quite easy. Parsing JSON data is possible, but can be tricky, depending on the actual data structure. As you will need .NET for the webservice call it might be a good idea to build an external assembly using JSON.NET http://james.newtonking.com/json, for example. Using the library directly from NAV is hard to impossible, because it uses some .NET constructs that aren't accessible from NAV. I would use the .NET approach if it should only be deployed on one server and try to do it in NAV with standard .NET libraries if it should be deployed on more systems.

    Let me know if you need an example of creating JSON in NAV or if you need help with JSON.net.
  • Options
    parambparamb Member Posts: 85
    Thanks for all the replies.
  • Options
    parambparamb Member Posts: 85
    SiSt wrote:
    Creating JSON data in NAV is quite easy. Parsing JSON data is possible, but can be tricky, depending on the actual data structure. As you will need .NET for the webservice call it might be a good idea to build an external assembly using JSON.NET http://james.newtonking.com/json, for example. Using the library directly from NAV is hard to impossible, because it uses some .NET constructs that aren't accessible from NAV. I would use the .NET approach if it should only be deployed on one server and try to do it in NAV with standard .NET libraries if it should be deployed on more systems.

    Let me know if you need an example of creating JSON in NAV or if you need help with JSON.net.

    Thanks for your offer to help.

    The following the data structure for JSON request and Response.
    Request:
    {
    "type": "NEWItem_REQUEST:#NewItem",
    "requestId": "PRE-PE-Item",
    "partDescr": "Test",
    "uom": "EA",
    "Price": 10
    }
    Response:
    {
    "SendNEWItem_REQUESTResult": {
    "DangeroudCode": "O",
    "Flag": "N",
    "OrderQuantity": 0,
    "errorTable": [
    {
    "errorItem": "itemNumber",
    "errorCode": "00",
    "errorMessageShort": "Item Successfully Added",
    "errorMessageLong": "New Item [1909876] was successfully created"
    }
    ],
    "inventoryClass": "ABC",
    "secondaryVendorId": "",
    "flameFlag": "N",
    "catalogs": "LM"
    }
    }
  • Options
    parambparamb Member Posts: 85
    I'll explain my case a bit more. In my case, Nav cannot create an Item with out a corresponding Item being created in a legacy application. Right now this is handled by Job Queues which is executed during non working hours. In short there is a one day delay. What we are trying to do is to interact with the Legacy application using web service: Nav send the request to the legacy applications web service and receives the response back when the Item is created.

    "I would use the .NET approach if it should only be deployed on one server and try to do it in NAV with standard .NET libraries if it should be deployed on more systems."

    Based on your above comment, I guess I will have to use standard .NET libraries in Nav.
  • Options
    SiStSiSt Member Posts: 46
    Building the request string:
    RequestText := STRSUBSTNO(
                                  '{"type": "NEWItem_REQUEST:#NewItem","requestId": "PRE-PE-Item","partDescr": "%1","uom": "EA","Price": %2}',
                                  'Description 123', 15);
    

    Call the webservice, if you're using 2013 you can use WCF or the WebClient from the .NET framework (see http://msdn.microsoft.com/en-us/library/hh169399(v=nav.71).aspx, http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client)
    If you're using 2009 you can also use the MSXML automation (it can also process text).

    Parsing the JSON data:

    PROCEDURE HandleResult@5326064(Value@5326062 : Text[1024]);
    VAR
      i@5326063 : Integer;
      Mode@5326064 : Integer;
      Key@5326065 : Text[1024];
      KeyValue@5326066 : Text[1024];
    
    Mode := 1;
    
    FOR i := 1 TO STRLEN(Value) DO BEGIN
      case mode of
        0: // Search for key end
          BEGIN
            IF Value[i] = '"' THEN BEGIN
              Mode := 6;
            END ELSE Key := Key + FOrmat(Value[i]);
          END;
        1: // Search for key begin
          BEGIN
            IF Value[i] = '"' THEN BEGIN
              Mode := 0;
            END;
          END;
        2: // Handle complex data types not required in this case...
          BEGIN
            //message('Complex Type ' + Key);
            Key := '';
            IF Value[i] = '"' THEN Mode := 0
            else Mode := 1;
          END;
        3: // Search for Value end
          BEGIN
            IF Value[i] = '"' THEN begin
              Mode := 1;
              ProcessKeyValue(Key, KeyValue);
              Key := '';
              KeyValue := '';
            end ELSE KeyValue := KeyValue + Format(Value[i]);
          END;
    
        4: // Search for Value begin
          BEGIN
            IF (Value[i] = '{') OR (Value[i] = '[') THEN
              Mode := 2;
            IF Value[i] = '"' THEN
              Mode := 3;
            IF Value[i] IN ['0'..'9'] THEN begin
              KeyValue := KeyValue + Format(Value[i]);
              Mode := 5;
            end;
          END;
        5: // Numeric value
          BEGIN
            IF Value[i] IN ['0'..'9'] THEN
              KeyValue := KeyValue + Format(Value[i])
            ELSE begin
              Mode := 1;
              ProcessKeyValue(Key, KeyValue);
              Key := '';
              KeyValue := '';
            end;
          END;
        6: // Search for :
          BEGIN
            IF Value[i] = ':' THEN MOde := 4;
          END;
      END;
    END;
    
    PROCEDURE ProcessKeyValue@5326066(Key@5326062 : Text[1024];Value@5326063 : Text[1024]);
    BEGIN
      MESSAGE('Key %1 = %2', Key, Value);
    END;
    

    With the code above I could parse the JSON you posted. It does not work for all JSON results, but hopefully give you an idea how it is possible to implement a JSON parser in NAV.
  • Options
    rthswrthsw Member Posts: 73
    Hi,
    just for the case someone is interested:
    for connecting a Navision with Shopware i wrote my own REST/JSON Wrapper direct in Navision without any additional software.

    Natural there is a external component, but only Microsoft components you find on every Windows-computer since 2003/Xp, so you don't need to setup any DLL's, programms or whatever on the using client computers.

    Currently the page is only in German because Shopware is very (only?) popular in Germany, but the examples are easy to read (just ignore the german text).

    https://sites.google.com/site/renethoen ... e-mit-josn

    If you want to drive a Shopware system out of Navision you might also be interested in a complete PIM Solution direct inside of Navision.
    Also in German but with a lot of explaining Pictures:

    https://sites.google.com/site/renethoene/navision/pim

    Hope i can help someone with this. Both Solutions are also to sell. Documentation will be more in the future.
  • Options
    gtrgtr Member Posts: 131
    Hello
    I visited your German Navision Shopware page - I'm looking for help to make up a specific Navision code to communicate with JSON - Can you tell me more about your api code unit ?
    Thanks
Sign In or Register to comment.