{"id":15373041,"url":"https://github.com/vi/json2","last_synced_at":"2025-04-15T12:31:50.054Z","repository":{"id":11012243,"uuid":"13338804","full_name":"vi/json2","owner":"vi","description":"xml2 for JSON; like gron","archived":false,"fork":false,"pushed_at":"2018-04-10T18:07:36.000Z","size":16,"stargazers_count":28,"open_issues_count":1,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-28T21:12:25.851Z","etag":null,"topics":["grep","json","sed","text-files"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-10-05T02:14:14.000Z","updated_at":"2024-06-08T02:12:18.000Z","dependencies_parsed_at":"2022-09-11T03:40:15.540Z","dependency_job_id":null,"html_url":"https://github.com/vi/json2","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vi%2Fjson2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vi%2Fjson2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vi%2Fjson2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vi%2Fjson2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vi","download_url":"https://codeload.github.com/vi/json2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249072291,"owners_count":21208157,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grep","json","sed","text-files"],"created_at":"2024-10-01T13:54:09.073Z","updated_at":"2025-04-15T12:31:49.832Z","avatar_url":"https://github.com/vi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"There is a [tool](http://www.ofb.net/~egnor/xml2/) to convert XML files to \nintermediate format that allows editing and data extraction to be performed \nwith simple (not XML-aware) tools, such as regular expressions-based `grep` \nor `sed`. It does not solve the general task of transforming XML files, but \nstill allows text handling tools to go farther than in case of \n[direct attempt to use them on XML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags).\n\nBut xml2 is for XML, and somebody may want the similar tool for JSON.\n\nHere there are two main tools plus several supplementrary ones:\n\n* json2 - converts JSON to intermediate text-editable format;\n* 2json - converts that intermediate format back to JSON;\n* json_compare - compares two JSON files to equality and reports the found difference, if any;\n* random_json - generates random \"tricky\" JSON (with confusing strings, empty objects, etc.);\n* json_keys - gathers keys used for in objects in the JSON\n* test.sh - endless \"fuzz test\" of `json2 | 2json` using random_json and json_compare.\n* json2dir and dir2json - \"unpacks\" JSON to files and directories and back;\n\nTested with Python 2.6, 2.7 and 3.2.\n\nExample\n---\n\nJSON file\n\n```\n{ \"mist\": \"qua\\nlity\\n\",\n  \"fist\": [],\n   \"gist\": [5,6,\"7\"],\n   \"...\": null,\n   \"test\":[[[false]]],\n   \"var\":{\"lib\":{\"dpkg\":\"status\"}}\n}\n```\n\nOutput of json2:\n\n```\n/...=null\n/gist/0=5\n/gist/1=6\n/gist/2=\"7\n/var/lib/dpkg=status\n/fist=[]\n/test/0/0/0=false\n/mist=\"qua\n/mist=\"lity\n/mist=\"\n```\n\nRules of the format\n---\n\n* Each line must contain \"=\". The first \"=\" on each line is always put by \njson2, \nsubsequent \"=\"s may happen in the data extracted from JSON;\n* The left part of the line before \"=\" is \"address\", the right part after the \nfirst \n\"=\" is \"value\".\n* Value can be string, number, null, float, boolean, empty list or empty object.\n* Any value that can't be interpreted as non-string is interpreted as string. \nUsing `\"` character just after `=` forces it to be a string. By default `json2` \nuses unescaped strings where possible: `if there_may_be_problems then \nprefix_with_\" else use_the_string_as_is`. `JSON2_ALWAYS_MARK_STRINGS=true` \noverrides this and makes json2 put `\"` before any string values.\n* Only empty lists and objects must be explicitly mentioned as values. Non-empty \nlists and objects still can have \"stubs\" like `=[]` or `={}` at the respective \naddress. `JSON2_ALWAYS_STUBS=true` forces stubs for all lists and objects.\n* Address is a list of keys separated by \"/\". The first empty key (before the \nfirst `/`) is ignored, subsequent empty keys are assumed as empty keys of \nobjects (for example, `{\"\":{\"\":\"\"}}` -\u003e `//=\"`). Each address entry \"descends\" \nfrom the top-level list of object into it's children (creating intermediate \nlists or objects if necessary).\n* Numeric keys are used as indexes (starting from 0) of the lists in JSON. \nNon-numeric keys are keys for object fileds.\n* All keys of object fileds are mangled to preserve assumptions about usage of \n`/`, `=`, `\"` and `\\n`  characters and to avoid mistakingly interepreting them\nas indexes for lists instead of keys for objects. Mangling rules are not \nstandard: apart from usual \\n, \\r and \\t, `/ \" = \\` becomes `\\| \\' \\_ \\!`. \nAdditionally the entire key may be prefixed with `\\` if it looks like a number.\n* Multiline string values are handled as repeated lines (with the same address).\n* Apart from multi-line string values, lines in `2json` input file may be reordered arbitrarily.\n\nLimitations\n---\n\n* Order of fields in objects is not preserved;\n* 2json is slow. It navigates into the hierarchy of objects and lists from the \nroot for every line;\n* All tools load the entire input file in memory as a tree, \nnot \"streamed\".\n* Is may be poor option if you need to handle recursive JSON files.\n* There may be corner case incompabilitis between json2 format generated when executing by Python 2 and Python 3. For example `+ 1` is not considered a valid number of Python 3, hence not prepended with `\\`.\n* Round-trip test fails on Python 2 in tricky corner case (involving tricky characters in keys) \n\nSee also\n---\n\n* [gron](https://github.com/tomnomnom/gron)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvi%2Fjson2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvi%2Fjson2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvi%2Fjson2/lists"}