# Elasticsearch Nest - JsonParsingException expected:'{',

**URL:** <https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801>\
**Category:** Elasticsearch\
**Created:** [August 19, 2019, 7:06pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801 "2019-08-19T19:06:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Airn5475](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/airn5475/32/13757_2.png) [@Airn5475](https://discuss.elastic.co/u/Airn5475)\
**Post date:** [August 19, 2019, 7:06pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/1 "2019-08-19T19:06:32Z")

</div>

I am in the process of updating my application to use Elasticsearch 7.3.  
I updated my mappings

```
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

```

Here's part of a query result:

```
"_source": {
  "location": {
    "lon": -81.9438063,
    "lat": 40.8507256
  }
}

```

I'm using Elasticsearch NEST 7.3 to try and cast a `IHit<ILazyDocument>` to my model

`hit.Source.As(typeof(MyModel))`

Here are my models:

```
public class MyModel
{
    public GeoCoordinate Location { get; set; }
}

public class GeoCoordinate
{
    [Text(Name = "lat")]
    public Latitude Latitude { get; set; }

    [Text(Name = "lon")]
    public Longitude Longitude { get; set; }
}

```

Here's the error message I'm receiving:

> expected:'{', actual:'-81.9438063', at offset:965

Partial Stack Trace (if helpful):

> at Elasticsearch.Net.Utf8Json.JsonReader.ReadIsBeginObjectWithVerify()  
> at Deserialize(Object , JsonReader& , IJsonFormatterResolver )  
> at Deserialize(Object , JsonReader& , IJsonFormatterResolver )  
> at Deserialize(Object , JsonReader& , IJsonFormatterResolver )  
> at Elasticsearch.Net.Utf8Json.JsonSerializer.Deserialize[T](Byte bytes, Int32 offset, IJsonFormatterResolver resolver)  
> at Elasticsearch.Net.Utf8Json.JsonSerializer.Deserialize[T](Stream stream, IJsonFormatterResolver resolver)  
> at Deserialize(Stream , IJsonFormatterResolver )  
> at Elasticsearch.Net.DiagnosticsSerializerProxy.Deserialize(Type type, Stream stream)  
> at Nest.LazyDocument.As(Type objectType)

Is my GeoCoordinate class not properly deserializing?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [August 20, 2019, 3:48am UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/2 "2019-08-20T03:48:07Z")

</div>

Would you be able to add the client API call that you're making, too?

---

<div class="post-metadata">

**Author:** ![Airn5475](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/airn5475/32/13757_2.png) [@Airn5475](https://discuss.elastic.co/u/Airn5475)\
**Post date:** [August 20, 2019, 1:12pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/3 "2019-08-20T13:12:23Z")

</div>

I'm searching two separate indexes.

`POST /companies,contacts/_search?typed_keys=true&search_type=dfs_query_then_fetch`

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [August 20, 2019, 8:59pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/4 "2019-08-20T20:59:25Z")

</div>

Can you show the .NET client API call that you're making? You can also get the request and response bytes in the `response.DebugInformation` by setting [`.DisableDirectStreaming()`](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/logging-with-on-request-completed.html). This will allow us to see what the JSON source document looks like

---

<div class="post-metadata">

**Author:** ![Airn5475](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/airn5475/32/13757_2.png) [@Airn5475](https://discuss.elastic.co/u/Airn5475)\
**Post date:** [August 20, 2019, 9:16pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/5 "2019-08-20T21:16:15Z")

</div>

Here ya go, had to trim down the number of results returned due to a character limit:

Valid NEST response built from a successful (200) low level call on POST: /companies%2Ccontacts/\_search?typed\_keys=true&search\_type=dfs\_query\_then\_fetch

# Audit trail of this API call:

- [1] HealthyResponse: Node: [http://localhost:9200/](http://localhost:9200/) Took: 00:00:00.0249636

# Request:

{"from":0,"query":{"bool":{"must":[{"bool":{"minimum\_should\_match":1,"should":[{"match":{"dba\_name.edge\_ngram":{"operator":"and","query":"cert ang+","\_name":"Company.DbaName.EdgeNgram.Match"}}},{"match":{"dba\_name":{"operator":"and","query":"cert ang+","\_name":"Company.DbaName.Match"}}},{"match\_phrase\_prefix":{"dba\_name.edge\_ngram":{"query":"cert ang+","slop":100,"\_name":"Company.DbaName.EdgeNgram.MatchPhrase"}}},{"match":{"first\_last\_name.edge\_ngram":{"operator":"and","query":"cert ang+","\_name":"Contact.FirstLastName.EdgeNgram.Match^0.0"}}},{"match":{"preferred\_last\_name.edge\_ngram":{"operator":"and","query":"cert ang+","\_name":"Contact.PreferredLastName.EdgeNgram.Match^0.0"}}}]}}],"should":[{"nested":{"ignore\_unmapped":true,"path":"children","query":{"match":{"children.dba\_name.edge\_ngram":{"operator":"and","query":"cert ang+","\_name":"Company.Children.DbaName.EdgeNgram.Match"}}},"score\_mode":"max","boost":2.0}},{"terms":{"\_name":"Contact.JobFunctions.Term^8.0","boost":8.0,"job\_functions":[1863,1864,1872,1873]}}]}},"sort":[{"is\_currently\_licensed":{"unmapped\_type":"boolean","missing":0,"order":"desc"}},{"\_score":{"order":"desc"}},{"first\_last\_name.keyword":{"unmapped\_type":"keyword","missing":"","order":"asc"}},{"is\_previously\_licensed":{"unmapped\_type":"boolean","missing":0,"order":"desc"}},{"last\_activity\_date":{"order":"desc"}}],"\_source":{"excludes":["children"],"includes":["\*"]},"track\_scores":true}

# Response:

{"took":15,"timed\_out":false,"\_shards":{"total":2,"successful":2,"skipped":0,"failed":0},"hits":{"total":{"value":20,"relation":"eq"},"max\_score":933.4909,"hits":[{"\_index":"companies\_v3","\_type":"\_doc","\_id":"6404","\_score":933.4909,"\_source":{"parent":{"dba\_name":"Certified Angus Beef LLC - Wooster","key":6404},"agent":{"hostname":"BAUMANXPS15","id":"e3c44bd2-4f90-46af-a626-6c1cd7755f1e","ephemeral\_id":"ebbce2ae-a5dc-444d-b2f3-d6f74bc60f87","type":"filebeat","version":"7.3.0"},"log":{"file":{"path":"C:\Elasticsearch\Artifacts\input\filebeat\_companies.2019\_08\_15.json"},"offset":3904432},"city":"Wooster","geo\_accuracy":8,"state\_abbreviation":"OH","star\_ranking":0,"country\_abbreviation":"USA","ecs":{"version":"1.0.1"},"@version":"1","primary\_license\_description":"Corp. Office - Misc.","key":6404,"banner":{"dba\_name":"Certified Angus Beef LLC - Wooster","key":6404},"last\_activity\_date":"2019-08-01T00:00:00","dba\_name":"Certified Angus Beef LLC - Wooster","volume\_13\_week":18661,"tags":,"input":{"type":"log"},"active\_status":"T","@timestamp":"2019-08-15T14:45:10.349Z","is\_currently\_licensed":true,"open\_issue\_count":0,"is\_previously\_licensed":false,"menu\_needed\_status":0,"location":{"lon":-81.9438063,"lat":40.8507256},"primary\_license\_key":43,"primary\_license\_status":"T"},"sort":[1,933.4909,"",0,1564617600000],"matched\_queries":["Company.DbaName.EdgeNgram.Match","Company.DbaName.EdgeNgram.MatchPhrase"]},{"\_index":"companies\_v3","\_type":"\_doc","\_id":"216875","\_score":933.4909,"\_source":{"parent":null,"agent":{"hostname":"BAUMANXPS15","id":"e3c44bd2-4f90-46af-a626-6c1cd7755f1e","ephemeral\_id":"ebbce2ae-a5dc-444d-b2f3-d6f74bc60f87","type":"filebeat","version":"7.3.0"},"log":{"file":{"path":"C:\Elasticsearch\Artifacts\input\filebeat\_companies.2019\_08\_15.json"},"offset":130279478},"city":"Makati City","geo\_accuracy":6,"state\_abbreviation":null,"star\_ranking":0,"country\_abbreviation":"PHILIP","ecs":{"version":"1.0.1"},"@version":"1","primary\_license\_description":"Restaurant - International","key":216875,"banner":null,"last\_activity\_date":null,"dba\_name":"Melo's Home of Certified Angus Beef","volume\_13\_week":0,"tags":,"input":{"type":"log"},"active\_status":"T","@timestamp":"2019-08-15T14:45:27.369Z","is\_currently\_licensed":false,"open\_issue\_count":0,"is\_previously\_licensed":false,"menu\_needed\_status":0,"location":{"lon":121.0330876,"lat":14.5494421},"primary\_license\_key":42,"primary\_license\_status":"F"},"sort":[0,933.4909,"",0,-9223372036854775808],"matched\_queries":["Company.DbaName.EdgeNgram.Match","Company.DbaName.EdgeNgram.MatchPhrase"]}

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [August 20, 2019, 9:27pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/6 "2019-08-20T21:27:07Z")

</div>

Do you also have the API call and what you're doing in .NET code with the response? I'd like to see this to try to reproduce the issue. Without these, I'm just guessing 😃

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [August 21, 2019, 12:57am UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/7 "2019-08-21T00:57:37Z")

</div>

> [@Airn5475](#):
>
> ```auto
> [Text(Name = "lat")] 
> public Latitude Latitude { get; set; } 
> 
> [Text(Name = "lon")] 
> public Longitude Longitude { get; set; }
> 
> ```

The serializer is expecting to deserialize a JSON object into each of the `Latitude` and `Longitude` types, but the response data has numeric values for these. I think these should be modelled as `double` e.g.

```auto
[PropertyName(Name = "lat")] 
public double Latitude { get; set; } 

[PropertyName(Name = "lon")] 
public double Longitude { get; set; }

```

An alternative to defining your own `GeoCoordinate` type is to use the `Nest.GeoCoordinate` type, which the JSON `{"lon":-81.9438063,"lat":40.8507256}` will deserialize into.

---

<div class="post-metadata">

**Author:** ![Airn5475](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/airn5475/32/13757_2.png) [@Airn5475](https://discuss.elastic.co/u/Airn5475)\
**Post date:** [August 21, 2019, 1:45pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/8 "2019-08-21T13:45:49Z")

</div>

My Latitude and Longitude classes have implicit casting to double built in, but nonetheless I tried changing to use the `PropertyName` attribute and the type of `double` with no luck.

---

<div class="post-metadata">

**Author:** ![Airn5475](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/airn5475/32/13757_2.png) [@Airn5475](https://discuss.elastic.co/u/Airn5475)\
**Post date:** [August 21, 2019, 2:26pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/9 "2019-08-21T14:26:42Z")

</div>

Quick Question: I am running Elasticsearch 7.3, but the Nuget package is for Elasticsearch 7.2, is this part of the problem perhaps?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [August 22, 2019, 5:44am UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/10 "2019-08-22T05:44:10Z")

</div>

> [@Airn5475](#):
>
> My Latitude and Longitude classes have implicit casting to double built in, but nonetheless I tried changing to use the `PropertyName` attribute and the type of `double` with no luck.

I would expect this to work. Do you have a full example to demonstrate?

> [@Airn5475](#):
>
> Quick Question: I am running Elasticsearch 7.3, but the Nuget package is for Elasticsearch 7.2, is this part of the problem perhaps?

Nope, [NEST 7.2.0 will work with Elasticsearch 7.3.0](https://github.com/elastic/elasticsearch-net#compatibility-matrix).

---

<div class="post-metadata">

**Author:** ![Airn5475](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/airn5475/32/13757_2.png) [@Airn5475](https://discuss.elastic.co/u/Airn5475)\
**Post date:** [August 22, 2019, 12:36pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/11 "2019-08-22T12:36:46Z")

</div>

Here is my GeoCoordinate class, which I created since I didn't want the dependency.

```
public class GeoCoordinate : IEquatable<GeoCoordinate>
{
	public static readonly GeoCoordinate Unknown = new GeoCoordinate();

	[PropertyName("lat")]
	public double Latitude { get; set; }

	[PropertyName("lon")]
	public double Longitude { get; set; }

	private GeoCoordinate() { }

	public GeoCoordinate(double latitude, double longitude)
	{
		if (double.IsNaN(latitude)) { throw new ArgumentNullException(nameof(latitude)); }
		if (double.IsNaN(longitude)) { throw new ArgumentNullException(nameof(longitude)); }

		Latitude = latitude;
		Longitude = longitude;
	}

	public GeoCoordinate(Latitude latitude, Longitude longitude)
	{
		Latitude = latitude;
		Longitude = longitude;
	}

	public bool IsUnknown => Equals(Unknown);

	public override string ToString()
	{
		return $"{Latitude},{Longitude}";
	}

	public override bool Equals(object other)
	{
		if (other == null) { throw new ArgumentNullException(nameof(other)); }
		return other is GeoCoordinate gc && Equals(gc);
	}

	public bool Equals(GeoCoordinate other)
	{
		if (other == null) { return false; }

		if (Latitude == null)
		{
			if (other.Latitude != null) { return false; }
		}
		else if (!Latitude.Equals(other.Latitude)) { return false; }

		if (Longitude == null)
		{
			if (other.Longitude != null) { return false; }
		}
		else if (!Longitude.Equals(other.Longitude)) { return false; }

		return true;
	}

	public override int GetHashCode()
	{
		return Latitude.GetHashCode() ^ Longitude.GetHashCode();
	}

	public static GeoCoordinate Parse(string coordinates)
	{
		if (coordinates.IsNullOrEmpty()) { return null; }
		var s = coordinates.Split(',');
		return new GeoCoordinate(s.First().ToDouble(), s.Last().ToDouble());
	}
}

public class Latitude : IEquatable<Latitude>
{
	private readonly double _value;

	public Latitude(double latitude)
	{
		if (latitude > 90.0 || latitude < -90.0)
		{
			throw new ArgumentOutOfRangeException(nameof(Latitude), latitude, "Latitude must be between 90 & -90.");
		}
		_value = latitude;
	}

	public static implicit operator Latitude(double latitude) => new Latitude(latitude);
	public static implicit operator Latitude(string latitude) => new Latitude(Convert.ToDouble(latitude));
	public static implicit operator double(Latitude latitude) => latitude._value;

	public override bool Equals(object other)
	{
		if (other == null) { throw new ArgumentNullException(nameof(other)); }
		var l = new Latitude(Convert.ToDouble(other));
		return Equals(l);
	}

	public bool Equals(Latitude other)
	{
		return other != null && _value.Equals(other._value);
	}

	public override string ToString()
	{
		return _value.ToString(CultureInfo.InvariantCulture);
	}

	public override int GetHashCode()
	{
		return _value.GetHashCode();
	}

}

public class Longitude : IEquatable<Longitude>
{
	private readonly double _value;

	public Longitude(double longitude)
	{
		if (longitude > 180.0 || longitude < -180.0)
		{
			throw new ArgumentOutOfRangeException(nameof(Latitude), longitude, "Longitude must be between 180 & -180.");
		}
		_value = longitude;
	}

	public static implicit operator Longitude(double longitude) => new Longitude(longitude);
	public static implicit operator Longitude(string longitude) => new Longitude(Convert.ToDouble(longitude));
	public static implicit operator double(Longitude longitude) => longitude._value;

	public override bool Equals(object other)
	{
		if (other == null) { throw new ArgumentNullException(nameof(other)); }
		var l = new Longitude(Convert.ToDouble(other));
		return Equals(l);
	}

	public bool Equals(Longitude other)
	{
		return other != null && _value.Equals(other._value);
	}

	public override string ToString()
	{
		return _value.ToString(CultureInfo.InvariantCulture);
	}

	public override int GetHashCode()
	{
		return _value.GetHashCode();
	}
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 19, 2019, 12:36pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-jsonparsingexception-expected/195801/12 "2019-09-19T12:36:47Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
