The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

 JSON::ize - Use JSON easily in one-liners - now with YAMLific action

SYNOPSIS

 $ perl -MJSON::ize -le '$j=jsonize("my.json"); print $j->{thingy};'

 $ perl -MJSON::ize -le '$j=jsonize("my.yaml"); print $j->{thingy};'

 # or yamlize, if you prefer

 $ perl -MJOSN::ize -le '$j=yamlize("my.yaml"); print $j->{thingy};'

 # plus yamls all the way down...

 # if you have PerlIO::gzip, this works

 $ perl -MJSON::ize -le '$j=jsonize("my.json.gz"); print $j->{thingy};'

 $ perl -MJSON::ize -le 'J("my.json"); print J->{thingy};' # short

 $ perl -MJSON::ize -le 'print J("my.json")->{thingy};' # shorter

 $ cat my.json | perl -MJSON::ize -lne 'parsej; END{ print J->{thingy}}' # another way

 $ perl -MJSON::ize -le '$j="{\"this\":\"also\",\"works\":[1,2,3]}"; print jsonize($j)->{"this"};' # also

 $ perl -MJSON::ize -e 'pretty_json(); $j=jsonize("ugly.json"); print jsonize($j);' # pretty!

 $ perl -MJSON::ize -e 'ugly_json; print J(J("indented.json"));' # strip whsp

 # JSON to YAML

 $ perl -MJSON::ize -e 'print yamlize jsonize "my.json"'

 $ perl -MJSON::ize -e 'print Y J "my.json"' 

 # and back 

 $ perl -MJSON::ize -e 'print jsonize yamlize "my.yaml"'

 $ perl -MJSON::ize -e 'print J Y "my.yaml"' 

DESCRIPTION

JSON::ize exports a function, jsonize(), and some synonyms (see below), that will do what you mean with the argument.

If argument is a filename, it will try to read the file and decode it from JSON or YAML.

If argument is a string that looks like JSON or YAML, it will try to encode it:

  • If argument is a Perl hashref or arrayref, and you called jsonize(), it will try to encode it as JSON.

  • If argument is a Perl hashref or arrayref, and you called yamlize(), it will try to encode it as YAML.

The underlying JSON object is

 $JSON::ize::JOBJ

METHODS

jsonize($j), jsonise($j), J($j)

Try to DWYM. In particular, encode to JSON. If called without argument, return the last value returned. Use this to retrieve after "parsej".

yamlize($j), yamlise($j), Y($j)

Try to DWYM. In particular, encode to YAML. If called without argument, return the last value returned.

parsej

Parse a piped-in stream of json. Use jsonize() (without arg) to retrieve the object. (Uses "incr_parse" in JSON.)

pretty_json()

Output pretty (indented) json.

ugly_json()

Output json with no extra whitespace.

SEE ALSO

JSON, JSON::XS.

AUTHOR

 Mark A. Jensen
 CPAN: MAJENSEN
 mark -dot- jensen -at- nih -dot- gov

LICENSE

Copyright (c) 2018, 2019 Mark A. Jensen.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.