Questions tagged [json.net]
Json.NET (also known as Newtonsoft.Json) is a popular high-performance JSON framework for .NET.
json.net
13,752
questions
0
votes
0
answers
9
views
How to read Obsolete JSON attributes but not write them?
I need to read older JSON with [Obsolete] properties, but when writing JSON only the [Required] properties should be written.
I still have a mix of older Newtonsoft.Json and newer Text.Json in .NET 8, ...
0
votes
0
answers
18
views
Ignore NULL values in JsonConvert.Deserialize or JToken.Parse
For example, I have following JSON: {"a":null} and I want to deserialize it to Newtonsoft's JToken.
I tried JToken jt = JToken.Parse(json) and JToken jt = JsonConvert.DeserializeObject<...
0
votes
0
answers
47
views
JsonConvert replace serialized non-object values with type names
I'm trying to use Newtonsoft's JsonConvert.SerializeObject, except I want to switch each value into its type name whilst still ensuring any objects or arrays are accounted for.
As an example I would ...
1
vote
2
answers
57
views
Adding a new JSON property to a class from a Nuget
I work with a class that comes from a Nuget package, and therefore I can't add properties to it.
Imagine this class:
public class Base
{
public Data Data { get; set; }
// Many other properties....
-2
votes
0
answers
32
views
How to serialize object to JSON format which inherits IList and has other properties [duplicate]
I built a IData object which has a IValues property where IValues inherits from IList<ValueModel> and has a Field property.
public interface IData
{
IValues Values { get; set; }
}
// I want ...
0
votes
1
answer
72
views
JsonConvert.DeserializeObject with different property types needs to continue even after failing but collect the list of errors
I need to deserialize a class with different types (int, float, string, guid) of properties. I am hoping to find a way to do this so that:
if a value does not match it's expected type an error would ...
1
vote
2
answers
40
views
How to get parent node id from child node id in JSON
Here is my JSON string:
[
{
"Order": 1,
"IssueId": 83719,
"Journal": "HLRF",
"Identity": "HLRF, Vol.137, Iss.4, y.2024, ...
0
votes
1
answer
43
views
Empty Object don't override in Merge using JSON.net
I have this code, for merge 2 json (complete and partial). There is a empty object.
string jsonOrigen = @"
{
'business': {
'tradingaccount': [
'60325f59-6bef-418a-69a1-...
0
votes
1
answer
51
views
Get ASP.NET Core 8 MVC controller to return JSON properties unchanged without lowercasing property names
I have an ASP.NET Core 8 controller.
When I return an object serialized using Newtonsoft.Json in the Ok return below, it changes the properties to lower case in the JSON.
I want them to stay as ...
0
votes
1
answer
54
views
Serializing generic class with Newtonsoft without encapsulating inner type
Suppose the following Generic class and inner type class:
public class Generic<T>
where T : class
{
public bool IsSuccess { get; set; }
public string Message { get; set; }
public ...
0
votes
0
answers
52
views
Generate Custom JSON Schema using Newtonsoft.Json.Schema.Generation
We are using Newtonsoft.Json.Schema.Generation (C#, dotNet 5.0) and I would like generate a custom JSON schema based on the properties and its attributes of a class which has category attribute set. ...
1
vote
1
answer
49
views
.NET JSON Property custom required logic
Suppose the following class Definition
public class Foo
{
[JsonProperty]
public int Number { get; set; }
[JsonProperty]
public string Text { get; set; }
}
I now need the property ...
0
votes
1
answer
30
views
.NET Framework Newtsonsoft json customize required exception message
Suppose I have the following class definition
public class Person
{
[JsonProperty(Required = Required.Always)]
public string Name { get; set; }
}
And when I try to instantiate a new Person ...
0
votes
0
answers
39
views
After upgrading from .NET 5 to .NET 7 and removing Newtonsoft.Json, DateTime values in API responses are different [duplicate]
I recently upgraded my project from .NET 5 to .NET 7 and removed Newtonsoft.Json by replacing it with System.Text.Json. After the upgrade, I noticed that DateTime values in the responses of my API ...
0
votes
0
answers
28
views
this[object key] operator in JToken behavior [closed]
I was chasing down a problem and found my answer but am wondering if this could be changed/improved. JObject implements IDictionary<string,JToken?> which contains an indexing operator this[...
0
votes
1
answer
60
views
Map JSON string array of headers and nested array of results to C# Generic
I am working with a service that returns the following JSON. While I can use Newtonsoft to deserialize into a C# Generic, the string array of headers, then nested array of results has thrown me a ...
0
votes
1
answer
85
views
How to deserialize json to C# class with arbitrary number of values and attributes
I'm trying to consume some JSON files, which could contains a arbitrary number of child values.
By simplifying the JSON, I get something like:
{
"timestamp":1710415627927,
"parent":...
1
vote
1
answer
40
views
FLURL 4.0 use Newtonsoft JSON Serializer on a single request
Referencing Flurl Serializers it shows that Flurl 4.0 can use the Newtonsoft based serializer, but it is not clear to me how to do this for a single request. I need this to address a circular loop ...
1
vote
1
answer
94
views
Generate JSON Schema for only required properties
We are using Newtonsoft JSON (C#, .Net Framework 4.5) and I would like generate a JSON schema based on a few properties of a class which has category attribute set. How do I achieve this?
Example:
...
0
votes
1
answer
71
views
ASP.NET Core Web API : can I do validation with ModelState.IsValid with a [FromBody] object
Was wondering if it is possible to use Model.IsValid with an ASP.NET Core Web API that takes in a JSON body as [FromBody] object from the controller argument.
Or should I just write my own validation ...
1
vote
1
answer
40
views
ASP.NET Core 3.1: Use Newtonsoft JSON only in some controllers
I have an ASP.NET Core 3.1 application where I use
services.AddControllers().AddNewtonsoftJson();
to use the Newtonsoft JSON serializer. Unfortunately I cannot switch completely to System.Text.Json ...
0
votes
0
answers
40
views
Serialization and validation of multiple C# attributes (built-in and Custom) in JSON schema
I required a help on JSON schema and JSON validation where We have a requirement to have multiple attributes (built-in and custom) for a property in C# and then serialize them into JSON schema.
I am ...
0
votes
1
answer
22
views
Serializing XML into POCO and then into JSON string
I have the following XML and I need to serialize it into a POCO and then deserialize it to JSON.
Here's the XML:
<PAC xmlns="https://mynamespace.com">
<Creds>
<AID>...
-1
votes
1
answer
85
views
How to group properties together when serializing to JSON with JsonConverter<T>
I will change my question all together leaving the initial question at the bottom.
I will try to explain what I'm trying to achieve without providing the underlying solution as I did before.
Time is ...
-1
votes
0
answers
19
views
Newtonsoft.dll load error only on PC, but not from USB during .exe run
Odd behavior has developed in a stable .exe [VB.net 4.6.1 framework]. This .exe hasn't been re-compiled in 3 years because it has performed its function without incident since then.
Only .dll ...
-1
votes
2
answers
64
views
Parse a JSON object from a string that contains more than just the JSON object
I receive a response from an API call and handle the response with three cases.
Failure
Failed because the actions were already performed
Success
I handle the case with "Failed due to previous ...
-2
votes
0
answers
40
views
Json Serialization of Task Objects in C#8.0 doesn't do anything - It neither throws exception nor code proceeds to next line of code
I am trying to serialize tasks object using newtonsoft json serializer, but it doesn't do anything. When I put the cancellation token, I get the exception that task was cancelled. Below is my simple ...
0
votes
1
answer
63
views
How to add different JSON objects to a single array?
I have a JSON object with the key name IssuesList and its value being a JSON array of objects. My problem is that the existing JSON uses two different types of objects in that array. Most instances of ...
0
votes
1
answer
34
views
How do I parse out a variable from my json string?
I'm trying to parse the variable "name" from the following json string:
{"data":{"items":[{"name":"30-2169b (2/2)"}]},"account_id":12748781}
...
-1
votes
2
answers
65
views
How to deserialize a section of the JSON message using C#?
My C# program will make a REST API call. The response message format is JSON (the converted soap XML message to JSON).
The nested JSON response message will be similar to the following:
{
"soap:...
0
votes
1
answer
61
views
Create a New Json File with Properties Pulled From Another File Using C#
What I'm trying to do is grab some Properties and sub-Properties from a .json file to create a new json file. Let's say I have a file....
{
"One": {
"SomeProp": ...
3
votes
1
answer
61
views
Deserialize JSON with nested lists from HttpResponseMessage
I've already tried some solutions described on Stackoverflow (e.g. Best way to deserialize a nested list in a json - C#).
I have already tried this, but it doesn't work for me. Maybe I have made a ...
1
vote
1
answer
30
views
Strange behavior of NewtonSoft json.Net with Powershell Core
Here below is a PS function supposed to convert PS object to NS json.Net object.
function ConvertTo-NSJson ( $psObject ) {
$json = $psObject | ConvertTo-Json -Compress -Depth 10
$nsJson = [...
0
votes
0
answers
24
views
error The specified deps.json [] does not exist
I am building an app in .net 8 and blazor, and when I want to do my first migration to the database I get the following error: The specified deps.json [D:\C# Projects in visual studio\APPVales\...
1
vote
0
answers
39
views
How do you handle nulls when serializing to json [duplicate]
The default UnixDateTimeConverter in Newtonsoft converts zero values to the Unix Epoch date whereas I am trying to convert it to null by creating a customer convertor.
This works when serializing but ...
0
votes
3
answers
75
views
Unable to deserialize the API response json in .NET Core C#
I have 3rd party web api that returns json response contains escape characters and many backslashes as below.
My aim to read all service group and its related services. I am unable to deseriable even ...
0
votes
1
answer
52
views
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path
I'm trying to parse a string to JSON, and it works if I parse it to JObject, but if I try to parse to my own custom type, I'm getting an error when it tries to parse the Properties property
error: ...
1
vote
1
answer
41
views
JSON return sometime normal string and sometime list of string [duplicate]
I have an external API that returns JSON objects as a response and while I am trying to deserialize it's throwing an exception.
public class JsonHoldStringOrListOfString
{
public static void Main()...
1
vote
0
answers
67
views
Why does Newtonsoft.Json cause a FileNotFoundException in mscorlib.dll?
Building a Windows console OpenAPI application and testing in VS IDE debug mode, the following code does not generate the following error, but attaching to the application process after starting the ...
0
votes
1
answer
69
views
How can I convert a JSON string to an <Anonymous Type>?
I want to convert a JSON string (built from a dynamic object to PSON via NewtonSoft back to a dynamic object.
The call:
dynamic? dataFromJson = JsonConvert.DeserializeObject<dynamic>(message....
0
votes
1
answer
102
views
Null-conditional operators?[] Throwing for data being null
Documentation here says
If a evaluates to null, the result of a?.x or a?[x] is null.
I want to understand what does evaluates mean here. The reason I am asking is that I am getting error for a case ...
0
votes
1
answer
78
views
Newtonsoft.Json - Numbers in Json file get incorrectly identified as being int when they're floats during serialisation
On a project, running on .NET 4.8 I'm executing a call to a web service, this call then returns a JSON, which needs to be processed.
I created the C# classes (long live json2csharp) and decided to ...
2
votes
1
answer
56
views
Possible mutation of static readonly record through Cosmos SDK and Newtonsoft
This is a very awkward error and it took me a while to tackle it down to these few lines of code. The full code can be grabbed as fiddle, but you have to provide your Cosmos Url and Key to let it run. ...
0
votes
0
answers
33
views
Log silence deserialization problem from Newtonsoft.Json or System.Text.Json?
In dealing with different versions of JSON data contracts, we sometimes encounter scenarios where the serialization library (e.g., Newtonsoft.Json or System.Text.Json) silently handles discrepancies, ...
0
votes
1
answer
87
views
Deserialize flat JSON to List<KeyValuePair<string, string>>
Edit: I realized my issue of not using a Dictionary<string, string> right after posting this question, so I immediately deleted it. Looks like I accidentally undeleted it again. Sorry about ...
1
vote
0
answers
41
views
C# - How to override null value in JsonConvert.SerializeObject output [duplicate]
I am trying to override the serialized value of a nullable object property
using a JsonConverterAttribute
pointing to a Custom JsonConverter
In the example shown below I am able to properly override ...
0
votes
0
answers
53
views
Attribute to indicate string should be serialized using JRaw
Is there an attribute to indicate a string property of an object should be serialized using JRaw in JSON.NET?
public class MyClass {
public string MyJsFunction {get;set}
}
MyJsFunction should be ...
0
votes
1
answer
79
views
REST API Response with property sometimes get a list or an object
I am calling the same REST API and from that API I am getting a response as a JSON object, and while trying to deserialize it, it throws an error because one of the objects sometimes returns as a list ...
1
vote
2
answers
81
views
c# Newtonsoft Deserialize Json into object depending on properties
Im in a situation where I am receiving Json that can be in two different formats and I am trying to find a good way to deserialize it into their respective objects. I have one Json string that would ...
1
vote
1
answer
71
views
Can I validate and deserialize in a single operation?
I am reading a string asynchronously from an HttpRequestData instance (Azure Function HttpTrigger). I then use JObject.Parse, then I execute the IsValid() method on the JObject, and then if it is ...