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 :
Microsoft Dynamics AX (Archived)

JSON String to Axapta MAP

(0) ShareShare
ReportReport
Posted on by

I have the below JSON string, which I am converting to a map and then reading the values based on the keys

str json = '{"PortalId" : "POR146", 
             "MemberId" : "MEM132", 
             "ParmContainer": [{"Key1":"First",
                                "Key2":"Second"},
                               {"Key1":"Third",
                                "Key2":"Fourth"}]}';


I am able to read the keys and values using the RetailCommonWebAPI::getMapFromJSONString.

The problem that I have is reading the values inside the parmContainer key. From the documentation that I found, the keys are stored as strings, and values are stored as containers. When I look inside the container, I get integer values instead of "First", "Second", "Third" and "Fourth".

while ( mapEnumerator.moveNext())
        {
            switch (mapEnumerator.currentKey().ToString())
            {
                case "ParmContainer"     :
                container test = mapEnumerator.currentValue();
                info(conPeek(test,1));
                info(conPeek(test,2));
                info(conPeek(test,3));
                info(conPeek(test,4));
            }
        }


Any ideas on how to solve this issue please?

*This post is locked for comments

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

    I would rather use classes. For example, you can create a C# class library, then generate classes by Edit > Paste Special > Paste JSON as Classes, use Newtonsoft.Json to parse the JSON string to these classes and use them in X++.

    Welcome to the world of .NET, full of options. :-)

  • Verified answer
    Community Member Profile Picture
    on at
    static void Job24(Args _args)
    {
        str json = '{"PortalId" : "POR146",          "MemberId" : "MEM132", "ParmContainer": [{"Key1":"First", "Key2":"Second"}, {"Key1":"Third","Key2":"Fourth"}]}';
        Map map;
        Map map1;
        List testList;
        
        ListEnumerator listEnumerator;
        container test;
        mapEnumerator mapEnumerator;
        map = RetailCommonWebAPI::getMapFromJsonString(json);
    
        mapEnumerator = map.getEnumerator();
        while ( mapEnumerator.moveNext())
            {
                switch (mapEnumerator.currentKey())
                {
                    case "ParmContainer"     :
                    test = mapEnumerator.currentValue();
    
                    testList =List::create(test);
    
                    listEnumerator = testList.getEnumerator();
    
                    while (listEnumerator.moveNext())
                    {
                        test = listEnumerator.current();
                        map1 = Map::create(test);
                        info(map1.lookup('Key1'));
                        info(map1.lookup('Key2'));
                    }
                }
            }
    }

    ?

  • Community Member Profile Picture
    on at

    Brilliant thanks, exactly what i needed!

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans