Skip to content
XulbuX edited this page Sep 11, 2025 · 3 revisions

Json

This class includes methods for advanced working with JSON files and data.


read()

This method will replace all special space characters with normal spaces.
Params:

  • json_file: str the path to the json file
  • comment_start: str = ">>" the start of a custom json comment
  • comment_end: str = "<<" the end of a custom json comment
  • return_original: bool = False whether to also return the original data, including comments

Returns: the JSON data as a dictionary or a tuple with the processed data and the original data


create()

This method will create a nicely formatted JSON file from a dictionary.
Params:

  • json_file: str the file name or path, with or without file extension
  • data: dict the data to be written to the file
  • indent: int = 2 the amount of spaces to use for indentation
  • compactness: int = 1 how compact the data should be formatted (see Data.to_str())
  • force: bool = False whether to overwrite the file if it already exists

Returns: the absolute path of the created file


update()

Update single/multiple values inside JSON files, without needing to know the rest of the data.
Params:

  • json_file: str the path to the json file
  • update_values: dict[str, Any] the value-path/s along with the new value/s to be inserted
  • comment_start: str = ">>" the start of a custom json comment
  • comment_end: str = "<<" the end of a custom json comment
  • path_sep: str = "->" the separator of the value-path parts (see Data.get_path_id())

Returns:no return value

Update Values:

The update_values parameter is a dictionary, where the keys are the paths to the data to update, and the values are the new values to set.

Example:

For this JSON data:

{
  "healthy": {
    "fruit": ["apples", "bananas", "oranges"],
    "vegetables": ["carrots", "broccoli", "celery"]
  }
}

... the update_values dictionary could look like this:

{
    # CHANGE VALUE "apples" TO "strawberries"
    "healthy->fruit->0": "strawberries",
    # CHANGE VALUE UNDER KEY "vegetables" TO [1, 2, 3]
    "healthy->vegetables": [1, 2, 3]
}

In this example, if you want to change the value of "apples", you can use healthy->fruit->apples as the value-path.
If you don't know that the first list item is "apples", you can use the items list index inside the value-path, so healthy->fruit->0.
⇾ If the given value-path doesn't exist, it will be created.

★⠀Python Library by XulbuX⠀★

Project Links

Testing and Formatting

Classifiers

  • Intended Audience:
    • Developers
  • License:
    • OSI Approved
    • MIT License
  • Operating Systems:
    • Full Library: OS Independent
  • Supported Python Versions:
    • Python 3.13
    • Python 3.12
    • Python 3.11
    • Python 3.10
  • Topics:
    • Libraries
    • Python Modules
    • Software Development

The XulbuX Logo
Clone this wiki locally