Questions tagged [json-deserialization]

JSON deserialization is the process of converting a JSON string into an instance of an object, often a class.

JSON (Object Notation) is an efficient data encoding format that enables fast exchanges of small amounts of data between client browsers and -enabled Web services.

JSON deserialization is the process of converting a JSON string into an instance of an object, often a class. JSON encoded strings carry both structural and data information, so the instance of the object resulting from deserialization is a well-defined, data-filled object.

For example the following string:

string json_encoded_string = @"{""name"" : ""John"", ""surname"" : ""Doe"", ""age"" : 38}";

can be deserialized into an instance of the following class:

class Person
{
    public string name { get; set; }
    public string surname { get; set; }
    public int age { get; set; }
}
2322 questions
35
votes
1 answer

JSON.NET: How to deserialize interface property based on parent (holder) object value?

I have such classes class Holder { public int ObjType { get; set; } public List Objects { get; set; } } abstract class Base { // ... doesn't matter } class DerivedType1 : Base { // ... doesn't matter } class DerivedType2 :…
Zoka
  • 2,312
  • 4
  • 23
  • 33
32
votes
3 answers

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 },{ "prop1": "string", "prop2": "string", …
user3574076
  • 711
  • 1
  • 6
  • 23
31
votes
6 answers

Is it possible to change the output alias in pydantic?

Setup: # Pydantic Models class TMDB_Category(BaseModel): name: str = Field(alias="strCategory") description: str = Field(alias="strCategoryDescription") class TMDB_GetCategoriesResponse(BaseModel): categories:…
Rechu
  • 617
  • 1
  • 4
  • 14
29
votes
1 answer

The JSON value could not be converted to System.DateTime

I have an Employee table public class Employee { [Key] public long ID { get; set; } public DateTime EmpDate { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } I have created web API to post…
Adi
  • 405
  • 1
  • 7
  • 13
29
votes
4 answers

How to Deserialize JSON data?

I am new to working with JSON data. I am reading data from a web service. The query data sent back is the following: [["B02001_001E","NAME","state"], ["4712651","Alabama","01"], ["691189","Alaska","02"], ["6246816","Arizona","04"], …
eitan barazani
  • 1,123
  • 3
  • 18
  • 34
28
votes
4 answers

JSON: JsonMappingException while try to deserialize object with null values

I try to deserialize object that contains null-properties and have the JsonMappingException. What I do: String actual = "{\"@class\" : \"PersonResponse\"," + " \"id\" : \"PersonResponse\"," + " \"result\" : \"Ok\","…
VB_
  • 45,112
  • 42
  • 145
  • 293
27
votes
5 answers

How use jackson ObjectMapper inside custom deserializer?

I try to write custom jackson deserializer. I want "look" at one field and perform auto deserialization to class, see example below: import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import…
Cherry
  • 31,309
  • 66
  • 224
  • 364
26
votes
2 answers

CustomDeserializer has no default (no arg) constructor

I am consuming a REST Api with RestTemplate. The response I'm getting from the API has lots of nested objects. Here's a little snippet as an example: "formularios": [ { "form_data_id": "123006", "form_data": { "form_data_id":…
abril
  • 363
  • 1
  • 3
  • 4
25
votes
14 answers

System.Text.Json.JsonException: The input does not contain any JSON tokens

I'm just trying to use a Http POST method in a Blazor app through public async Task CreateUnit(UnitEntity unit) { await _http.PostJsonAsync("api/units", unit); } _http and myObject have been defined elsewhere, but I'm getting this…
Broad3857
  • 363
  • 1
  • 4
  • 10
24
votes
3 answers

.Net Core 3.0 TimeSpan deserialization error - Fixed in .Net 5.0

I am using .Net Core 3.0 and have the following string which I need to deserialize with Newtonsoft.Json: { "userId": null, "accessToken": null, "refreshToken": null, "sessionId": null, "cookieExpireTimeSpan": { "ticks":…
Kok How Teh
  • 3,298
  • 6
  • 47
  • 85
22
votes
1 answer

Rust & Serde JSON deserialization examples?

I'm trying to figure out how to deserialize JSON into a structure using Serde. For instance, the example JSON on serde_json's own documentation contains the following data: { "FirstName": "John", "LastName": "Doe", "Age": 43, …
vegai
  • 330
  • 1
  • 3
  • 6
20
votes
4 answers

Decode JSON string in Java with json-simple library

I am new to using the json-simple library in Java and I've been through both the encoding and decoding samples. Duplicating the encoding examples was fine, but I have not been able to get the decoding ones to work with mixed type JSON. One of my…
Sharda Singh
  • 727
  • 3
  • 10
  • 19
18
votes
1 answer

Modify existing object with new partial JSON data using Json.NET

Consider the below example program var calendar = new Calendar { Id = 42, CoffeeProvider = "Espresso2000", Meetings = new[] { new Meeting { Location = "Room1", From =…
Linus
  • 3,254
  • 4
  • 22
  • 36
17
votes
7 answers

Can Swift 4's JSONDecoder be used with Firebase Realtime Database?

I am trying to decode data from a Firebase DataSnapshot so that it can be decoded using JSONDecoder. I can decode this data fine when I use a URL to access it with a network request (obtaining a Data object). However, I want to use the Firebase API…
17
votes
3 answers

Newtonsoft JSON - How to use the JsonConverter.ReadJson method to convert types when deserializing JSON

I need help understanding how to use the the JsonConverter.ReadJson method to convert a value of any number of types (string, boolean, Date, int, array, object) to a specific custom type. For example, I have the following; public override object…
ClaraU
  • 877
  • 3
  • 11
  • 22