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
859 votes
22 answers
1.0m views

How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?

I have a simple key/value list in JSON being sent back to ASP.NET via POST. Example: { "key1": "value1", "key2": "value2"} I AM NOT TRYING TO DESERIALIZE INTO STRONGLY-TYPED .NET OBJECTS I simply ...
richardtallent's user avatar
723 votes
17 answers
641k views

How to ignore a property in class if null, using json.net

I am using Json.NET to serialize a class to JSON. I have the class like this: class Test1 { [JsonProperty("id")] public string ID { get; set; } [JsonProperty("label")] public string ...
Amit's user avatar
  • 25.7k
621 votes
27 answers
519k views

JSON.NET Error Self referencing loop detected for type

I tried to serialize POCO class that was automatically generated from Entity Data Model .edmx and when I used JsonConvert.SerializeObject I got the following error: Error Self referencing loop ...
NevenHuynh's user avatar
  • 6,679
530 votes
4 answers
440k views

How can I change property names when serializing with Json.net?

I have some data in a C# DataSet object. I can serialize it right now using a Json.net converter like this DataSet data = new DataSet(); // do some work here to populate 'data' string output = ...
culix's user avatar
  • 10.4k
504 votes
9 answers
497k views

Deserialize JSON object into dynamic object using Json.net

Is it possible to return a dynamic object from a json deserialization using json.net? I would like to do something like this: dynamic jsonResponse = JsonConvert.Deserialize(json); Console.WriteLine(...
ryudice's user avatar
  • 36.9k
369 votes
6 answers
353k views

.NET NewtonSoft JSON deserialize map to a different property name

I have following JSON string which is received from an external party. { "team":[ { "v1":"", "attributes":{ "eighty_min_score":"", "home_or_away":"...
RasikaSam's user avatar
  • 5,473
366 votes
3 answers
306k views

How to deserialize a JObject to .NET object

I happily use the Newtonsoft JSON library. For example, I would create a JObject from a .NET object, in this case an instance of Exception (might or might not be a subclass) if (result is Exception) ...
Sebastian's user avatar
  • 6,383
343 votes
12 answers
748k views

Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)

I know there are a few posts about Newtonsoft so hopefully this isn't exactly a repeat...I'm trying to convert JSON data returned by Kazaa's API into a nice object of some kind WebClient client = new ...
J Benjamin's user avatar
  • 4,752
340 votes
9 answers
382k views

How to implement custom JsonConverter in JSON.NET?

I am trying to extend the JSON.net example given here http://james.newtonking.com/projects/json/help/CustomCreationConverter.html I have another sub class deriving from base class/Interface public ...
Snakebyte's user avatar
  • 3,775
333 votes
12 answers
667k views

How to convert JSON to XML or XML to JSON in C#?

I started to use Json.NET to convert a string in JSON format to object or viceversa. I am not sure in the Json.NET framework, is it possible to convert a string in JSON to XML format and viceversa?
David.Chu.ca's user avatar
327 votes
6 answers
362k views

Convert Newtonsoft.Json.Linq.JArray to a list of specific object type

I have the following variable of type {Newtonsoft.Json.Linq.JArray}. properties["Value"] {[ { "Name": "Username", "Selected": true }, { "Name": "Password", "Selected": true } ...
Mdb's user avatar
  • 8,478
324 votes
14 answers
227k views

How can I return camelCase JSON serialized by JSON.NET from ASP.NET MVC controller methods?

My problem is that I wish to return camelCased (as opposed to the standard PascalCase) JSON data via ActionResults from ASP.NET MVC controller methods, serialized by JSON.NET. As an example consider ...
aknuds1's user avatar
  • 66.8k
267 votes
46 answers
490k views

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

I am getting the Error System.IO.FileLoadException : Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its ...
user3437755's user avatar
  • 2,671
267 votes
37 answers
535k views

Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. Manifest definition does not match the assembly reference

Things I've tried after searching: in Web.Config put a binding on the old version: <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture=...
noobieDev's user avatar
  • 3,123
219 votes
13 answers
307k views

How to make sure that string is valid JSON using JSON.NET

How can one validate whether a raw string is valid JSON or just text? I'm using JSON.NET.
Imran Qadir Baksh - Baloch's user avatar
215 votes
5 answers
580k views

How to write a JSON file in C#?

I need to write the following data into a text file using JSON format in C#. The brackets are important for it to be valid JSON format. [ { "Id": 1, "SSN": 123, "Message": "whatever" ...
user1429595's user avatar
  • 2,685
214 votes
13 answers
233k views

JSON.Net Self referencing loop detected

I have a mssql database for my website within 4 tables. When I use this: public static string GetAllEventsForJSON() { using (CyberDBDataContext db = new CyberDBDataContext()) { ...
PassionateDeveloper's user avatar
209 votes
7 answers
195k views

Parse JSON in C#

I'm trying to parse some JSON data from the Google AJAX Search API. I have this URL and I'd like to break it down so that the results are displayed. I've currently written this code, but I'm pretty ...
Mike B's user avatar
  • 12.8k
208 votes
8 answers
264k views

Specifying a custom DateTime format when serializing with Json.Net

I am developing an API to expose some data using ASP.NET Web API. In one of the API, the client wants us to expose the date in yyyy-MM-dd format. I don't want to change the global settings (e.g. ...
Stay Foolish's user avatar
  • 3,686
199 votes
7 answers
153k views

Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?

I've just upgraded my ASP web API project from .NET Core 2.0 to 3.0. I was using services.AddMvc() .AddJsonOptions(opts => opts.SerializerSettings.ContractResolver = new ...
NeilMacMullen's user avatar
199 votes
6 answers
185k views

JSON.net: how to deserialize without using the default constructor?

I have a class that has a default constructor and also an overloaded constructor that takes in a set of parameters. These parameters match to fields on the object and are assigned on construction. ...
kmacdonald's user avatar
  • 3,421
190 votes
7 answers
269k views

Can Json.NET serialize / deserialize to / from a stream?

I have heard that Json.NET is faster than DataContractJsonSerializer, and wanted to give it a try... But I couldn't find any methods on JsonConvert that take a stream rather than a string. For ...
Omri Gazitt's user avatar
  • 3,468
189 votes
17 answers
209k views

Casting interfaces for deserialization in JSON.NET

I am trying to set up a reader that will take in JSON objects from various websites (think information scraping) and translate them into C# objects. I am currently using JSON.NET for the ...
tmesser's user avatar
  • 7,606
184 votes
13 answers
123k views

Making a property deserialize but not serialize with json.net

We have some configuration files which were generated by serializing C# objects with Json.net. We'd like to migrate one property of the serialised class away from being a simple enum property into a ...
Will Dean's user avatar
  • 39.3k
177 votes
22 answers
912k views

Unexpected character encountered while parsing value

Currently, I have some issues. I'm using C# with Json.NET. The issue is that I always get: {"Unexpected character encountered while parsing value: e. Path '', line 0, position 0."} So the ...
zAfLu's user avatar
  • 1,828
176 votes
16 answers
115k views

Order of serialized fields using JSON.NET

Is there a way to specify the order of fields in a serialized JSON object using JSON.NET? It would be sufficient to specify that a single field always appear first.
Kevin Montrose's user avatar
173 votes
5 answers
397k 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": [ ...
Avinash's user avatar
  • 2,143
165 votes
5 answers
116k views

What exceptions does Newtonsoft.Json.DeserializeObject throw?

What exceptions does Newtonsoft.Json.DeserializeObject throw? I want to handle them. http://james.newtonking.com/json/help/?topic=html/M_Newtonsoft_Json_JsonConvert_DeserializeObject.htm#...
cja's user avatar
  • 9,768
158 votes
4 answers
158k views

Specify a value can be a string or null with JSON Schema

Hopefully this isn't obvious to others because I find the docs at https://json-schema.org/ to be lacking in finer details. I'm getting a block of JSON with some properties that can be null or a string....
evanmcdonnal's user avatar
  • 47.1k
157 votes
4 answers
144k views

json.net has key method?

If my response has key "error" I need to process error and show warning box. Is there "haskey" method exists in json.net? Like: var x= JObject.Parse(string_my); if(x.HasKey["error_msg"]) ...
SevenDays's user avatar
  • 3,728
149 votes
9 answers
62k views

How to handle both a single item and an array for the same property using JSON.net

I'm trying to fix my SendGridPlus library to deal with SendGrid events, but I'm having some trouble with the inconsistent treatment of categories in the API. In the following example payload taken ...
Robert McLaws's user avatar
149 votes
7 answers
304k views

Deserializing JSON Object Array with Json.net

I am attempt to use an API that use the follow example structure for their returned json [ { "customer":{ "first_name":"Test", "last_name":"Account", "email":"...
Shawn C.'s user avatar
  • 6,651
148 votes
6 answers
215k views

Get value from JToken that may not exist (best practices)

What's the best practice for retrieving JSON values that may not even exist in C# using Json.NET? Right now I'm dealing with a JSON provider that returns JSON that sometimes contains certain key/...
Paul Hazen's user avatar
  • 2,401
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 ...
user avatar
146 votes
4 answers
186k views

How do I enumerate through a JObject?

I'm trying to determine how to access the data that is in my JObject and I can't for the life of me determine how to use it. JObject Object = (JObject)Response.Data["my_key"]; I can print it to the ...
Geesu's user avatar
  • 5,938
143 votes
3 answers
189k views

How to serialize a JObject without the formatting?

I have a JObject (I'm using Json.Net) that I constructed with LINQ to JSON (also provided by the same library). When I call the ToString() method on the JObject, it outputs the results as formatted ...
Hugo's user avatar
  • 6,334
143 votes
6 answers
156k views

Json.net serialize/deserialize derived types?

json.net (newtonsoft) I am looking through the documentation but I can't find anything on this or the best way to do it. public class Base { public string Name; } public class Derived : Base { ...
Will's user avatar
  • 10.2k
140 votes
8 answers
218k views

Creating JSON on the fly with JObject

For some of my unit tests I want the ability to build up particular JSON values (record albums in this case) that can be used as input for the system under test. I have the following code: var ...
Karl Anderson's user avatar
135 votes
13 answers
155k views

How to store JSON in an entity field with EF Core?

I am creating a reusable library using .NET Core (targeting .NETStandard 1.4) and I am using Entity Framework Core (and new to both). I have an entity class that looks like: public class Campaign { ...
Alex's user avatar
  • 2,885
131 votes
9 answers
337k views

How to import JsonConvert in C# application?

I created a C# library project. The project has this line in one class: JsonConvert.SerializeObject(objectList); I'm getting error saying the name JsonConvert doesn't exist in the current ...
Jainendra's user avatar
  • 24.9k
127 votes
5 answers
346k views

Send JSON via POST in C# and Receive the JSON returned?

This is my first time ever using JSON as well as System.Net and the WebRequest in any of my applications. My application is supposed to send a JSON payload, similar to the one below to an ...
Hunter Mitchell's user avatar
125 votes
6 answers
207k views

Checking for empty or null JToken in a JObject

I have the following... JArray clients = (JArray)clientsParsed["objects"]; foreach (JObject item in clients.Children()) { // etc.. SQL params stuff... command.Parameters["@MyParameter"]....
Kyle's user avatar
  • 5,467
123 votes
4 answers
177k views

Convert object of any type to JObject with Json.NET

I often need to extend my Domain model with additional info before returning it to the client with WebAPI. To avoid creation of ViewModel I thought I could return JObject with additional properties. I ...
dragonfly's user avatar
  • 17.6k
118 votes
5 answers
137k views

Convert JObject into Dictionary<string, object>. Is it possible?

I have a web API method that accepts an arbitrary json payload into a JObject property. As such I don't know what's coming but I still need to translate it to .NET types. I would like to have a ...
tucaz's user avatar
  • 6,624
116 votes
9 answers
105k views

Web API 2: how to return JSON with camelCased property names, on objects and their sub-objects

UPDATE Thanks for all the answers. I am on a new project and it looks like I've finally got to the bottom of this: It looks like the following code was in fact to blame: public static ...
Tom's user avatar
  • 8,062
116 votes
6 answers
72k views

Deserializing polymorphic json classes without type information using json.net

This Imgur api call returns a list containing both Gallery Image and Gallery Album classes represented in JSON. I can't see how to deserialize these automatically using Json.NET given that there is ...
Peter Kneale's user avatar
  • 1,926
114 votes
5 answers
67k views

Private setters in Json.Net

I know there's an attribute to handle private setters but I kind of want this behavior as a default, is there a way to accomplish this? Except tweaking the source. Would be great if there was a ...
Daniel's user avatar
  • 8,223
113 votes
6 answers
80k views

Ensuring json keys are lowercase in .NET

Is there simple way using JSON in .NET to ensure that the keys are sent as lower case? At the moment I'm using the newtonsoft's Json.NET library and simply using string loginRequest = JsonConvert....
Mark's user avatar
  • 2,454
112 votes
2 answers
142k views

Newtonsoft JsonSerializer - Lower case properties and dictionary [duplicate]

I'm using json.net (Newtonsoft's JsonSerializer). I need to customize serialization in order to meet following requirements: property names must start with lower case letter. Dictionary<string, T&...
Liero's user avatar
  • 26.2k
112 votes
5 answers
165k views

Parsing JSON using Json.net

I'm trying to parse some JSON using the JSon.Net library. The documentation seems a little sparse and I'm confused as to how to accomplish what I need. Here is the format for the JSON I need to ...
Carter's user avatar
  • 2,850

1
2 3 4 5
280