System.Text.Json.JsonException: Serialization JSON error for the invalid start of a property name

Issue Description

.NET code serialization using gives an error for a single quote and forces you to use a double quote.

System.Text.Json.JsonException: '''' is an invalid start of a property name. Expected a '"'. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'

or

System.Text.Json.JsonException: “'s' is an invalid start of a value is an invalid start of a property name. Expected a '"'. Path: $ | LineNumber: 0 | BytePositionInLine: 1.' 

is an invalid start of a property name

Resolution

I had this issue recently for one of the existing codes which was working perfectly fine with NewtonSoft JSON as a serializer.

I was trying to replace Newtonsoft with System.Text.Json.

The issue I found to be due to strict JSON specification usage by Microsoft as per RFC 8259.

Few issues I was able to fix easily by using some options but some issues which I thought Initially, could be fixable but found the limitation on System.Text.Json’s usage.

Reference: RFC 8259 

As per Microsoft,

A value enclosed in single quotes will result in a JsonException. System.Text.Json shall accept property names and string values only in double-quotes as per RFC 8259 specification.

Below JSON sample below are considered in Invalid format,

Invalid Format of JSON

Invalid Format which throws JsonException,

A bellow combination of ” ” and ‘ ‘ representation is not allowed in JSON.

{
  'FirstName': "ABCD"
}

OR

{
  'FirstName': "ABCD"
}

Note- Above both formats however works perfectly fine for Newtonsoft.Json serialization. but you may want to consider that as short term solution ?

Valid Format of JSON

{
  "FirstName": "ABCD"
}

Example

SystemTextJsonJsonException'''' is an invalid start of a property name

OR

SystemTextJsonJsonException is an invalid start of a property name

So here although this is not the resolution but rather additional information on the current design and limitation on System.Text.Json usage.

So please plan accordingly… and good luck.

Other scenarios causing “invalid start of a property name”

Comments in JSON fields are not allowed as default behavior while using System.Text.Json.

If you have comments or trailing commas in the JSON, then you might see the above erros.

Example:


{
  "Bank": "Bank of ABCD", //This is Bank Id
  "BranchInfo": [
    {
        "Location": 
          {
            "Street": "address1", //
            "Zip": "Zip",       /*This is zip code*/
            "State": "USA"
          }  
    }
  ]
}

To fix the issue either

  • remove comments or
  • add a JSON option with a serializer or deserializer to allow the comments.

Please see the below article for more details,

Other Useful Reference:

Did I miss anything else in these resolution steps?

Did the above steps resolve your issue? Please sound off your comments below!

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Please Subscribe to the blog to get a notification on freshly published blogs and best practices of software development.

8 thoughts on “System.Text.Json.JsonException ‘is an invalid start of a property name’

  1. They should have provided JsonDocumentOptions for forgiving the single quote and probably other stuff I have not found yet. I already have json in production with single quotes.

    This hack is not ideal but worked for me. Yeah, I don’t have an issue with the added parse time for now.

    var tmp = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
    json = Newtonsoft.Json.JsonConvert.SerializeObject(tmp);
    doc = JsonDocument.Parse(json, documentOptions);

  2. This makes the System.Text.Json unusable. I read JSON from thirdparties and require some slack in the JSON interpretation for my intergrations to work. Being to strict with the RFC just kills the use of this lib.

    1. Thanks Michael- I understand your concerns on using System.Text.Json limitations with third-party. Considering long-term benefits it is recommended to follow RFC specifications and guidelines.

Leave a Reply

Your email address will not be published. Required fields are marked *