web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

issue with iterate json array with RetailCommonWebAPI

(0) ShareShare
ReportReport
Posted on by 1,909

Hello,

I have two json arrays in below code. The first json array I called _jsonStrone and I don't have any issue iterating the values. But in second _jsonStrtwo array I have issue  to iterate the values of array and my real api doesn't contain any main key value to use in switch function to create a case. So I need iterate any element of array _jsonStrtwo and print the results even array doesn't contain key value like _jsonStrone as "branchList". Please advise.

static void readJSON(Args _args)
{
mapEnumerator mapEnumerator;
Map data;
List testList;
str _jsonStrone, _jsonStrtwo;
container test;
ListEnumerator listEnumerator;
int containerList;
Map map1;
int lengthIterate;
str _conpkrsult;

;
 _jsonStrone = '{"branchList":["11102206","12104348","603","604","605","606","607"]}';
 _jsonStrtwo = '[{"name":"Harry Potter","city":"London"},{"name":"Don Quixote","city":"Madrid"},{"name":"Joan of Arc","city":"Paris"},{"name":"Rosa Park","city":"Alabama"}]';
 
  
data = RetailCommonWebAPI::getMapFromJsonString(_jsonStrone); //_jsoStrtwo. 
mapEnumerator = data.getEnumerator();
while (mapEnumerator.moveNext())
{
switch (mapEnumerator.currentKey())
{
case "branchList" :
test = mapEnumerator.currentValue();
testList =List::create(test);
listEnumerator = testList.getEnumerator();
while (listEnumerator.moveNext())
{
test = listEnumerator.current();

 _conpkrsult = conpeek(test, 1);

  print _conpkrsult;
    pause;
}
break;
}
}
   
}

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

    Please add line indentation to your code. It's very important for readability, and you decrease your chances for getting an answer if you make it complicated for us.

    Anyway, using getMapFromJsonString() looks wrong if the JSON don't contain a map. It represents an array of objects.

    I don't see any suitable method in RetailCommonWebAPI, therefore either don't use it, add such a method or modify your JSON string to give it the structure expected by getMapFromJsonString().

  • Faqruddin Profile Picture
    1,909 on at

    Thanks martin. Please if you know any alternative way without using RetailCommonWebAPI methods, Please suggest. I need to read any json format regardless of structure.

  • Martin Dráb Profile Picture
    239,559 Most Valuable Professional on at

    If you open RetailCommonWebAPI, you'll see that it uses System.Web.Script.Serialization.JavaScriptSerializer. You could use that directly. I guess that it can deserialize arrays (but I don't use it, therefore I don't know).

    The most common library for this purpose is Newtonsoft.Json. If you know the structure up front, you can easily generate classes to deserialize the JSON to and then work with the data in strongly-typed manner. If you don't, you can deserialize JSON to a JObject and use its methods to manipulate the data.

    As always, there is no single approach that is the best in all cases. The choice depends on your particular situation.

  • Faqruddin Profile Picture
    1,909 on at

    Let me see System.Web.Script.Serialization.JavaScriptSerializer. As I see the method it supporting array of objects. But even I don't know how its deserialize the JSON array elements with RetailCommonWebAPI complex json array structures. I hope some one know this and comment it. Regarding Newtonsoft.Json library please if you know how to attach to ax 2012 r3. Please guide with an example.

    Thank you

  • Martin Dráb Profile Picture
    239,559 Most Valuable Professional on at

    An example of what exactly? I don't to waste with explaining a scenario that you aren't actually interested in.

  • Faqruddin Profile Picture
    1,909 on at

    Please read my comment above. You advise the most common used library to deserialize Newtonsoft.Json. I request in my last comment if you know the approach please advise with an example . I found an example below is a link and I hope it help someone. So no need more explanations. Appreciated your time. Thank you.

    axgrind.azurewebsites.net/.../

  • Martin Dráb Profile Picture
    239,559 Most Valuable Professional on at

    And I already mentioned quite different way how to use the library - one is generating classes for your JSON and deserializing JSON to those classes, and one deserializing to JObject. You can also use the library either from X++ through .NET Interop, or creating a C# class library and doing the work there, which especially useful when you want to use "Generate classes from JSON" and utilize generic types and methods.

    The blog post shows deserialization to JObject through .NET Interop, but it's impossible to say whether it's what you need unless you specify your requirements. It would be a mistake to think that this is the only option.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 661 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 620

#3
Subra Profile Picture

Subra 473

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans