Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

How to differentiate a packed map from a packed list?

(0) ShareShare
ReportReport
Posted on by 805
Hi,

Is there any documentation about the container structure which is created when using map.pack() or list.pack() methods to pack corresponding objects to containers?

I am using RetailCommonWebAPI class, getMapFromJsonString method which "desterilize" Json to Map of containers (packed maps or lists).

I would like to do the following:
1. container con = map.lookup("somekey");
2. Depends on the packed object (list or map) to create a list or map using create static methods on the corresponding classes (List::create(con) or Map::create(con).

That's why I need to be able to differentiate the types of the packed objects.
Is that possible?
Regards,
  • ist Profile Picture
    805 on at
    RE: How to differentiate a packed map from a packed list?

    Thank you All!

    There was a good discussion.

  • Suggested answer
    Pedro Tornich Profile Picture
    955 on at
    RE: How to differentiate a packed map from a packed list?

    After reading the other answers here, I believe we can assume that the Map key type will always be string and that the List type will always be container, because a JSON object list will be translated to a list of packed maps by the RetailCommonWebAPI class.

    With that in mind, I think you can just check the second position on the container and if it's Types::String, then it's a packed map, otherwise it's a list.

  • Verified answer
    Evaldas Profile Picture
    1,800 on at
    RE: How to differentiate a packed map from a packed list?

    I have not found any official documentation on the serialized maps or lists.

    This is my assumption. What else could it be?

    When you create a map you specify a key type and a value type by using an enum Type.

    pastedimage1572369028359v1.png

    Number 7 represent one of the types. I guess a container. Number 0 represents a String.

    If you query this enum values in your environment you will probably see the same result.

    pastedimage1572369051821v2.png

  • ist Profile Picture
    805 on at
    RE: How to differentiate a packed map from a packed list?

    Hi Evaldas,

    You mentioned that the 3rd is the type of the map value. Is there any official documentation for that?

    What does 7 mean in context of the map value?

    Regards,

  • Evaldas Profile Picture
    1,800 on at
    RE: How to differentiate a packed map from a packed list?

    Regarding packed map and list. The second parameter in the packed list defines the type of elements in the list.

    The second parameter in the packed map is the type of the map key. The 3rd is the type of the map value.

    You can use it if you know for sure that the types will not change.

  • ist Profile Picture
    805 on at
    RE: How to differentiate a packed map from a packed list?

    Hi Martin,

    Thanks for your recommendations and guidelines! I have read other articles written by you about this topic, so I am absolutely agree with you, that this is the right way.

    Lets forget about using the retail function and focus on the question. Is there a way to differentiate packed list from packed map?

    The only way I found is by the second element (0 for packed map and 7 for packed list) of the containers (based on their leading elements):

    Map:

    [0] 3

    [1] 0

    [2] 7

    [3] x (count)

    ...

    List:

    [0] 3

    [1] 7

    [2] x (count)

    Do you think we can trust on that?

    Do you know if it's documented somewhere?

    Regards,

  • Verified answer
    Martin Dráb Profile Picture
    233,025 Most Valuable Professional on at
    RE: How to differentiate a packed map from a packed list?

    My recommendation is to stop using the retail function, because it's useful only a few specialized cases. It's not a general-purpose deserialization method and you seemed to run into one of many of its limitations.

    Deserialization should give you objects, which isn't happen in your case - you just get data serialized in a different way and you loose important information on the way. It just makes the problem worse.

    I would do the following:

    1. Create a C# project in the same solution as your X++ project.
    2. Create/open a C# class.
    3. Take a sample JSON string, put it into the clipboard and use Edit > Paste special > Paste JSON as classes.
      This will create all classes necessary to deserialize the JSON string. If the JSON contains a hierarchy of different objects, you need multiple classes to be able to deserialize this string to an object graph. That's where things fail with getMapFromJsonString(), because it's trying to deserialize a hierarchy to a map that can hold only a single level.
    4. Use Newtonsoft.Json library to do the deserialization.
    5. Wrap the code in a public method that you can easily call from X++.
    6. Build the C# project, add a project reference to the X++ project and call your method from X++.
    7. Use the deserialized objects in your code.
      Notice how easier and safer it is in comparison with the original solution, because you're now working with named classes and properties and you get code completion in code editor and name and type checking at compilation.
  • Evaldas Profile Picture
    1,800 on at
    RE: How to differentiate a packed map from a packed list?

    Or you can always try to guess the structure. E.g. if the 3rd element value + 3 = conlen(container) then its the list. Or something like that.

  • Evaldas Profile Picture
    1,800 on at
    RE: How to differentiate a packed map from a packed list?

    That's true. Do you have control of what's in the JSON file?

  • ist Profile Picture
    805 on at
    RE: How to differentiate a packed map from a packed list?

    I tried this approach. It is applicable only if try catch block is not inside a transaction block, because i.e. if we try to create a map from container that has been created from packed list, it will throw an exception that need to be handled in catch block before we try to create a list. As we know handling an exception in transaction is not possible.

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March 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... 294,283 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,025 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans