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 deserialize the JSON response in AX 2009

(0) ShareShare
ReportReport
Posted on by 745

Hi,

 Please let me know the code sample to deserialize or read the response values.I was able to read the first level of value but not the next level or the Array level.

Provided a bit of response sample that i need to deserialize.From below response,I was able to deserialize and get the value for A and return Value1,But i dont how to deserialize the other values B,B1,C1,D,E,F,F1,Email etc.Please help me with any sample code

{"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"}}

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

    For refererence, this looks closely related to the discussion that we have in Divya's other thread, How to read and pass the values Json String in AX 2009,Axapta Map.

    Divya, please show us your current code, so we can understand what you're doing and we can suggest what you should add.

  • Divya Lakshmi Profile Picture
    745 on at

    Hi,

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

    but I could able to find or deserialize and return the value only for {"data":

    {"A",................that is i was able to return value only for A and not for others,Since my response was like the onw i have posted in my Question and not like the one in the link i have used.

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

    Show us your code, please.

    Also, let me format your JSON, so we can see the structure more easily:

    {
      "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 you're saying that you're able to access A but not B, please explain what problem you have with B.

  • 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;
    }
    

    I couldn't retrieve B,C or D and other path elements and its values.

    Job...Code to retrieve the array of object results is 

    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));
    }

    My JSON repose string is 

    json = {
    "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"
    }
    }

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

    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

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

    You showed code for getting A, but - if I understand it correctly - you said that it works. So it's not that interesting.

    But you're unable to extract values of B, right? Therefore that's what we should focus on. Show us your code for retrieving B and explain what problem you have with it. For example, are you able to get JObject representing B? Are you able to access its ValueB1 property?

  • Divya Lakshmi Profile Picture
    745 on at

    Hi,

    Yes,I don't know how to extract values for B,C and so on.

    I tried same as below. and of no use since the format "data.A.B" is wrong I think

    if(reader.isFound(strfmt("data.A.B", i)))

    {

    strResult = reader.getStringNode(strfmt("data.A.B", i));

    info(strfmt("%1", strResult));//, intResult));

    }

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

    I immediately see two problems:

    • Your code assumes that B is a child of A, which isn't true.
    • And it assumes that B is string, which isn't true either. B contains an object. You should get JObject representing B and get ValueB1 from it.

    By the wat, please don't forget using Insert > Code to paste source code.

  • Divya Lakshmi Profile Picture
    745 on at

    Hi,

    Could you please help to fix the code or rewrite the code to extract B's Value.

    Because ,I tried directly to find data.B or to B ,But still it didn't find anything

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

    I think that using JsonReader class from that blog post is actually making things more difficult for you, because it's written for a different purpose. Let's ignore it, at least for know. Try something like this instead:

    Newtonsoft.Json.Linq.JObject rootJObj;
    Newtonsoft.Json.Linq.JObject dataJObj;
    Newtonsoft.Json.Linq.JObject bJObj;
    Newtonsoft.Json.Linq.JObject b1JObj;
    Newtonsoft.Json.Linq.JArray valueB1Array;
    int childCount;
    ;
    rootJObj = Newtonsoft.Json.Linq.JObject::Parse(json);
    dataJObj = rootJObj.get_Item('data');
    bJObj = dataObj.get_Item('B');
    valueB1Array = bJObj.get_Item('ValueB1');
    childCount = valueB1Array.get_Count();

  • Divya Lakshmi Profile Picture
    745 on at

    Hi,

     I was able to retrieve the count of B1 ,returns the count 1 considering "ValueB1",But i couldn't return the nodes c1 and its values or couldn't return the value/String  as "ValueB1" for B1.....Likewise i was able to return the string value for B ....returns {"B1":{"ValueB1":[{"c1":"Test c1","c2":"c2 value"}]}

    Please let me know how to extract the values for B1,c1 etc...Without hardcoding the node name or string name.

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
Martin Dráb Profile Picture

Martin Dráb 663 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 348 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans