Dieser Post wurde aus meiner alten WordPress-Installation importiert. Sollte es Darstellungsprobleme, falsche Links oder fehlende Bilder geben, bitte einfach hier einen Kommentar hinterlassen. Danke.
JSON is a commonly used format for data transmission, especially with Ajax calls. It's more structured and easier to process then XML, but could be just a wall of text depending on the creator. This oneliner reformats JSON to look pretty and human-readable.
I've to write a script which outputs a JSON in the same structure as an existing file, but I need to understand the current structure before starting with the script. The existing file is valid JSON but hard to read, because it has no indention or newlines. I can't paste it into an online JSON-beautifier, because it contains company secrets and passwords. But Perl's JSON module provides an easy way for reformating:
perl -MFile::Slurp -MJSON -le '$j = JSON->new; $j->pretty(1); print $j->encode($j->decode(read_file($ARGV[0])));' my_file.jsonPerl is being called and instructed to load two modules: File::Slurp for reading the input file and JSON for parsing and writing the JSON-Format. A new JSON object is created and configured to output pretty JSON code. Finally, the file is being read, decoded from JSON into a Perl datastructure and encoded as (pretty) JSON.
3 Kommentare. Schreib was dazu-
Aristotle Pagaltzis
29.06.2013 11:01
Antworten
-
domm
29.06.2013 17:53
Antworten
-
Sebastian
29.06.2013 21:45
Antworten
Uhm, there’s a json_pp script included with JSON::PP (previously was part of the JSON distro, now part of core), which does just that. (If you’re using JSON::XS, there is also json_xs.)
or use JSON::PP:
json_pp < my_file.json
json_pp can also convert from/to Perl data structures...
Thanks to both of you, I didn't know that commandline tool.