Quelqu'un peut-il suggérer comment je peux embellir JSON en Python ou via la ligne de commande?
Le seul embellisseur JSON en ligne qui pouvait le faire était: http://jsonviewer.stack.hu/ .
Je dois cependant l'utiliser depuis Python.
Ceci est mon ensemble de données:
{ "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
la source
la source
Réponses:
Depuis la ligne de commande:
qui sort:
En termes de programmation, le manuel Python décrit un JSON joli et imprimable :
la source
Utilisez l'
indent
argument de ladumps
fonction dans le module json .À partir de la documentation:
la source
Une solution minimale en python qui colore les données json fournies via la ligne de commande:
Inspiré par
pjson
mentionné ci-dessus. Ce code doitpygments
être installé.Exemple de sortie:
la source
Essayez underscore-cli :
C'est un outil assez astucieux qui peut élégamment faire beaucoup de manipulation de données structurées, exécuter des extraits js, des modèles de remplissage, etc. Il est ridiculement bien documenté, raffiné et prêt pour une utilisation sérieuse. Et je l'ai écrit. :)
la source
La commande cli que j'ai utilisée avec python pour cela est:
Vous devriez pouvoir trouver plus d'informations ici:
http://docs.python.org/library/json.html
la source
python -mjson.tool < myfile.json
:.!python -mjson.tool
Il semble que jsbeautifier a ouvert ses outils et les a emballés sous forme de bibliothèques Python et JS, et d'outils CLI. Il ne semble pas qu'ils appellent un service Web, mais je n'ai pas vérifié de trop près. Voir le référentiel github avec les instructions d'installation.
À partir de leurs documents pour Python CLI et l'utilisation de la bibliothèque:
Pour embellir avec python:
La sortie embellie va à
stdout
.Utiliser
jsbeautifier
comme bibliothèque est simple:... ou, pour spécifier certaines options:
Si vous souhaitez passer une chaîne au lieu d'un nom de fichier et que vous utilisez bash, vous pouvez utiliser la substitution de processus comme suit:
la source
Je n'ai pas aimé la sortie de json.dumps (...) -> A mon goût beaucoup trop de nouvelles lignes. Et je ne voulais pas utiliser d'outil de ligne de commande ou installer quelque chose. J'ai enfin trouvé Pythons pprint (= jolie impression). Malheureusement, il ne génère pas le JSON approprié, mais je pense qu'il est utile d'avoir un glympse convivial pour les données stockées.
Sortie de
json.dumps(json_dict, indent=4)
Utilisation de pprint:
Résultat de
pprint.pformat(...)
oupprint.pprint(...)
:la source
Cela imprimera assez JSON qui se trouve dans le presse-papiers dans OSX. Copiez-le, puis appelez l'alias à partir d'une invite Bash.
la source
Vous pouvez diriger la sortie vers
jq
. Si votre script python contient quelque chose commealors vous pouvez tirer:
la source
Utilisez la bibliothèque d'outils python
Ligne de commande: python -mjson.tool
Dans le code: http://docs.python.org/library/json.html
la source
Première installation de pygments
puis
echo '<some json>' | python -m json.tool | pygmentize -l json
la source
Vos données sont mal formées. Les champs de valeur en particulier ont de nombreux espaces et de nouvelles lignes. Les formateurs automatisés ne fonctionneront pas là-dessus, car ils ne modifieront pas les données réelles. Lorsque vous générez les données pour la sortie, filtrez-les si nécessaire pour éviter les espaces.
la source
Avec jsonlint (comme xmllint):
la source