Dynamic JSON property addition in C# at runtime

Below is an example of my JSON structure:

[
  {
    "id": "test.txt",
    "fields": {
      "_str.application": [ "Text File" ],
      "_str.body": [ "asdsadasd" ],
      "_str.mimetype": [ "text/plain" ]
    }
  }
]

Is there a way to dynamically add new properties to fields using C#?

I've already set up for Serialization.

public class Fields
{
    [JsonProperty("_str.application")]
    public IList<string> _str_application { get; set; }

    [JsonProperty("_str.body")]
    public IList<string> _str_body { get; set; }

    [JsonProperty("_str.mimetype")]
    public IList<string> _str_mimetype { get; set; }
}

public class Document
{
    [JsonProperty("id")]
    public string id { get; set; }

    [JsonProperty("fields")]
    public Fields fields { get; set; }
}

I am looking for a way to dynamically include additional JsonPropertys within Fields during runtime.

Answer №1

One way to dynamically add fields to a JSON object is by using Json.NET for deserialization and serializing again:

string jsonData = "{'Name':'John'}";
dynamic person = JObject.Parse(jsonData);
person.Age = 30;
jsonData = JsonConvert.SerializeObject(person);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generate a new tree structure using the identifiers of list items within an unorganized list

Attempting to convert the id's of li's in a nested unordered list into valid JSON. For example, consider the following list. To be clear, the goal is not to create the UL list itself, but rather the JSON from the li id's <ul class="lis ...

"Exploring the world of JSON in the realm of

I am attempting to showcase JSON data using jQuery Mobile Is there something incorrect on this page? Check out my code here! Thank you! ...

Guide on utilizing the retrieved information from a URL to choose specific JSON data in Django

Suppose I have a GET request with URL domain/search/?value=123 and the following JSON data: [ { "id": 1, "value": 123, "value2": 123123 }, { "id": 2, "value": 1, "value2": 214 } ] I want to retrieve data where value = 123. ...

The AJAX request is not triggered before the postback when using a LinkButton in ASP

I am facing an issue with running an insert statement using a button in my application. Although it is functional, I have noticed that whenever I click the button, the Page_Load function runs before the ajax statement executes. How can I ensure that the a ...

Exploring deeply nested objects within Express by iterating through them

I am trying to figure out how to iterate through objects in Express.js. I can retrieve information from the JSON file, but when I attempt to loop through it, I keep getting an error saying that it's not defined. What could I be missing here? My goal ...

Issue with Google Calendar: Unable to locate com.google.api.client.json.JsonFactory.fromInputStream

Currently, I'm developing a Spring-MVC application and I'm facing an issue while trying to integrate Calendar functionality. The problem seems to be related to authentication due to a json error. I have been using Googles sample code but it appea ...

Exploring the Power of SQL in JSON Parsing (OPENJSON)

If you want to access your Google searches data, you can download it in the form of multiple JSON files. I am currently working on parsing them into columns named [TimeStamp] and [Query Text] using the SQL function OPENJSON. DECLARE @json as nvarchar(max) ...

The system encountered an issue with converting org.json.JSONObject to JSONArray

I am having an issue with a PHP file in my Android project. When I try to display the array data from my PHP code, I encounter an error stating "error parsing data org.json.JSONException ... error org.json.JSONObject cannot be converted to JSONArray." Bel ...

The LINQ to Entities software is unable to identify the method 'Newtonsoft.Json.Linq.JToken get_Item(System.String)' method

Below is a working code snippet: string data = Encoding.UTF8.GetString(eventData.GetBytes()); JObject o = JObject.Parse(data); var disp = db.Dispositivos .Where(p => p.ClaveDispositivo == "UM5WOkRIFtS9dWbM5f1YM/ncpdrpSYrh3zND9Y/YHM4="); if(disp.T ...

Leveraging the power of PHP and AJAX for seamless transmission and reception of JSON data through a

I am in the process of creating a basic form that consists of a single text field and a submit button. The goal is to have the entered value posted to a $.getJSON method upon clicking the button, which will then retrieve a JSON response and display it on t ...

An unexpected special character (ï) was discovered at the beginning of the JSON parsing process in Java while utilizing the sample-json-1.1.1

I am encountering an issue while parsing a Har file generated from the ie11 browser using sample-json in Java. The error message I am receiving is "Unexpected character (ï) at position 0". Interestingly, the same code works perfectly fine with a Har file ...

What is the process for starting Chrome in landscape mode on a mobile emulator using C# Selenium?

Using this C# code, I successfully launched a web page with the Chrome browser in mobile portrait emulator mode: ChromeOptions options = new ChromeOptions(); options.AddArguments("disable-infobars"); options.AddArguments("start-maximized") ...

Empty Json Index

I have developed an Ionic application that communicates with my Laravel-based API by sending JSON data. Whenever a post request is made to the designated route, I invoke a method on my controller. This controller then takes the received data and stores i ...

How to send data from JavaScript to ASP.NET

$(document).ready(function () { $("#MainContent_ddlFieldName").on("change", function () { var id = $(this).val(); var name = $(this + "option:selected").text(); $('#<%= lblValue.ClientID %> ...

Using axios to render data in a view template

I am currently working on developing a basic Single Page Application (SPA) with the help of vue.js and axioz for scripts, without using CLI or any other tools. At this point, I have succeeded in fetching data from a JSON file, rendering and paginating the ...

Bring in a JavaScript file from Blogger without using a tag

Is there a way to retrieve blogger feeds code without using