web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at

    First of all, let me make your code readable. Please don't forget to use Insert > Code next time.

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

    Then please give us more information. When you say "pass the values in Array", where do you want to pass them? Are you talking about a JSON serializer? Or are you creating JSON as string by yourself?

  • Divya Lakshmi Profile Picture
    745 on at

    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

  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at

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

  • Divya Lakshmi Profile Picture
    745 on at

    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
    237,990 Most Valuable Professional on at

    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

    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.

  • Divya Lakshmi Profile Picture
    745 on at

    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?

  • Martin Dráb Profile Picture
    237,990 Most Valuable Professional on at

    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

    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
    237,990 Most Valuable Professional on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 490 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 241 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans