Questions tagged [json.net]
Json.NET (also known as Newtonsoft.Json) is a popular high-performance JSON framework for .NET.
json.net
13,962
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, ...
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....
46
votes
5
answers
13k
views
Is the Json.NET JsonSerializer threadsafe?
I'm trying to reduce the amount of garbage my web service generates, and I noticed that we're creating a new Json.NET JsonSerializer instance for each request. It is not the most lightweight object ...
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<...
6
votes
1
answer
10k
views
JsonConvert.SerializeObject adds \r\n
so currently I am working on a WebAPI and I run in the following error.
While I try to return a List serialized with JsonConvert.SerializeObject(object) my second attribute (string JSON) get covered ...
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 ...
-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 ...
7
votes
4
answers
4k
views
JSON deserialization - Map array indices to properties with JSON.NET
I want to deserialize a 2-dimensional array to a collection of .net objects. The reason is, array syntax will be easier for my user to work with in an input file. So I just want to map the indices of ...
0
votes
2
answers
759
views
Cannot correctly parse my object from JSONreturned from my C# Web API
I have searched until my eyes are bleeding but alas no answers.
I have a class called Booking.
public class Booking
{
[Key]
public int Id { get; set; }
[Required]
...
52
votes
4
answers
64k
views
How to improve JSON deserialization speed in .Net? (JSON.net or other?)
We're considering replacing (some or many) 'classic' SOAP XML WCF calls by JSON (WCF or other) calls, because of the lower overhead and ease of use directly in Javascript. For now, we've just added an ...
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-...
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, ...
21
votes
5
answers
41k
views
Serializing strings containing apostrophes with JSON.Net
I am using JSON.Net as my serializer for a large MVC 3 web application in c# and the Razor view engine. For the initial page load in one view, there is a large amount of JSON dumped inside a script ...
5
votes
4
answers
18k
views
How to get first child of JObject without using foreach loop
I need to get the first child of JObject.
This is how I temporarily solved it with foreach loop breaking after first iteration.
foreach (KeyValuePair<string, JToken> item in (JObject)json["...
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
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>...
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 ...
173
votes
5
answers
398k
views
Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly
I have this JSON:
[
{
"Attributes": [
{
"Key": "Name",
"Value": {
"Value": "Acc 1",
"Values": [
...
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. ...
48
votes
8
answers
131k
views
Deserializing JSON array into strongly typed .NET object
When I can call the 3rd party api and get back a single class worth of data everything deserialises fine using this code
TheUser me = jsonSerializer.Deserialize(response, typeof(TheUser)) as TheUser
...
2
votes
3
answers
639
views
Deserialize Json to List C#
I'm having some issues on deserializing a Json that the api returns.
The Json is the following:
{ "Result":[
{
"Id": 1,
"Type": "Type1"
...
-1
votes
2
answers
734
views
using newtonsoft json deserialize object that contains array/list using dictionary
I want to convert JSON to object. The code below works fine.
internal class Program
{
static void Main(string[] args)
{
string json = @"{
'name': 'Ayrton ...
9
votes
1
answer
35k
views
Using [JsonProperty] to match JSON parameters to C# class properties which are type List
I'm trying to deserialize the following JSON response (using Json.NET):
[
{
"0": "Yes",
"1": "No",
"pollid": "1",
"...
104
votes
3
answers
83k
views
JContainer, JObject, JToken and Linq confusion
I am having trouble understanding when to use JContainer, JObject, and JToken. I understand from the "standards" that JObject is composed of JProperties and that JToken is the base abstract class for ...
44
votes
4
answers
131k
views
Deserialize JSON to nested C# Classes
Below is a (slightly) stripped down response I get from a REST API upon successful creation of a new "job code" entry. I need to deserialize the response into some classes, but I'm stumped.
...
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 ...
147
votes
5
answers
380k
views
Deserializing JSON data to C# using JSON.NET
I'm relatively new to working with C# and JSON data and am seeking guidance. I'm using C# 3.0, with .NET3.5SP1, and JSON.NET 3.5r6.
I have a defined C# class that I need to populate from a JSON ...
4
votes
2
answers
4k
views
Newtonsoft JSON - Way of ignoring properties without adding [JsonIgnore]
This may seem silly, but It was the only way I can phrase it.
What I am trying to do is serialise an object and its data. The object is inheriting a large BaseObject class. I don't wan't any of the ...
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 ...
55
votes
7
answers
57k
views
System.Text.Json: Deserialize JSON with automatic casting
Using .Net Core 3's new System.Text.Json JsonSerializer, how do you automatically cast types (e.g. int to string and string to int)? For example, this throws an exception because id in JSON is numeric ...
10
votes
2
answers
22k
views
How can I return key-value pairs from a function in C#?
I am implementing a C# function.
I need to get the values from the function to the form.
public void GetRoles(string strGetRoles)
{
string keyObj;
string valueObj;
var parseTree = ...
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[...
34
votes
3
answers
117k
views
Deserialize nested JSON into C# objects
I am getting JSON back from an API that looks like this:
{
"Items": {
"Item322A": [{
"prop1": "string",
"prop2": "string",
"prop3": 1,
"prop4": false
},{
"...
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
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 ...
1
vote
1
answer
847
views
C# Json Deserialization into string throws exception: "Unexpected character encountered while parsing value: {. Path '', line 1, position 1."}
I know there are a lot of questions out there about this topic but I can't seem to find one that fits what I'm seeing. I've tried cleaning the string, swapping double quotes with single quotes, ...
9
votes
4
answers
9k
views
JSON.NET Root Tag and Deserialization
I have the following JSON returning from a Java service
{"Test":{
"value": 1,
"message": "This is a test"
}}
I have the following C# class
class ...
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":...
2
votes
2
answers
5k
views
Iterate through nested JSON object array and access inner token value
I am parsing a JSON object from a JSON file, a sample of the structure of which is shown below:
{
"Context": [
{
"name": "Cities",
"cities&...
7
votes
3
answers
6k
views
How to ignore null values in a JSON dictionary with JSON.NET
When serializing a dictionary with JSON.NET, it seems like the NullValueHandling setting is ignored.
var dict = new Dictionary<string, string>
{
["A"] = "Some text",
...
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:
...
-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 ...
0
votes
2
answers
1k
views
How to add a (possibly empty) JSON property, based on a condition?
Yesterday, I needed to add a property in a JSON file, I've done this using DefaultHandling, as mentioned in this previous question:
[JsonProperty("DIAGNOSTICS", DefaultValueHandling = ...
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 ...
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
2
answers
771
views
Error in Visual Studio 2022 debugging data table visualizer - missing NewtonsoftSerializationHelper.dll or dependencies
I have upgraded Visual studio 2022 to the latest updated release 17.7.4 from the previous build. From then, I'm getting an error "Internal error in Visualizer" when trying to visualize a C# ...
0
votes
3
answers
2k
views
SignalR JSON Self Referencing Loop Detected
I have a .NET class that I am passing from web server to browser client via standard SignalR (C#, ASP.NET, Visual Studio 2013 Community).
This class contains a property that is itself a reference to ...