0

I am perplexed as why the DeserializeObject method cannot deserialize valid JSON:

string source = JsonConvert.SerializeObject(data.Source);
Maintenance ticket_data = JsonConvert.DeserializeObject<Maintenance>(source); //breaks here

When I hit my endpoint, I receive the following:

"ExceptionMessage": "Unexpected character encountered while parsing value: {. Path 'elements', line 21, position 5."

I see where that is happening. Elements has many different attributes, Elements is an array.

In the Maintenance class I have:

public IEnumerable<string> Elements { get; set; }

I used the JSONLint website to make sure source was valid JSON, and it is.

Maintenance Class

Some of the JSON output:

{
  "doc_type": "ticket",
  "updated_date": 12345,
  "ticket_number": "1234",
  "start": 1234,
  "summary": "hello",
  "description": "do stuff",
  "active": true,
  "related_tickets": [],
  "tags": [],
  "elements": [
    {
      "last_updated": 5678,
      "entry_id": null,
      "name": "something",

Any insight as to why I cannot deserialize this JSON would be greatly appreciated.

Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
Nubtacular
  • 1,367
  • 2
  • 18
  • 38
  • 2
    Can you post a sample JSON and the class definition just from this it is difficult to say something. – Aleš Doganoc Jun 05 '18 at 22:03
  • Updated original post. – Nubtacular Jun 05 '18 at 22:21
  • That is totally valid JSON but does not conform to the schema of the Maintenance class. The items in "elements" are not strings. They are some other complex object. Create another object to represent that thing and use it in definition of elements, `IEnumerable`. You could also use object if you didn't really care about getting at its properties. – Mike Zboray Jun 05 '18 at 22:28
  • change the type of Elements to IEnumerable> – Sir Rufo Jun 05 '18 at 22:33
  • Whenever you have valid JSON, DO NOT try to hand-craft the classes to deserialize into. Use http://json2csharp.com, it will produce the correct class definitions. – Craig W. Jun 05 '18 at 22:39
  • @mikez elements in the sql server database are in a varchar column like so: `element1;element2;element3`. When I'm creating my `elasticMaintenance` data I create Elements: `Elements = d.Element_Ids.Split(';'),` – Nubtacular Jun 05 '18 at 22:41
  • @DnfD What is the value for "elements" in the json you posted then? It's obviously not a string that was split by ';'. – Mike Zboray Jun 05 '18 at 22:45

3 Answers3

2

The error is due to de-serializing a list of objects: elements[{}] to a list of string elements: public IEnumerable<string> Elements

What you can do is create a wrapper Elements class with properties that match the JSON payload like this

 public class Elements {
  public DateTime last_updated {get;set;}
  public int? entry_id {get; set;}
  public string name {get; set;}
 } 

then in your Maintenance.cs you can change the Elements property to

public IEnumerable<Elements> Elements { get; set; }

Also take note of the data type in the JSON payload. Some are not strings. for example is last_updated

jmesolomon
  • 563
  • 5
  • 15
1

I think the problem is that your elements is not an array of stings like you defined in the object. From what it looks from the JSON snippet you have objects inside the elements array.

"elements": [{
      "last_updated": 5678,
      "entry_id": null,
      "name": "something",
}

This defines an object with properties last_updated, entry_id, name so this should map to a class or a Dictionary like somebody already suggested in the comments.

Else it should look something like this in JSON:

"elements": ["string1", "string2", "string3"]
Aleš Doganoc
  • 11,568
  • 24
  • 40
0

you must use List or IEnumerable of string insted Maintenance like this code:

var ticket_data = JsonConvert.DeserializeObject<List<string>>(source);

then set your element of Maintenance class like this code

    Elements =ticket_data ;
mehdi farhadi
  • 1,415
  • 1
  • 10
  • 16