Alguém pode sugerir como posso embelezar JSON em Python ou por meio da linha de comando?
O único embelezador JSON online que poderia fazer isso era: http://jsonviewer.stack.hu/ .
Preciso usá-lo de dentro do Python, no entanto.
Este é meu conjunto de dados:
{ "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
"}
}
] } }
python
json
command-line
Khwaishien
fonte
fonte
Respostas:
Na linha de comando:
que produz:
De forma programática, o manual Python descreve JSON de impressão bonita :
fonte
Use o
indent
argumento dadumps
função no módulo json .Dos documentos:
fonte
Uma solução in-python mínima que colore os dados json fornecidos por meio da linha de comando:
Inspirado no
pjson
mencionado acima. Este código precisapygments
ser instalado.Exemplo de saída:
fonte
Experimente o underscore-cli :
É uma ferramenta muito bacana que pode fazer muita manipulação de dados estruturados de maneira elegante, executar snippets js, preencher modelos, etc. É ridiculamente bem documentada, polida e pronta para uso sério. E eu escrevi. :)
fonte
O comando cli que usei com o python para isso é:
Você deve encontrar mais informações aqui:
http://docs.python.org/library/json.html
fonte
python -mjson.tool < myfile.json
:.!python -mjson.tool
Parece que o jsbeautifier abriu o código-fonte de suas ferramentas e as empacotou como bibliotecas Python e JS e como ferramentas CLI. Não parece que eles chamam um serviço da web, mas não verifiquei muito de perto. Consulte o repositório github com instruções de instalação.
De seus documentos para Python CLI e uso de biblioteca:
Para embelezar usando python:
A saída embelezada vai para
stdout
.Para usar
jsbeautifier
como biblioteca é simples:... ou, para especificar algumas opções:
Se você quiser passar uma string em vez de um nome de arquivo e estiver usando o bash, poderá usar a substituição de processo da seguinte maneira:
fonte
Eu não gostei da saída de json.dumps (...) -> Para o meu gosto muitas novas linhas. E eu não queria usar uma ferramenta de linha de comando ou instalar algo. Eu finalmente encontrei pprint do Pythons (= impressão bonita). Infelizmente, ele não gera JSON adequado, mas acho que é útil ter um símbolo amigável nos dados armazenados.
Saída de
json.dumps(json_dict, indent=4)
Uso de pprint:
Resultado de
pprint.pformat(...)
oupprint.pprint(...)
:fonte
Isso imprimirá muito bem o JSON que está na área de transferência do OSX. Basta copiá-lo e, em seguida, chamar o alias de um prompt Bash.
fonte
Você pode canalizar a saída para
jq
. Se você python script contém algo comoentão você pode disparar:
fonte
Use a biblioteca de ferramentas Python
Linha de comando: python -mjson.tool
No código: http://docs.python.org/library/json.html
fonte
Primeira instalação de pigmentos
então
echo '<some json>' | python -m json.tool | pygmentize -l json
fonte
Seus dados estão mal formados. Os campos de valor, em particular, têm vários espaços e novas linhas. Os formatadores automatizados não funcionarão nisso, pois não modificarão os dados reais. Conforme você gera os dados para saída, filtre-os conforme necessário para evitar os espaços.
fonte
Com jsonlint (como xmllint):
fonte