Jshon is a lightweight and efficient command-line utility designed specifically for parsing and manipulating JSON data within shell scripts. It allows developers to easily extract values, modify existing data, and even construct new JSON objects directly from the terminal, making it an indispensable tool for automating tasks and integrating JSON processing into your workflows.
Jshon simplifies complex JSON operations with intuitive commands. Below are some common use cases:
To extract a specific value associated with a key from a JSON object, use the -e flag followed by the key name. For nested structures, chain multiple -e flags.
# Extract a value from a JSON object for a key
jshon -e "key" < input.json
# Extract a nested value from a JSON object
jshon -e "outerKey" -e "innerKey" < input.json
# Extract a value from JSON, specifying path with multiple keys
jshon -e "key1" -e "key2" < input.json
Jshon enables in-place modification of JSON values or deletion of key-value pairs.
# Modify a value in a JSON object
jshon -e "key" -s "newValue" < input.json
# Delete a key-value pair from a JSON object
jshon -n -e "keyToRemove" < input.json
Convert JSON arrays into shell-friendly formats, such as shell arrays, for easier iteration and processing.
# Convert a JSON array to a shell array
myArray=($(jshon -a -e "arrayKey" < input.json))
Jshon can also be used to format JSON for readability or to construct new JSON objects from simple inputs.
# Pretty-print JSON data
jshon -p < input.json
# Creating a new JSON object
echo '["key","value"]' | jshon -n
Transform JSON values into a format that is easily consumable by other shell commands.
# Convert JSON to shell-friendly output
jshon -e "key" -u < input.json