Serialize JSON in AX

JSON is a format that encodes objects in a string and Serialization means to convert an object into that string. So below is an example to how to serialize JSON in AX ( X++)

static void serializeJSON(Args _args)
{
    int iterator;
    str myJson;
    System.Object obj;
    
    //using javascript web script serializer
    System.Web.Script.Serialization.JavaScriptSerializer serialize = new System.Web.Script.Serialization.JavaScriptSerializer();
    
    
    // declare and set array to store multiple values
    System.Type listType = System.Type::GetType('System.Collections.ArrayList');
    CLRObject arrayList = System.Activator::CreateInstance(listType);

    new InteropPermission(InteropKind::ClrInterop).assert();
    
    // get value and store in object
    for(iterator = 0; iterator <= 5 ; iterator++)
    {
        obj = strFmt('%1 is the value %2', iterator, DateTimeUtil::utcNow());
        // add object in array
        arrayList.Add(obj);
    }

    // Use serialize method to serialize the content in JSON.
    myJson = serialize.Serialize(arrayList);

    info(myJson);
}

Hope it help you guys to achieve what you need. Happy DAXing.

Comments