Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

How to read and pass the values Json String in AX 2009,Axapta Map

(0) ShareShare
ReportReport
Posted on by 745

Hi,

 Please let me know the sample code on how to pass the values in Array and read the values from Array in AX 2009.

I was able to find lots of code in AX 2012 and above but not for 2009.Appreciate your response with the sample codes.

For example: I need to pass body data for below;

Body data: {"ID 1": "001",
"currency": "SGD",
"item_steps": [
{
"item_id": 0,
"type": "Test"
},
{
"item_id": 0,
"type": "Test2"
}
],
"steps": [
{
"quantity": 4,
"address": "20 Road"

}

]

}

Thanks and Regards,

Divya Lakshmi J

  • Divya Lakshmi Profile Picture
    745 on at
    RE: How to read and pass the values Json String in AX 2009,Axapta Map

    Hi,

    OK

  • Martin Dráb Profile Picture
    231,772 Most Valuable Professional on at
    RE: How to read and pass the values Json String in AX 2009,Axapta Map

    Now we're discussing exactly the same thing in two threads of yours: this one and How to deserialize the JSON response in AX 2009. Let's keep this one for serialization and discuss deserialization in the other thread.

  • Divya Lakshmi Profile Picture
    745 on at
    RE: How to read and pass the values Json String in AX 2009,Axapta Map

    Hi MArtin,

     I have copied the same code from the link and pasted here.Please help me to modify the code that would return the value for the sample response i have attached in my question.

    class JsonReader
    {
        Newtonsoft.Json.Linq.JObject    jObject;
    }
    
    public void loadJson(str _json)
    {
        ;
    
        jObject = Newtonsoft.Json.Linq.JObject::Parse(_json);
    }
    
    public static JsonReader parseJson(str _json)
    {
        JsonReader 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;
        }
    }
    
    
    
    public real getRealNode(str path)
    {
        return this.traversePath(path);
    }
    
    public int getIntNode(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;
    }
    
    
    
    job to retrieve the array of objects
    
    JsonReader  reader;
    str         json,
                strResult;
    int         i,
                intResult;
    ;
    
    json = "{\"object\":[{\"Property\":\"Alpha\",\"Value\":1},{\"Property\":\"Beta\",\"Value\":2},{\"Property\":\"Gamma\",\"Value\":3}]}";
    
    reader = JsonReader::parseJson(json);
    
    for (i = 1; reader.isFound(strfmt("object.%1.Property", i)); i  )
    {
        strResult = reader.getStringNode(strfmt("object.%1.Property", i));
        intResult = reader.getIntNode(strfmt("object.%1.Value", i));
        info(strfmt("%1 = %2", strResult, intResult));
    }
    

    Here I have made slight change to retrieve my first level data that is to return A's value only since my response is 

    {"data":
    {"A":"value1=","B":{"B1":{"ValueB1":[{"c1":"Test c1","c2":"c2 value"}},
    "D":{},
    "E":{}},
    "F":[{"F1":"F1 value","F2 lang":"en"},
    "email":"testEmail id",
    "tokens":{"G1_token":"test token value"},
    "name":"Test name","phone":"123456"}}

    //////////////////////////////////

    if(reader.isFound(strfmt("data.A", i)))
    {
    strResult = reader.getStringNode(strfmt("data.A", i));
    info(strfmt("%1", strResult));//, intResult));

    Please let me know how to retrieve the value for other nodes and forloop

    and let me know code sample to pass values for the Body data to pass in Json request string and format of my request body data is posted in my question

  • Martin Dráb Profile Picture
    231,772 Most Valuable Professional on at
    RE: How to read and pass the values Json String in AX 2009,Axapta Map

    Sorry, AX 2009 is a very old version and I don't remember which classes were available there.

    If you want to use Newtonsoft.Json and you reject my solution, I think the only other way is using JObject.

    The link doesn't work - it looks incomplete. Can you provide a correct one, please?

  • Divya Lakshmi Profile Picture
    745 on at
    RE: How to read and pass the values Json String in AX 2009,Axapta Map

    Hi,

    I have used the same code from the link.axgrind.azurewebsites.net/.../

    Is that possible to add any code for serializing in this class?

  • Divya Lakshmi Profile Picture
    745 on at
    RE: How to read and pass the values Json String in AX 2009,Axapta Map

    Hi MArtin,

    RetailCommonWebAPI class is available only in Ax 2012.I tried to replicate the same in AX 2009 ,But i faced lot of issues where i cant replicate some system classes.

    Is that possible to help me with Ax class code samples without using custom class library deployments.

  • Martin Dráb Profile Picture
    231,772 Most Valuable Professional on at
    RE: How to read and pass the values Json String in AX 2009,Axapta Map

    If you want to use Newtonsoft.Json, I recommend creating a custom C# class library. There create classes for your data - you can generate them from JSON with Edit > Paste Special > Paste JSON as Classes.

    Then you'll use .NET Interop to create instances of these classes and populate them with values.

    I believe that this syntax for working with CLR array worked already in AX 2009:

    MyNamespace.MyClass[] myArray = MyNamespace.MyClass[](2);
    myArray.SetValue(myObject, 0);

    Then you can assing the array to an array property of another object:

    parentObject.set_ArrayProperty(myArray);

    An alternative may be using RetailCommonWebAPI class. I don't remember it well, but I think you would use nested maps there. It may be easier for you, because you won't have to deal with deployments of the custom class library.

  • Divya Lakshmi Profile Picture
    745 on at
    RE: How to read and pass the values Json String in AX 2009,Axapta Map

    Hi Martin,

    I don't know which JSON Serializer to Use in AX 2009. Currently ,I tried to use Newtonsoft.JSON dll to do so.But,I was able to deserialize only first level of data from the response.I don't have any sample code to for serializing.Sample values i have attached in my first example code itself.For example column type is Test,Value for quantity is 4 ,value for address is 20 Road..Those are examples

  • Martin Dráb Profile Picture
    231,772 Most Valuable Professional on at
    RE: How to read and pass the values Json String in AX 2009,Axapta Map

    All right, but which JSON serializer are you using? What data does it expect on input?

  • Divya Lakshmi Profile Picture
    745 on at
    RE: How to read and pass the values Json String in AX 2009,Axapta Map

    Hi Martin,

     Yes, I'm talking about JSON serializer,I didn't know how to pass values for Item_id and other columns under Item_steps and likewise quantity and other columns under steps .....You can notice the body request sample i have attached,So ,Please let me know the code sample for how to pass values for Item_id,type ,quantity ,address which under item_Steps and steps

    Regards,

    Divya Lakshmi

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,892 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,772 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans