Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize a dynamic object to a JSON string in dotnet core?

I am passing a JSON payload to an API Controller, and one of the fields is dynamic because the field needs to be passed again as a JSON string to another API. The dotnet core 3.1 middle layer shouldn't be concerned with typing, as the payload will change.

This is the object that is passed into the API Controller:

    public class GitHubAction
    {
        [JsonProperty("Title")]
        public string Title { get; set; }

        [JsonProperty("Enabled")]
        [JsonConverter(typeof(BooleanParseStringConverter))]
        public bool Enabled { get; set; }

        [JsonProperty("Action")]
        [JsonConverter(typeof(ExpandoObjectConverter))]
        public dynamic Action { get; set; }
    }

Here is a picture of that dynamic object looks like in VSCode:

enter image description here

When I use JsonConvert.SerializeObject(x.Action); the string result isn't being properly converted, but instead serializes to ValueKind: "{\"ValueKind\":1}".

What I want to get is the action object value as a JSON string, which should look like "{"createRepository":{"onboarding":{"service":{"organization":"foo","repositoryName":"foo-2-service","description":"A test service."}}}}"

Is there a simple solution for serializing a dynamic object?

like image 683
Todd Worden Avatar asked Feb 29 '20 12:02

Todd Worden


People also ask

How do you serialize an object in .NET core?

NET objects as JSON (serialize) To write JSON to a string or to a file, call the JsonSerializer. Serialize method. The JSON output is minified (whitespace, indentation, and new-line characters are removed) by default.

How do you serialize and deserialize a string in C#?

We can implement JSON Serialization/Deserialization in the following three ways: Using JavaScriptSerializer class. Using DataContractJsonSerializer class. Using JSON.NET library.

How do you serialize and deserialize an object in C# using JSON?

Serializing is as simple as Deserializing. string json = JsonConvert. SerializeObject(model); File. WriteAllText("C:\json.

How can I convert JSON to string?

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

What is JsonConvert DeserializeObject C#?

DeserializeObject<T>(String,JsonConverter[]) Deserializes the JSON to the specified . NET type using a collection of JsonConverter. DeserializeObject(String, JsonSerializerSettings)


3 Answers

I had the same problem and I fixed it this way

using System.Text.Json;

string serializedObject = JsonSerializer.Serialize(x.Action); //Instead of use JsonConvert.SerializeObject(x.Action);

Instead of Newtonsoft.Json you have to use System.Text.Json.

like image 71
JPocoata Avatar answered Sep 17 '22 00:09

JPocoata


Try this

var jsonString=s.Action.ToString();

This will give you the json string

like image 34
Kiran B Avatar answered Sep 20 '22 00:09

Kiran B


Use Newtonsoft.Json instead of default System.Text.Json

Add Microsoft.AspNetCore.Mvc.NewtonsoftJson package from nuget and services.AddControllers().AddNewtonsoftJson() in Startup.cs/ConfigureServices()

like image 21
Evgeniy Gubin Avatar answered Sep 18 '22 00:09

Evgeniy Gubin