Может ли кто-нибудь подсказать, как я могу украсить JSON в Python или через командную строку?
Единственный онлайн-украситель JSON, который мог это сделать, был: http://jsonviewer.stack.hu/ .
Однако мне нужно использовать его из Python.
Это мой набор данных:
{ "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
источник
источник
Ответы:
Из командной строки:
который выводит:
Программно руководство по Python описывает довольно печатный JSON :
источник
Используйте
indent
аргументdumps
функции в модуле json .Из документов:
источник
Минимальное решение на Python, которое окрашивает данные json, передаваемые через командную строку:
Вдохновлено
pjson
упомянутым выше. Этот код необходимоpygments
установить.Пример вывода:
источник
Попробуйте underscore-cli :
Это довольно изящный инструмент, который может элегантно выполнять множество операций со структурированными данными, выполнять js-фрагменты, шаблоны заполнения и т. Д. Он до смешного хорошо документирован, отполирован и готов к серьезному использованию. И я это написал. :)
источник
Команда cli, которую я использовал для этого с python:
Здесь вы можете найти дополнительную информацию:
http://docs.python.org/library/json.html
источник
python -mjson.tool < myfile.json
:.!python -mjson.tool
Похоже, jsbeautifier открыла исходный код своих инструментов и упаковала их как библиотеки Python и JS, а также как инструменты CLI. Не похоже, что они обращаются к веб-сервису, но я не слишком внимательно проверял. См. Репозиторий github с инструкциями по установке.
Из их документов для Python CLI и использования библиотеки:
Чтобы украсить с помощью Python:
Украшенный выход идет в
stdout
.Использовать
jsbeautifier
как библиотеку просто:... или, чтобы указать некоторые параметры:
Если вы хотите передать строку вместо имени файла и используете bash, вы можете использовать замену процесса следующим образом:
источник
Мне не понравился вывод json.dumps (...) -> На мой вкус, слишком много новых строк. И я не хотел использовать инструмент командной строки или что-то устанавливать. Наконец-то я нашел Pythons pprint (= красивый отпечаток). К сожалению, он не генерирует правильный JSON, но я думаю, что полезно иметь удобный глимпс для сохраненных данных.
Выход
json.dumps(json_dict, indent=4)
Использование pprint:
Результат
pprint.pformat(...)
илиpprint.pprint(...)
:источник
Это будет довольно печатать JSON, который находится в буфере обмена в OSX. Просто скопируйте его, затем вызовите псевдоним из командной строки Bash.
источник
Вы можете направить вывод в
jq
. Если ваш скрипт python содержит что-то вродетогда вы можете стрелять:
источник
Используйте библиотеку инструментов Python
Командная строка: python -mjson.tool
В коде: http://docs.python.org/library/json.html
источник
Сначала установите пигменты
затем
echo '<some json>' | python -m json.tool | pygmentize -l json
источник
Ваши данные плохо сформированы. В частности, поля значений содержат множество пробелов и новых строк. Автоматические программы форматирования не будут работать с этим, поскольку они не будут изменять фактические данные. По мере создания данных для вывода фильтруйте их по мере необходимости, чтобы избежать пробелов.
источник
С jsonlint (например, xmllint):
источник