Friday 10 September 2021

Read JSON file data in AX 2009 using batch processing

Today, I will share the code snippet which can be used to read a json
file in AX 2009.

We will be using .NET Newtonsoft.Json.dll file to proceed with this work.

Download the Newtonsoft.Json.dll  and place it on this path : D:\Program Files (x86)\Microsoft Dynamics AX\50\Client\Bin.


We will create a new class with name as 'JSONReaderWrapper' and add following methods in it.

class JSONReaderWrapper
{
   Newtonsoft.Json.Linq.JObject    jObject;
}

//Add these methods
public int getIntNode(str path)
{
    return this.traversePath(path);
}

public real getRealNode(str path)
{
    return this.traversePath(path);
}
public str getStringNode(str path)
{
    return System.Convert::ToString(this.traversePath(path));
}
public boolean isFound(str _path)
{
    return this.traversePath(_path) != null;
}
public void loadJson(str _json)
{
    ;
    jObject = Newtonsoft.Json.Linq.JObject::Parse(_json);
}

public static JSONReaderWrapper parseJson(str _json)
{
    JSONReaderWrapper reader = new JsonReader();
    ;

    reader.loadJson(_json);
    return reader;
}
private anytype traversePath(str                               path,
                             Newtonsoft.Json.Linq.JContainer   obj = jObject)
{
    List                            pathElements;
    ListEnumerator                  le;
    Newtonsoft.Json.Linq.JValue     value;
    Newtonsoft.Json.Linq.JToken     token;
    Newtonsoft.Json.Linq.JTokenType thisType,
                                    nestedType;
    Newtonsoft.Json.Linq.JObject    newObject;
    Newtonsoft.Json.Linq.JArray     newArray;
    str                             current,
                                    thisTypeString,
                                    nestedTypeString;

    #define.JObject("Newtonsoft.Json.Linq.JObject")
    #define.JArray ("Newtonsoft.Json.Linq.JArray")

    ;

    pathElements = strSplit(path, @".\/");

    le = pathElements.getEnumerator();

    if (le.moveNext())
    {
        current = le.current();

        thisType = obj.GetType();
        thisTypeString = thisType.ToString();

        switch (thisTypeString)
        {
            case #JObject:
                token = obj.get_Item(current);
                break;
            case #JArray:
                token = obj.get_Item(str2int(current) - 1);
                break;
            default:
                return null;
        }

        if (token)
        {
            nestedType = token.GetType();
            nestedTypeString = nestedType.ToString();

            if (nestedTypeString != #JObject && nestedTypeString != #JArray)
            {
                switch (thisTypeString)
                {
                    case #JArray:
                        return obj.get_Item(str2int(current) - 1);
                    case #JObject:
                        return obj.get_Item(current);
                    default:
                        return null;
                }
            }

            switch (nestedTypeString)
            {
                case #JObject:
                    newObject = Newtonsoft.Json.Linq.JObject::FromObject(token);
                    return this.traversePath(strDel(path, 1, strLen(current) + 1), newObject);
                case #JArray:
                    newArray = Newtonsoft.Json.Linq.JArray::FromObject(token);
                    return this.traversePath(strDel(path, 1, strLen(current) + 1), newArray);
                default:
                    return null;
            }
        }
        else
        {
            return null;
        }
    }
    else
    {
        return null;
    }
}

//Create another class which extends RunBaseBatch and add a method in it.

Json input string  = "{\"ItemDef\":[{\"ItemId\":\"Test1\",\"Price\":1},{\"ItemId\":\"Test2\",\"Price\":2},{\"ItemId\":\"Test3\",\"Price\":3}]}";
client server static  void readJsonFileData(str _jsonInput)
{
    str              jsonResult;
    System.Exception ex;
    str              itemId;
    str              quantity;
    str              conditionCode;
    real             intResult;
    container        jsonConResult;
    int              i;
    JSONReaderWrapper   reader;
;


    try
    {
        jsonResult = _jsonInput;

        new InteropPermission(InteropKind::ClrInterop).assert();

        reader = JSONReaderWrapper::parseJson(jsonResult);

        for (i = 1; reader.isFound(strfmt("ItemDef.%1.ItemId", i)); i++)
        {
            itemId = reader.getStringNode(strfmt("ItemDef.%1.ItemId", i));
             price = reader.getIntNode(strfmt("ItemDef.%1.Price", i));
             info(strfmt("%1 = %2", itemId, price));

        }


     }
     catch(Exception::CLRError)
     {
        ex = CLRInterop::getLastException().GetBaseException();
        error(ex.get_Message());
     }

}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Custom Business events Part 3 - (Activate custom business event) in D365 F&O

 In this blog we will discuss about the steps to activate a custom business in D365 F&O. As we know that business event catalog does not...