Skip to content

pa-extract-field

by rob

Data

Extract a field from JSON input using dot notation. Navigates JSON structure to locate and output the specified field path.

# pa-extract-field

Extract a field from JSON input using dot notation. Parses JSON and navigates to the specified field path, outputting primitives as strings or nested structures as JSON.

## Installation

This is a CmdForge tool. It should be available in your PATH after installation via CmdForge.

## Usage

The tool reads JSON from stdin and extracts the specified field:

```bash
# Extract a top-level field
echo '{"name":"Alice","age":30}' | pa-extract-field --field name
# Output: Alice

# Extract a nested field using dot notation
echo '{"user":{"profile":{"email":"[email protected]"}}}' | pa-extract-field --field user.profile.email
# Output: [email protected]

# Extract a nested object (outputs as JSON)
echo '{"user":{"name":"Alice","age":30}}' | pa-extract-field --field user
# Output: {"name":"Alice","age":30}

# Use default value when field not found
echo '{"name":"Alice"}' | pa-extract-field --field email --default "[email protected]"
# Output: [email protected]

# Extract from array elements
echo '{"users":[{"name":"Alice"},{"name":"Bob"}]}' | pa-extract-field --field users.0.name
# Output: Alice
```

## Arguments

| Flag | Default | Description |
|------|---------|-------------|
| `--field` | (required) | Field path to extract using dot notation (e.g., 'address.city') |
| `--default` | "" | Default value if field not found |

## How It Works

The tool executes a single-step pipeline:

1. **extract_field**: Parses the JSON input from stdin, navigates the object tree using the dot-notation path, and returns the value. Primitive values (strings, numbers, booleans) are output as strings. Nested objects or arrays are output as JSON. If the field doesn't exist, the default value is returned.

## Dependencies

None. This tool has no external dependencies beyond Python's standard library.

No reviews yet.

Issues

No issues reported for this tool.