Extension methods providing convenient access to the LenientJsonParser. These extensions allow you to parse potentially malformed JSON strings (e.g., from LLM output) using a fluent API style.
LenientJsonExtensions
01 Syntax
Microsoft.VisualBasic.MIME.application.json.LenientJson.LenientJsonExtensions
02 Methods
| Name | Overloads | Summary |
|---|---|---|
| ParseJsonLenient | 1 | Parse a JSON string leniently, tolerating common syntax errors produced by Large Language Models (LLMs). |
| LoadLenientJson | 1 | Load and parse a JSON file leniently from the specified file path. |
| RepairJson | 1 | Repair a malformed JSON string by parsing it leniently and then re-serializing it to produce valid JSON. |
03 Members
ParseJsonLenient(
String)Parse a JSON string leniently, tolerating common syntax errors produced by Large Language Models (LLMs).
Parameters
| Name | Type | Description |
|---|---|---|
json | String | The JSON text to parse (possibly malformed). |
Returns
The parsed JsonElement, or Nothing if the input is empty or contains no recognizable JSON value.
Example
Dim json As String = "{name: 'Alice', age: 30,}" ' Unquoted key, single quote, trailing comma
Dim obj As JsonElement = json.ParseJsonLenient()
Console.WriteLine(obj("name").ToString()) ' Output: Alice
LoadLenientJson(
String)Load and parse a JSON file leniently from the specified file path.
Parameters
| Name | Type | Description |
|---|---|---|
file | String | The path to the JSON file. |
Returns
The parsed JsonElement, or Nothing if the file does not exist or is empty.
RepairJson(
String)Repair a malformed JSON string by parsing it leniently and then re-serializing it to produce valid JSON.
Parameters
| Name | Type | Description |
|---|---|---|
json | String | The malformed JSON text. |
Returns
A valid JSON string if parsing succeeded; otherwise, the original input string is returned unchanged.
Example
Dim malformed As String = "{name: 'Alice', age: 30,}"
Dim repaired As String = malformed.RepairJson()
' repaired = {"name":"Alice","age":30}