A lightweight JSON parser written in Go that supports JSON formatting and selective querying using dot notation. This parser implements a custom lexer and parser to handle JSON data structures.
- Custom lexer and parser implementation
- JSON string formatting with customizable indentation
- Query JSON data using dot notation path selectors
- Support for all JSON data types:
- Objects
- Arrays
- Strings
- Numbers
- Booleans
- Null
git clone https://github.com/Prashant047/go-json.git
cd go-json
go build
The parser supports two main operations: format
and select
.
Format JSON with custom indentation:
# Format JSON from a file
go-json format -f input.json -t " "
# Format JSON from stdin
echo '{"name": "John", "age": 30}' | go-json format
Select specific values using dot notation:
# Query from a file
go-json select -q "user.address.street" -f input.json
# Query from stdin
echo '{"user": {"address": {"street": "123 Main St"}}}' | go-json select -q "user.address.street"
- Use dot notation to access object properties:
user.name
- Use bracket notation with indices to access array elements:
users.[0].name
- Combine both for complex queries:
users.[0].addresses.[1].street
Given this JSON:
{
"users": [
{
"name": "John",
"age": 30,
"addresses": [
{
"type": "home",
"street": "123 Main St"
},
{
"type": "work",
"street": "456 Market St"
}
]
}
]
}
Query examples:
# Get the first user's name
go-json select -q "users.[0].name" -f user.json
# Output: "John"
# Get the first user's work address
go-json select -q "users.[0].addresses.[1]" -f user.json
# Output: {
# "type": "work",
# "street": "456 Market St"
# }
The parser is implemented in several components:
lexer
: Tokenizes JSON input into a stream of tokensparser
: Constructs an AST (Abstract Syntax Tree) from tokensast
: Defines the node types for the syntax treeoperations
: Implements formatting and query operations