Questions tagged [json.net]

Json.NET (also known as Newtonsoft.Json) is a popular high-performance JSON framework for .NET.

json.net
Filter by
Sorted by
Tagged with
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, ...
PieterV's user avatar
  • 573
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<...
Denis's user avatar
  • 3,705
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 ...
James Coyle's user avatar
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....
Gil's user avatar
  • 153
-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 ...
Ludovic Wagner's user avatar
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 ...
Emilija Vilija Trečiokaitė's user avatar
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, ...
Sarper Sözen's user avatar
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-...
Kiquenet's user avatar
  • 14.7k
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 ...
C D's user avatar
  • 11
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 ...
MPelletier's user avatar
  • 16.5k
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. ...
Developer 's user avatar
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 ...
Wouter Vandenputte's user avatar
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 ...
Wouter Vandenputte's user avatar
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 ...
mth12's user avatar
  • 15
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[...
Glenn Poorman's user avatar
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 ...
jmlumpkin's user avatar
  • 930
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":...
Cascador84's user avatar
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 ...
azpc's user avatar
  • 550
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: ...
Developer 's user avatar
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 ...
CuriousGeo66's user avatar
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 ...
user2033412's user avatar
  • 2,009
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 ...
Developer 's user avatar
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>...
MB34's user avatar
  • 4,288
-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 ...
El Barto's user avatar
-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 ...
user3208998's user avatar
-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 ...
Micah Stovall's user avatar
-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 ...
kashif Rahman's user avatar
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 ...
Nosnetrom's user avatar
  • 161
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} ...
surfdmountain's user avatar
-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:...
Ullan's user avatar
  • 957
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": ...
Superfreak3's user avatar
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 ...
Lennart's user avatar
  • 101
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 = [...
Alain's user avatar
  • 65
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\...
Leandro Fontana's user avatar
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 ...
sgmoore's user avatar
  • 15.9k
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 ...
PavanKumar GVVS's user avatar
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: ...
buga's user avatar
  • 1,022
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()...
Shibu's user avatar
  • 110
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 ...
Marc George's user avatar
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....
David Thielen's user avatar
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 ...
pr0mpT_07's user avatar
  • 184
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 ...
Fysicus's user avatar
  • 333
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. ...
Oliver's user avatar
  • 44.1k
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, ...
Banshee's user avatar
  • 15.6k
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 ...
lenny's user avatar
  • 774
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 ...
2ynn's user avatar
  • 11
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 ...
Alexander Zeitler's user avatar
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 ...
Shibu's user avatar
  • 110
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 ...
rjs52's user avatar
  • 15
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 ...
Peter Morris's user avatar
  • 21.9k

1
2 3 4 5
276