Skip to content

Corona-Studio/JsonRepairSharp

Repository files navigation

JsonRepairSharp CodeQL Advanced .NET

Repair invalid JSON documents.

This is the C# version of the original project: https://github.com/josdejong/jsonrepair

Read the background article "How to fix JSON and validate it with ease"

How to use?

using JsonRepairSharp.Class;

// ...

var fixedJson = JsonRepairCore.JsonRepair(rawJson);

// ...

You can also catch the JsonRepairError exception to handle the case where the library failed to fix the JSON.

Items to fix

  • Add missing quotes around keys
  • Add missing escape characters
  • Add missing commas
  • Add missing closing brackets
  • Repair truncated JSON
  • Replace single quotes with double quotes
  • Replace special quote characters like “...” with regular double quotes
  • Replace special white space characters with regular spaces
  • Replace Python constants None, True, and False with null, true, and false
  • Strip trailing commas
  • Strip comments like /* ... */ and // ...
  • Strip ellipsis in arrays and objects like [1, 2, 3, ...]
  • Strip JSONP notation like callback({ ... })
  • Strip escape characters from an escaped string like {\"stringified\": \"content\"}
  • Strip MongoDB data types like NumberLong(2) and ISODate("2012-12-19T06:01:17.171Z")
  • Concatenate strings like "long text" + "more text on next line"
  • Turn newline delimited JSON into a valid JSON array, for example:
    { "id": 1, "name": "John" }
    { "id": 2, "name": "Sarah" }
  • The JsonRepairSharp library has streaming support and can handle infinitely large documents.