Skip to main content

JSON Parser Helper

The JsonParser library provides utility methods for parsing, manipulating, and querying JSON data. It allows for operations such as parsing JSON strings, modifying JSON structures, searching for elements, and adding or removing keys and values.

Library: ADS.Json.Helper

Service: JsonParser

1. Parse JSON​

JToken Parse(string json);

Description:

  • Parses a JSON string into a JToken object.

Parameters:

  • json (string) – The JSON string to parse.

Returns:

  • JToken – The parsed JSON object.

2. Stringify JSON​

string Stringify(JToken jToken, bool indented = true);

Description:

  • Converts a JToken object back into a formatted JSON string.

Parameters:

  • jToken (JToken) – The JSON object to convert.

  • indented (bool, optional) – Whether to format the output (default: true).

    Returns:

    • string – The formatted JSON string.

3. Check If Key Exists​

bool IsExist(JToken jToken, string path);

Description:

  • Checks if a specific key or value exists within a JSON object.

Parameters:

  • jToken (JToken) – The JSON object.

  • path (string) – The JSON path to check.

    Returns:

    • bool – True if the key exists, otherwise False.

4. Find All Matching Elements​

List<JToken> FindAll(JToken jToken, string path);

Description:

  • Searches for all elements in the JSON object that match the given JSON path.

Parameters:

  • jToken (JToken) – The JSON object.

  • path (string) – The JSON path to search for.

    Returns:

    • List<JToken> – A list of matching JSON elements.

5. Find First Matching Element​

JToken Find(JToken jToken, string path);

Description:

  • Finds the first occurrence of an element matching the JSON path.

Parameters:

  • jToken (JToken) – The JSON object.

  • path (string) – The JSON path to search for.

    Returns:

    • JToken – The first matching element.

6. Remove Element​

void RemoveFromObject(JToken jToken, string path);

Description:

  • Removes a key-value pair from a JSON object.

Parameters:

  • jToken (JToken) – The JSON object.
  • path (string) – The JSON path to remove.

7. Remove Element from Array​

void RemoveFromArray(JToken jToken, string path, int index);

Description:

  • Removes an element from a JSON array at the specified index.

Parameters:

  • jToken (JToken) – The JSON object.
  • path (string) – The JSON path to locate the array.
  • index (int) – The index of the element to remove.

8. Replace JSON Value​

JToken ReplaceValue(JToken jToken, string path, object value);

Description:

  • Replaces the value of a JSON key.

Parameters:

  • jToken (JToken) – The JSON object.

  • path (string) – The JSON path to locate the key.

  • value (object) – The new value to assign.

    Returns:

    • JToken – The updated JSON object.

9. Add Key-Value Pair​

JToken AddKey(JToken jToken, string path, string key, object value);

Description:

  • Adds a new key-value pair to an existing JSON object.

Parameters:

  • jToken (JToken) – The JSON object.

  • path (string) – The JSON path to locate the parent object.

  • key (string) – The new key.

  • value (object) – The value to assign.

    Returns:

    • JToken – The updated JSON object.

10. Add Value to Array​

JToken AddValue(JToken jToken, string path, object value);

Description:

  • Adds a new value to an existing JSON array.

Parameters:

  • jToken (JToken) – The JSON object.

  • path (string) – The JSON path to locate the array.

  • value (object) – The value to add.

    Returns:

    • JToken – The updated JSON object.

11. Get Value​

dynamic GetValue(JToken jToken, string path);

Description:

  • Retrieves the value of a JSON key.

Parameters:

  • jToken (JToken) – The JSON object.

  • path (string) – The JSON path to locate the key.

    Returns:

    • dynamic – The retrieved value.