¿Cómo embellecer JSON en Python?

¿Alguien puede sugerir cómo puedo embellecer JSON en Python o mediante la línea de comandos?

El único embellecedor JSON basado en línea que podía hacerlo era: http://jsonviewer.stack.hu/ .

Necesito usarlo desde Python, sin embargo.

Este es mi conjunto de datos:

{ "head": {"vars": [ "address" , "description" ,"listprice" ]} , "results": { "bindings": [ { "address" : { "type":"string", "value" : " Dyne Road, London NW6"}, "description" :{ "type":"string", "value" : "6 bed semi detached house"}, "listprice" : { "type":"string", "value" : "1,150,000"} } , { "address" : { "type":"string", "value" : " Tweedy Road, Bromley BR1"}, "description" :{ "type":"string", "value" : "5 bed terraced house"}, "listprice" : { "type":"string", "value" : "550,000"} } , { "address" : { "type":"string", "value" : " Vera Avenue, London N21"}, "description" :{ "type":"string", "value" : "4 bed detached house"}, "listprice" : { "type":"string", "value" : " 995,000 "} } , { "address" : { "type":"string", "value" : " Wimbledon Park Side, London SW19"}, "description" :{ "type":"string", "value" : "3 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Westbere Road, West Hampstead, London NW2"}, "description" :{ "type":"string", "value" : "5 bedroom semi detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " The Avenue, Hatch End, Pinner HA5"}, "description" :{ "type":"string", "value" : "5 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Princes Park Avenue, London NW11"}, "description" :{ "type":"string", "value" : "4 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Canons Drive, Edgware HA8"}, "description" :{ "type":"string", "value" : "4 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Westbere Road, West Hampstead NW2"}, "description" :{ "type":"string", "value" : "5 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Haymills Estate, Ealing, London"}, "description" :{ "type":"string", "value" : "5 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Dene Terrace Woodclyffe Drive, Chislehurst, Kent BR7"}, "description" :{ "type":"string", "value" : "5 bedroom terraced house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Dene Terrace Woodclyffe Drive, Chislehurst, Kent BR7"}, "description" :{ "type":"string", "value" : "5 bedroom semi detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Northwick Close, St John's Wood NW8"}, "description" :{ "type":"string", "value" : "3 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Claremont Gardens, Surbiton KT6"}, "description" :{ "type":"string", "value" : "13 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Dene Terrace Woodclyffe Drive, Chislehurst, Kent BR7"}, "description" :{ "type":"string", "value" : "5 bedroom end terrace house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Stamford Road, London N1"}, "description" :{ "type":"string", "value" : "4 bedroom terraced house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Stanhope Avenue, London N3"}, "description" :{ "type":"string", "value" : "6 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Haymills Estate, Ealing, London"}, "description" :{ "type":"string", "value" : "5 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Elms Crescent, London SW4"}, "description" :{ "type":"string", "value" : "5 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Princes Park Avenue, London NW11"}, "description" :{ "type":"string", "value" : "4 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Abbeville Road, London SW4"}, "description" :{ "type":"string", "value" : "4 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Canons Drive, Edgware HA8"}, "description" :{ "type":"string", "value" : "4 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Henson Avenue, Willesdon Green NW2"}, "description" :{ "type":"string", "value" : "5 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Woodstock Road, London NW11"}, "description" :{ "type":"string", "value" : "5 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Tamworth Street, London SW6"}, "description" :{ "type":"string", "value" : "5 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Stanhope Avenue, Finchley, London"}, "description" :{ "type":"string", "value" : "5 bedroom semi detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " The Old Burlington, Church Street, London W4"}, "description" :{ "type":"string", "value" : "3 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Ebury Close, Northwood HA6"}, "description" :{ "type":"string", "value" : "4 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Middleton Road, London NW11"}, "description" :{ "type":"string", "value" : "4 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Henson Avenue, Willesden Green NW2"}, "description" :{ "type":"string", "value" : "5 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Huron Road, London SW17"}, "description" :{ "type":"string", "value" : "6 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Corringway, Ealing W5"}, "description" :{ "type":"string", "value" : "5 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Woodlands Avenue, New Malden KT3"}, "description" :{ "type":"string", "value" : "5 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Gunnersbury Park Area, Ealing, London"}, "description" :{ "type":"string", "value" : "6 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Blenheim Gardens, London, Brent NW2"}, "description" :{ "type":"string", "value" : "6 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Creighton Road, London NW6"}, "description" :{ "type":"string", "value" : "4 bedroom terraced house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Plaistow Lane, Bromley BR1"}, "description" :{ "type":"string", "value" : "7 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Greenfield Gardens, London NW2"}, "description" :{ "type":"string", "value" : "4 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Hendon Avenue, London N3"}, "description" :{ "type":"string", "value" : "3 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Peckham Park Road, London SE15"}, "description" :{ "type":"string", "value" : "6 bedroom semi detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Woodclyffe Drive, Chislehurst BR7"}, "description" :{ "type":"string", "value" : "5 bedroom house for sale"}, "listprice" : { "type":"string", "value" : " From 1,250,000 "} } , { "address" : { "type":"string", "value" : " Highwood Hill, Mill Hill, London"}, "description" :{ "type":"string", "value" : "5 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Stanhope Avenue, London N3"}, "description" :{ "type":"string", "value" : "5 bedroom semi detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Kersley Mews, London SW11"}, "description" :{ "type":"string", "value" : "3 bedroom mews for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Ebury Close, Northwood HA6"}, "description" :{ "type":"string", "value" : "4 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Ellesmere Road, Chiswick W4"}, "description" :{ "type":"string", "value" : "6 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " The Avenue, Hatch End, Pinner, Middlesex"}, "description" :{ "type":"string", "value" : "5 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Wandsworth, London SW18"}, "description" :{ "type":"string", "value" : "6 bedroom semi detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Carlton Road, New Malden KT3"}, "description" :{ "type":"string", "value" : "4 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " St Mary's Mews, Ealing W5"}, "description" :{ "type":"string", "value" : "3 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Ritherdon Road, Balham, London SW17"}, "description" :{ "type":"string", "value" : "5 bedroom semi detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Goldsmith Avenue, London W3"}, "description" :{ "type":"string", "value" : "5 bedroom property for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } , { "address" : { "type":"string", "value" : " Plaistow Lane, Bromley, Kent BR1"}, "description" :{ "type":"string", "value" : "7 bedroom detached house for sale"}, "listprice" : { "type":"string", "value" : " 1,250,000 "} } ] } } 

Desde la línea de comandos:

 echo '{"one":1,"two":2}' | python -mjson.tool 

que produce:

 { "one": 1, "two": 2 } 

Programáticamente, el manual de Python describe JSON de impresión bonita :

 >>> import json >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) { "4": 5, "6": 7 } 

Utilice el argumento de indent de la función de dumps en el módulo json .

De los documentos:

 >>> import json >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) { "4": 5, "6": 7 } 

Una solución mínima en Python que colorea los datos json suministrados a través de la línea de comandos:

 import sys import json from pygments import highlight, lexers, formatters formatted_json = json.dumps(json.loads(sys.argv[1]), indent=4) colorful_json = highlight(unicode(formatted_json, 'UTF-8'), lexers.JsonLexer(), formatters.TerminalFormatter()) print(colorful_json) 

Inspirado por pjson mencionado anteriormente. Este código necesita que se instalen pygments .

Ejemplo de salida:

introduzca la descripción de la imagen aquí

Intenta subrayar-cli :

 cat myfile.json | underscore print --color 

Es una herramienta bastante ingeniosa que puede hacer una gran cantidad de manipulación de datos estructurados, ejecutar fragmentos de código js, ​​rellenar plantillas, etc. Está ridículamente bien documentada, pulida y lista para su uso serio. Y lo escribí. 🙂

El comando cli que he usado con python para esto es:

 cat myfile.json | python -mjson.tool 

Debes poder encontrar más información aquí:

http://docs.python.org/library/json.html

Parece que jsbeautifier open obtuvo sus herramientas y las empaquetó como Python y JS libs, y como herramientas CLI. No parece que llamen a un servicio web, pero no revisé demasiado de cerca. Ver el repository github con instrucciones de instalación.


De sus documentos para el uso de la CLI y la biblioteca de Python:

Para embellecer utilizando python:

 $ pip install jsbeautifier $ js-beautify file.js 

La salida embellecida va a la salida stdout .

Usar jsbeautifier como biblioteca es simple:

 import jsbeautifier res = jsbeautifier.beautify('your javascript string') res = jsbeautifier.beautify_file('some_file.js') 

… o, para especificar algunas opciones:

 opts = jsbeautifier.default_options() opts.indent_size = 2 res = jsbeautifier.beautify('some javascript', opts) 

Si desea pasar una cadena en lugar de un nombre de archivo, y está usando bash, puede usar la sustitución de procesos de la siguiente manera:

 $ js-beautify <(echo '{"some": "json"}') 
 alias jsonp='pbpaste | python -m json.tool' 

Esto imprimirá bastante JSON que está en el portapapeles en OSX. Solo tienes que copiarlo y luego llamar al alias desde un indicador de Bash.

Primero instala pygments

entonces

echo '' | python -m json.tool | pygmentize -l json

Podrías canalizar la salida a jq . Si tu script en python contiene algo como

 print json.dumps(data) 

entonces puedes disparar:

 python foo.py | jq '.' 

Usa la biblioteca de herramientas de Python

Línea de comando: python -mjson.tool

En el código: http://docs.python.org/library/json.html

Sus datos están mal formados. Los campos de valor en particular tienen numerosos espacios y nuevas líneas. Los formateadores automáticos no funcionarán en esto, ya que no modificarán los datos reales. A medida que genere los datos para la salida, filtre según sea necesario para evitar los espacios.

Con jsonlint (como xmllint):

 aptitude install python-demjson jsonlint -f foo.json