A command-line tool written in Go that analyzes JSON data and generates a visual tree representation of its structure, including field types and optionality detection.
- Automatic Schema Detection: Analyzes JSON data to infer the structure and types of all fields
- Optional Field Detection: Identifies optional fields by analyzing presence across multiple objects
- Nested Structure Support: Handles deeply nested objects and arrays
- Multiple Input Sources: Supports reading from:
- Standard input (stdin)
- Local files
- HTTP/HTTPS URLs
- Tree Visualization: Displays the JSON structure as an easy-to-read tree with types and optional markers
- Array Merging: Intelligently merges schemas from arrays of objects
git clone https://github.com/TheBabaYaga/json-shape.git
cd json-shape
go build -o json-shapego install github.com/TheBabaYaga/json-shape@latestRead from a file:
json-shape path/to/file.jsonRead from stdin:
cat file.json | json-shape
# or
echo '{"name": "test", "value": 123}' | json-shapeRead from a URL:
json-shape https://api.example.com/data.jsonThe tool outputs a tree structure showing:
- Field names
- Field types (string, number, boolean, object, array, null, unknown)
- Optional fields marked with
(optional) - Nested structures with proper indentation
Example output:
root
├── name: string
├── age: number (optional)
├── address
│ ├── street: string
│ ├── city: string
│ └── postalCode: string (optional)
└── tags
├── id: number
└── name: string (optional)
- JSON Parsing: The tool parses JSON data into a generic Go interface structure
- Field Analysis: It recursively analyzes all fields, determining their types and tracking their presence
- Type Inference: Types are inferred from the actual values:
stringfor text valuesnumberfor numeric values (Go represents JSON numbers as float64)booleanfor true/false valuesobjectfor nested objectsarray<type>for arrays (e.g.,array<string>,array<number>)unknownfor fields where the type cannot be determined (e.g., fields that are alwaysnullin the input)
- Optionality Detection: A field is marked as optional if:
- It appears in fewer objects than the parent object count
- It has a null value at least once (even if it's the only value, it remains
unknown (optional))
- Schema Merging: When analyzing arrays of objects, the tool merges all object schemas to create a unified structure
Input (simple.json):
{
"name": "Alice",
"age": 30,
"email": "alice@example.com"
}Output:
root
├── age: number
├── email: string
└── name: string
Input (employees.json):
[
{"name": "Alice", "age": 30, "department": "Engineering"},
{"name": "Bob", "age": 25},
{"name": "Charlie", "age": 35, "department": "Sales", "manager": "Alice"}
]Output:
root
├── age: number
├── department: string (optional)
├── manager: string (optional)
└── name: string
Input:
{
"user": {
"id": 1,
"profile": {
"bio": "Developer",
"avatar": null
}
}
}Output:
root
└── user
├── id: number
└── profile
├── avatar: unknown (optional)
└── bio: string
Input:
{
"tags": [
{"id": 1, "name": "tag1"},
{"id": 2, "extra": true}
]
}Output:
root
└── tags
├── extra: boolean (optional)
├── id: number
└── name: string (optional)
Run the test suite:
go test -vThe test suite includes:
- Type detection tests
- Optionality detection tests
- Field merging tests
- Nested structure tests
- Integration tests
- Go 1.22 or later
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Kevin T'Syen
This tool is useful for:
- Understanding JSON API responses
- Documenting JSON data structures
- Debugging JSON parsing issues
- Generating schema documentation
- Analyzing large JSON datasets
- It does not validate JSON against a schema
- It does not generate JSON Schema or OpenAPI definitions
- It does not infer types beyond what appears in the input
- It does not guarantee correctness for unseen data
Note: JSON input is read fully into memory. Very large files or responses may require sufficient memory.