Skip to main content

A simple CLI tool for validating and formatting JSON data.

Project description

🐍 The jsoncons Package 🐛❇️🐉

🚙 JSON CLI Utility in Python 🐍

License: MIT Python Version PyPI version

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 decode command might be an alias or offer slightly different formatting if needed)

Preview Branch v1.0.1

Features In Development for v1.1.0

  • Improved FP COBOL-to-JSON functionality
  • Jupyter Notebook Tutorial
  • Template Python Function using jsoncons

🤝 Contributing 🖥️

Contributions are welcome! If you find errors, have suggestions for improvements, or want to add more examples, please feel free to:

  1. Open an issue to discuss the change.
  2. Fork the repository.
  3. Create a new branch (git checkout -b feature/your-feature-name).
  4. Make your changes and commit them (git commit -m 'Add some feature').
  5. Push to the branch (git push origin feature/your-feature-name).
  6. 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 ✅

  1. Imports: Imports necessary modules like unittest, sys (for patching argv/streams), io (for capturing streams), os, json, tempfile, shutil, and unittest.mock.patch. It also imports the cli module from the package.
  2. TestJsonConsCLI Class: Inherits from unittest.TestCase.
  3. 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.
  4. tearDown: Cleans up by removing the temporary directory and all its contents using shutil.rmtree().
  5. run_cli Helper:
    • Takes a list of arguments (args_list) and optional stdin_data.
    • Prepends the script name ('serial-json') to the arguments list as sys.argv[0].
    • Uses unittest.mock.patch as a context manager to temporarily replace sys.argv, sys.stdout, and sys.stderr with test-controlled objects (io.StringIO for streams).
    • If stdin_data is provided, sys.stdin is also patched.
    • Calls the actual cli.main() function within the patched context.
    • Catches SystemExit (which sys.exit() raises) to get the exit code.
    • Returns the captured stdout string, stderr string, and the exit code.
  6. Test Methods (test_...):
    • Each method tests a specific scenario (stdin/stdout, file I/O, options, errors).
    • They call run_cli with 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).
  7. if __name__ == '__main__':: Allows running the tests directly using python -m unittest tests.test_cli or python tests/test_cli.py.

⛰️ Extending jsoncons to COBOL 👀

How COBOL could interact:

A COBOL program could:

  1. Write data to a temporary text file (e.g., input.txt).
  2. Use CALL 'SYSTEM' (or equivalent OS call) to execute the Python script:
    CALL 'SYSTEM' USING 'jsoncons input.txt output.json'.
    
  3. Read the resulting output.json file from COBOL.

Alternatively:

  1. COBOL generates simple key-value pairs or a structured text format.
  2. A more sophisticated jsoncons encode command could be written to parse this specific text format and produce JSON.
  3. A jsoncons decode command 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsoncons-1.0.1.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsoncons-1.0.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file jsoncons-1.0.1.tar.gz.

File metadata

  • Download URL: jsoncons-1.0.1.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for jsoncons-1.0.1.tar.gz
Algorithm Hash digest
SHA256 28e856fa73dcf841810661a29bb39382e2dbea535c1706b03d6174f56cc9a0c2
MD5 d23f6a2b517abdff2f5e7a82f83b0a15
BLAKE2b-256 569895353f236f9561b2f54f350defc187211ea0713281dcec72eaa8c32af7bd

See more details on using hashes here.

File details

Details for the file jsoncons-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: jsoncons-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for jsoncons-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 db12bd67d390f84bd07de7feb93d4108caf07ee2363e892dd0ba3df975d015f4
MD5 60b218ada17c13414a657d7e015f1f86
BLAKE2b-256 d738a6c645fbbfbb01bbc18340b4cd05e8aac2a562af6d248314ffd481e063b9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page