Application Programming Interfaces C#

Dynamic JSON Usage with Newtonsoft

This C# program demonstrates how to create, access, and modify a JSON document dynamically with the Newtonsoft Json package. Newtonsoft.json is a Nuget package that must be installed in order to run this program.

Program.cs

using System;
// Requires Nuget Newtonsoft.Json package
using Newtonsoft.Json.Linq;

namespace JsonDynamicUsage
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var qJsonObject = new JObject();

            // Not dynamically added
            qJsonObject.Add("Scripture", new JObject());

            // Get the dynamic JObject version, which allows us to add properties like JavaScript
            dynamic qDynScriptureObject = (JObject)qJsonObject.GetValue("Scripture");

            qDynScriptureObject.Ecclesiates1 = "What does man gain by all the toil at which he toils under the sun?";
            qDynScriptureObject.Ecclesiates3 = "For everything there is a season, and a time for every matter under heaven:";
            // A dynamic array means that we can put any type into the array
            qDynScriptureObject.Seasons = new JArray() as dynamic;
            qDynScriptureObject.Seasons.Add("a time to be born, and a time to die;");
            qDynScriptureObject.Seasons.Add("a time to kill, and a time to heal;");
            qDynScriptureObject.Seasons.Add("a time to rend, and a time to sew;");

            dynamic qDynMainObject = qJsonObject;
            qDynMainObject.Beattitudes = new JArray() as dynamic;
            qDynMainObject.Beattitudes.Add("Blessed are the poor in spirit: for theirs is the kingdom of God.");
            qDynMainObject.Beattitudes.Add("Blessed are the pure of heart: for they shall see God.");

            var qAnotherObject = new JObject
            {
                { "Cardinal", new JArray() },
                { "Theological", new JArray() }
            };
            qDynMainObject.Add("Virtues", qAnotherObject);

            ((JArray)qJsonObject.SelectToken("Virtues.Cardinal")).Add("Fortitude");
            ((JArray)qJsonObject.SelectToken("Virtues.Cardinal")).Add("Temperance");
            ((JArray)qJsonObject.SelectToken("Virtues.Cardinal")).Add("Wisdom");
            ((JArray)qJsonObject.SelectToken("Virtues.Cardinal")).Add("Prudence");

            ((JArray)qJsonObject.SelectToken("Virtues.Theological")).Add("Faith");
            ((JArray)qJsonObject.SelectToken("Virtues.Theological")).Add("Hope");
            // Alternative method
            ((JArray)qJsonObject["Virtues"]["Theological"]).Add("Charity");

            var qAnotherArray = JArray.Parse(@"[
                    ""You shall not murder"",
                    ""You shall not steal""
                ]");
            qDynMainObject.Add("Commandments", qAnotherArray);


            Console.WriteLine(qJsonObject.ToString());
        }
    }
}
 

Output

{
  "Scripture": {
    "Ecclesiates1": "What does man gain by all the toil at which he toils under the sun?",
    "Ecclesiates3": "For everything there is a season, and a time for every matter under heaven:",
    "Seasons": [
      "a time to be born, and a time to die;",
      "a time to kill, and a time to heal;",
      "a time to rend, and a time to sew;"
    ]
  },
  "Beattitudes": [
    "Blessed are the poor in spirit: for theirs is the kingdom of God.",
    "Blessed are the pure of heart: for they shall see God."
  ],
  "Virtues": {
    "Cardinal": [
      "Fortitude",
      "Temperance",
      "Wisdom",
      "Prudence"
    ],
    "Theological": [
      "Faith",
      "Hope",
      "Charity"
    ]
  },
  "Commandments": [
    "You shall not murder",
    "You shall not steal"
  ]
}
Press any key to continue . . .
 
 

© 2007–2025 XoaX.net LLC. All rights reserved.