fx - Command-line JSON Processor
fx
is a powerful command-line tool designed for processing and transforming JSON data. It allows developers to interact with JSON directly from their terminal, making it an indispensable utility for scripting, data manipulation, and debugging.
Parse and Pretty-Print JSON
Easily parse JSON from files and display it in a human-readable, pretty-printed format. This is fundamental for inspecting JSON structures.
# Parse JSON from a file and pretty-print it
cat file.json | fx
Extract Specific JSON Fields
Navigate through your JSON data to extract specific fields or nested values using dot notation.
# Extract a specific field from JSON
cat file.json | fx .fieldName
# Extract nested fields from JSON
cat file.json | fx '.parent.child'
Transform and Filter JSON Data
Leverage JavaScript expressions to transform, filter, and map JSON objects and arrays. This enables complex data manipulation on the fly.
# Use fx to filter array elements
cat file.json | fx 'filter(e => e.key >= 10)'
# Transform JSON objects
cat file.json | fx 'map(e => ({ id: e.id, value: e.value }))'
# Use JavaScript expressions in fx
cat file.json | fx 'e => ({ ...e, newKey: e.oldKey * 2 })'
# Chain multiple operations to transform JSON data
cat file.json | fx .fieldName | fx 'sortBy(["key"])'
Convert JSON to Other Formats
Transform JSON data into other common formats, such as CSV, for easier integration with other tools and systems.
# Convert JSON array to a comma-separated list
cat file.json | fx 'join(",")'
# Convert JSON to CSV format
cat file.json | fx 'map(e => [e.id, e.value].join(","))' | fx 'join("\n")'
Handle Inline and Direct JSON Input
fx
can process JSON data piped directly from standard input or provided as inline strings.
# Handle JSON data passed directly
echo '{"key": "value"}' | fx .key
# Use fx with an inline JSON array
echo '[{"key": 1}, {"key": 2}]' | fx 'filter(e => e.key > 1)'
Utility Functions
Utilize built-in functions for common tasks like counting elements or joining array items.
# Count elements in a JSON array
cat file.json | fx 'length'