Welcome to dora the JSON explorer 👋
Dora makes exploring JSON fast, painless, and elegant.
NOTE:
- dora is currently an early WIP and the main focus was for teaching the content through a medium blog post. In other words, dora is still a ways out from being a stable tool.
- Recently made an initial pre-release at
0.1.0. New github project tracks progress towards 0.2.0 - a mininum usable API. Releases from 0.1.0 up to but not including 0.2.0 will be marked as pre-releases.
Install
go get github.com/bradford-hamilton/dora/pkg/dora
Usage
var exampleJSON = `{ "string": "a neat string", "bool": true, "PI": 3.14159 }`
c, err := dora.NewFromString(exampleJSON)
if err != nil {
fmt.Printf("\nError creating client: %v\n", err)
}
str, err := c.GetString("$.string")
if err != nil {
fmt.Println(err)
}
boolean, err := c.GetBool("$.bool")
if err != nil {
fmt.Println(err)
}
float, err := c.GetFloat64("$.PI")
if err != nil {
fmt.Println(err)
}
fmt.Println(str) // a neat string
fmt.Println(boolean) // true
fmt.Println(float) // 3.14159
Query Syntax
-
All queries start with $.
-
Access objects with . only, no support for object access with bracket notation [].
- This is intentional, as you can interpolate at the call site, so there is no reason to offer two syntaxes that do the same thing.
-
Access arrays by index with bracket notation [].
-
New: Fetch by type to allow caller to ask for the proper Go type. For the time being, asking for objects or arrays in their entirety must be done through GetString which will return the chunk of JSON.
Current API:
GetString
GetFloat64
GetBool
-
Next feature will be approaching this either with some sort of serialization option maybe similar to stdlib or a simpler one with no options that returns a map or something? Will think about that some.
Example with a JSON object as root value:
JSON:
{
"name": "bradford",
"someArray": ["some", "values"]
"obj": {
"innerKey": {
"innerKey2": "innerValue",
"innerKey3": [{ "kindOfStuff": "neatStuff" }]
}
},
"someBool": true,
"PI": 3.14159
}
Query: Result:
$.name == "bradford"
$.someArray == "[\"array\", \"values\"]"
$.someArray[0] == "some"
$.someArray[1] == "values"
$.someArray[2] == error
$.obj.innerKey.innerKey2 == "innerValue"
$.obj.innerKey.innerKey3[0].kindOfStuff == "neatStuff"
$.someBool == true
$.PI == 3.14159
Example with a JSON array as root value:
JSON:
[
"some",
"values",
{
"objKey": "objValue",
"objKey2": [{ "catstack": "lampcat" }]
}
]
Query: Result:
$[0] == "some"
$[1] == "values"
$[2] == "{ \"objKey\": \"objValue\", \"objKey2\": [{ \"catstack\": \"lampcat\" }] }"
$[2].objKey == "objValue"
$[2].objKey2[0] == "{ \"catstack\": \"lampcat\" }"
$[2].objKey2[0].catstack == "lampcat"
Run tests
go test ./...
Author
👤 Bradford Lamson-Scribner
🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Show your support
Give a ⭐️ if this project helped you!