Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Parsing Dynamic JSON without External Assemblies

Posted on by Microsoft Employee

Hi I am trying to parse a JSON response that contains dynamic keys and values. Below is an example of the JSON I am parsing:

 {
  "data": [
    {
      "Id": "XXXXXXXXXXXXXXXXXXXXXXXX",
      "IsActive": true,
      "DateCreated": "2017-11-09T00:01:49.827Z",
      "DateModified": "2017-11-09T00:01:49.827Z",
      "IsDeleted": false,
      "Uid": "XXXXXXXXXXXXXXXXXXXXXXXX",
      "CustomObject": {
        "customdata1": " Store my customdata1 value",
        "customdata2": " Store my customdata2 value"
      }
    }
  ],
  "Count": 1
}

Where CustomObject is the dynamic object that I am trying to parse. Another option for me is to simply convert the value of CustomObject into a string, but I am also unable to find a method for that.

*This post is locked for comments

  • Suggested answer
    Temmy Wahyu Raharjo Profile Picture
    Temmy Wahyu Raharjo 2,914 on at
    RE: Parsing Dynamic JSON without External Assemblies

    Hi,

    For this requirement usually I will use this helper:

    /// <summary>

           /// Deserialize json string to an instance of type T using DataContractJsonSerializer.

           /// </summary>

           public static string ToJson<T>(this T obj)

           {

               using (var stream = new MemoryStream())

               {

                   var serializer = new DataContractJsonSerializer(typeof(T));

                   serializer.WriteObject(stream, obj);

                   return Encoding.UTF8.GetString(stream.ToArray());

               }

           }

           /// <summary>

           /// Serialize from an instance of type T to json string using DataContractJsonSerializer.

           /// </summary>

           public static T FromJson<T>(this string json)

           {

               using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))

               {

                   var serializer = new DataContractJsonSerializer(typeof(T));

                   return (T) serializer.ReadObject(stream);

               }

           }

    This helper will help you to change your object from list data/object to json string (ToJson) and FromJson will help you to parse from string into object.

  • PhuocLV Profile Picture
    PhuocLV 347 on at
    RE: Parsing Dynamic JSON without External Assemblies

    I use this [View:https://github.com/facebook-csharp-sdk/simple-json.git]

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans