Package: canif
Parser and pretty-printer for JSON and JSON-ish data, on the command line or as a Python library.SynopsisSuppose you have a JavaScript object literal:$catinput.jssetConfig({editable: true, latlng: new LatLng(48.1434, 17.1082), last_modified: new Date(1995, 11, 17)})or a Pythonreprexpression:$catinput.py{"_id": 73, "occur": OrderedDict([("doc1", 10), ("doc2", 1)]), "match": <_sre.SRE_Match object at 0x7f2b9eaa5f80>}or a MongoDB document:$catinput.mongo{"id": ObjectId("507f191e810c19729de860ea"), "last_modified": ISODate("2020-05-20T00:00:00Z")}You can use thecaniflibrary to load up that data as Plain Old Data Structures:>>>withopen('input.js','rt',encoding='UTF-8')asfile_in:...canif.load(file_in){'$$setConfig':[{'editable':True,'latlng':{'$$new LatLng':[48.1434,17.1082]},'last_modified':{'$$new Date':[1995,11,17]}}]}>>>withopen('input.py','rt',encoding='UTF-8')asfile_in:...canif.load(file_in){'_id':73,'occur':{'$$OrderedDict':[[('doc1',10),('doc2',1)]]},'match':'$repr<_sre.SRE_Match object at 0x7f2b9eaa5f80>'}>>>withopen('input.mongo','rt',encoding='UTF-8')asfile_in:...canif.load(file_in){'id':{'$oid':['507f191e810c19729de860ea']},'last_modified':{'$$ISODate':['2020-05-20T00:00:00Z']}}Think of it as a version ofjson.loadsthat accepts a wider syntax.This can be useful when writing Web scrapers, as it allows you to parse JavaScript data structures without having to use a
JavaScript interpreter or a full-blown syntax parser.Command line usageYou can also use thecanifcommand-line tool to pretty-print JSON-ish data:$canif<input.jssetConfig({editable: true,latlng: new LatLng(48.1434,17.1082,),last_modified: new Date(1995,11,17,),},)$canif<input.py{"_id": 73,"occur": OrderedDict([("doc1",10,),("doc2",1,),],),"match": <_sre.SRE_Match object at 0x7f2b9eaa5f80>,}$canif<input.mongo{"id": ObjectId("507f191e810c19729de860ea",),"last_modified": ISODate("2020-05-20T00:00:00Z",),}There's an option to convert the data to proper JSON:$canif-j<input.js{"$$setConfig": [{"editable": true,"latlng": {"$$new LatLng": [48.1434,17.1082]},"last_modified": {"$$new Date": [1995,11,17]}}]}The program pretty-prints the data as it loads it, so it should be able to handle very large files.Use--helpfor a quick rundown of available options:$canif--helpusage: canif [-h] [-i N | -f] [-j] [-T] [--single-document][--ensure-ascii] [-I ENCODING] [-O ENCODING]Pretty-print JSON and JSON-ish dataoptional arguments:-h, --help            show this help message and exit-i N, --indent N      Indent each level by N spaces (0 means flat,single-line output)-f, --flatten         Flatten output (equivalent to -i 0)-j, --json-output     Convert data to valid JSON if it wasn'talready (e.g. None becomes null)-T, --no-trailing-commasDon't insert trailing commas after the lastitem in a sequence. This is implied by--json-output.--single-document     Check that the input consists of a singledocument, rather that the default ofaccepting a stream of documents--ensure-ascii        Ensure JSON output is ASCII by using \uXXXXsequences in place of non-ASCII characters-I ENCODING, --input-encoding ENCODINGCharacter set used for decoding the input(default: UTF-8)-O ENCODING, --output-encoding ENCODINGCharacter set used for encoding the output(default: UTF-8)Emacs bindingsIf you happen to use Emacs'shell-mode, this can be very useful to format the logs or output of any Python program.Seecanif.elfor a simple function that you can bind to a keystroke and that will pretty-print the data structure
at point.Supported formatsCanif is for "JSON and JSON-ish" data. It can parse:plain JSONJavaScript object literals, including:unquoted keystrailing commas//commentsempty array slots (e.g.[1,,,4])printed Python data structures, includingsets:{42}tuples:("one", "two", "three")<repr>representationsMongoDB BSONThe parsed data can be pretty-printed in a couple formats:verbatim (i.e. only whitespace and commas are modified)strict JSONThere are options to control the output, e.g. indent depth, trailing commas, etc.