A simple CLI tool for validating and formatting JSON data.
Project description
🐍 The jsoncons Package 🐛❇️🐉
🚙 JSON CLI Utility in Python 🐍
The jsoncons package is designed to provide a basic command-line interface for handling JSON data. This can be useful for simple scripting or interoperability tasks (e.g., having a COBOL program generate a text file that this tool converts to JSON, or vice versa).
Installation:
pip install jsoncons
Basic Usage for Pretty-Print JSON:
-
Create Input File If Necessary: In your project directory, verify there is a file named input.json with the following content:
{"key":"value", "items":[1,2]}
-
Validate & Pretty-print JSON: Read from stdin, write to stdout. (Linux Command)
echo '{"key":"value", "items":[1,2]}' | jsoncons encode
Windows Powershell Command: Read from stdin, write to stdout.
echo {"\"key\"":"\"value\"", "\"items\"":[1,2]} | jsoncons encode
-
Validate & Pretty-print JSON from file to file: (Tested on Windows 10)
jsoncons encode input.json output_pretty.json
-
(The
decodecommand might be an alias or offer slightly different formatting if needed)
🤝 Contributing 🖥️
Contributions are welcome! If you find errors, have suggestions for improvements, or want to add more examples, please feel free to:
- Open an issue to discuss the change.
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature-name). - Make your changes and commit them (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/your-feature-name). - Open a Pull Request.
📝 License ⚖️
This project is licensed under the MIT License - see the LICENSE file for details.
🧪 Unit Test Explanation For jsoncons Package ✅
- Imports: Imports necessary modules like
unittest,sys(for patching argv/streams),io(for capturing streams),os,json,tempfile,shutil, andunittest.mock.patch. It also imports theclimodule from the package. TestJsonConsCLIClass: Inherits fromunittest.TestCase.setUp:- Creates a temporary directory using
tempfile.mkdtemp()to isolate test files. - Defines paths for input, output, and invalid files within the temp directory.
- Creates sample valid and invalid JSON strings and data structures.
- Writes the sample valid and invalid JSON to the respective temporary files.
- Creates a temporary directory using
tearDown: Cleans up by removing the temporary directory and all its contents usingshutil.rmtree().run_cliHelper:- Takes a list of arguments (
args_list) and optionalstdin_data. - Prepends the script name (
'serial-json') to the arguments list assys.argv[0]. - Uses
unittest.mock.patchas a context manager to temporarily replacesys.argv,sys.stdout, andsys.stderrwith test-controlled objects (io.StringIOfor streams). - If
stdin_datais provided,sys.stdinis also patched. - Calls the actual
cli.main()function within the patched context. - Catches
SystemExit(whichsys.exit()raises) to get the exit code. - Returns the captured stdout string, stderr string, and the exit code.
- Takes a list of arguments (
- Test Methods (
test_...):- Each method tests a specific scenario (stdin/stdout, file I/O, options, errors).
- They call
run_cliwith appropriate arguments and/or stdin data. - They use
self.assertEqual,self.assertNotEqual,self.assertTrue,self.assertIn, etc., to verify:- The exit code (0 for success, non-zero for errors).
- The content of captured
stderr(should be empty on success, contain error messages on failure). - The content of captured
stdout(when output is expected there). - The existence and content of output files (when file output is expected).
if __name__ == '__main__':: Allows running the tests directly usingpython -m unittest tests.test_cliorpython tests/test_cli.py.
⛰️ Extending jsoncons to COBOL 👀
How COBOL could interact:
A COBOL program could:
- Write data to a temporary text file (e.g.,
input.txt). - Use
CALL 'SYSTEM'(or equivalent OS call) to execute the Python script:CALL 'SYSTEM' USING 'jsoncons input.txt output.json'.
- Read the resulting
output.jsonfile from COBOL.
Alternatively:
- COBOL generates simple key-value pairs or a structured text format.
- A more sophisticated
jsonconsencodecommand could be written to parse this specific text format and produce JSON. - A
jsonconsdecodecommand could parse JSON and output a simple text format readable by COBOL.
The provided CLI keeps things simple and standard, relying on JSON as the interchange format, which COBOL would interact with via file I/O and system calls.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsoncons-0.7.0.tar.gz.
File metadata
- Download URL: jsoncons-0.7.0.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28803d558a53990686034747f09aeb2baab87b8024f106ca3c86821d01943636
|
|
| MD5 |
243532d74228771d86325ed34e6dd5c0
|
|
| BLAKE2b-256 |
c24739f25ffe53963b8cd7493e5924bb9689918bfba80855623b3cbd09983bb2
|
File details
Details for the file jsoncons-0.7.0-py3-none-any.whl.
File metadata
- Download URL: jsoncons-0.7.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4489914bd7ab2ccbdad97f0ed59158893459f126fba649998e6236a884a4e68d
|
|
| MD5 |
a4dc2f611a86e35482eeff4eb471e171
|
|
| BLAKE2b-256 |
f7b905534a78cb4c230cfc9ae2fa900aa748d245e31c7250aff993bcbcb95083
|