Giter Site home page Giter Site logo

clarketm / pprintjson Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 1.0 109 KB

A json pretty printer for Python ๐Ÿ

Home Page: https://pprintjson.readthedocs.io/en/latest/

License: MIT License

Python 76.89% Makefile 23.11%
python pprint json pprintjson ppjson

pprintjson's Introduction

PyPi release PyPi versions Downloads Documentation Status

A json pretty printer for Python ๐Ÿ.

Check out the pprintjson docs

Installation

Install with the standard json JSON encoder

$ pip install pprintjson

Install with the premier simplejson JSON encoder

$ pip install pprintjson[simplejson]

Usage


usage: pprintjson.py [-h] [-i num] [-o file] [-c cmd] [-v] [file]

A pretty-printing function for json.

positional arguments:
  file                    json <file> to pretty-print

optional arguments:
  -h, --help              show this help message and exit
  -i num, --indent num    indent <num> number of spaces at each level (default: 4)
  -o file, --output file  write output to <file> instead of stdout (default: stdout)
  -c cmd, --command cmd   json <cmd> to pretty-print
  -v, --version           show program's version number and exit

Script

Pretty print JSON from a file using the pprintjson CLI.

$ pprintjson "./path/to/file.json"

Pretty print JSON from a stdin using the pprintjson CLI.

$ echo '{ "a": 1, "b": "string", "c": true }' | pprintjson

Pretty print JSON from a string using the pprintjson CLI.

$ pprintjson -c '{ "a": 1, "b": "string", "c": true }'

Pretty print JSON from a string with an indent of 1.

$ pprintjson -c '{ "a": 1, "b": "string", "c": true }' -i 1

Pretty print JSON from a string and save output to a file output.json.

$ pprintjson -c '{ "a": 1, "b": "string", "c": true }' -o ./output.json

Module

Pretty print JSON from a dict using the pprintjson module.

# 1. import the "pprintjson" function.
from pprintjson import pprintjson as ppjson

# 2. pretty print JSON.
obj = { "a": 1, "b": "string", "c": True }

ppjson(obj)

stdout

License

MIT ยฉ Travis Clarke

pprintjson's People

Contributors

clarketm avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pprintjson's Issues

Feature request: save output as a string have `\n`

Thank you for write this lightweight tool! โ™ฅ

Sometimes maybe we don't want it only be printed on terminal, but save it in a string

# 1. import the "pprintjson" function.
from pprintjson import pprintjson as ppjson

# 2. pretty print JSON.
obj = { "a": 1, "b": "string", "c": True }

out=ppjson(obj)

For example, I wish out be the formatted {\n "a": 1,\n "b": "string",\n "c": True\n} and can be reused, without to call ppjson() once more.

Install pprintjson as a CLI tool?

pprintjson should be installed as a console script for use at the command line. It should accept input via string, file, or stdin.

Feature request: Have a more consise format

Thank you for you sweet JSON printer, I am using and enjoying it. ๐Ÿคฉ
But, pprintjson often uses unnecessary room and I request an option to get a more concise display.

You may be inspired from file prettyjson.py described in that SO answer.

Example of a concise representation converted to an unnecessary verbose one:

$ pprintjson -c '
{
  "grid": {"port": "COM5"},
  "policy": {
    "movingaverage": 5,
    "hysteresis": 5,
    "fan1": {
      "name": "CPU",
      "signal": "cpu",
      "mode": "auto",
      "speed": 100,
      "curve": [[0, 75], [50, 75], [75, 100]]
    }
  }
}'
{
    "grid": {
        "port": "COM5"
    },
    "policy": {
        "movingaverage": 5,
        "hysteresis": 5,
        "fan1": {
            "name": "CPU",
            "signal": "cpu",
            "mode": "auto",
            "speed": 100,
            "curve": [
                [
                    0,
                    75
                ],
                [
                    50,
                    75
                ],
                [
                    75,
                    100
                ]
            ]
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.