JSON to YAML Converter
The json_xs
tool is a powerful command-line utility for
converting JSON data. It leverages C libraries for high performance,
making it an efficient choice for developers working with JSON in Perl
environments. This tool can handle various conversion tasks, from
pretty-printing and compacting JSON to transforming it into other
formats like YAML or Perl data structures.
JSON to YAML Conversion
Easily convert your JSON data into YAML format. This is particularly
useful for configuration files or when working with systems that
prefer YAML over JSON. The -t yaml
option ensures a clean
and accurate conversion.
# Convert a JSON string to a YAML format
json_xs -t yaml < input.json > output.yaml
JSON Formatting and Validation
json_xs
can also be used to format JSON for better
readability or to validate its structure. The tool ensures that your
JSON is well-formed and can output it unchanged if it's valid, or
report errors if it's not.
# Convert a JSON string to a pretty-printed JSON format
json_xs < input.json > pretty_output.json
# Validate JSON and print it unchanged if valid
json_xs -t null < input.json
Advanced JSON Transformations
Beyond basic formatting, json_xs
supports more advanced
operations. This includes converting JSON to a Perl data structure for
further processing or concatenating multiple JSON inputs into a single
array.
# Convert JSON to Perl data structure and pretty-print it
json_xs -t dumper < input.json
# Slurp up all input and concatenate them into a single array
json_xs -a < multi_json_input.json > array_output.json
Key Deduplication
When dealing with JSON data that might contain duplicate keys,
json_xs
provides an option to de-duplicate them, keeping
the last encountered value for each key. This ensures data integrity
and predictability.
# De-duplicate keys while keeping the last value for a key
json_xs -D < input.json > deduped_output.json
Usage Examples
Here are a few more examples demonstrating the versatility of
json_xs
:
# Convert a JSON string to a compact JSON format
json_xs -c < input.json > compact_output.json
# Convert JSON from STDIN to a pretty-printed JSON format
echo '{"key":"value"}' | json_xs > pretty_output.json