Dora makes exploring JSON fast, painless, and elegant.
- dora's main focus was for learning and teaching the content through a medium blog post. In other words, dora is not currently a stable tool.
go get github.com/bradford-hamilton/dora/pkg/dora
var exampleJSON = `{ "string": "a neat string", "bool": true, "PI": 3.14159 }`
c, err := dora.NewFromString(exampleJSON)
if err != nil {
return err
}
str, err := c.GetString("$.string")
if err != nil {
return err
}
boolean, err := c.GetBool("$.bool")
if err != nil {
return err
}
float, err := c.GetFloat64("$.PI")
if err != nil {
return err
}
fmt.Println(str) // a neat string
fmt.Println(boolean) // true
fmt.Println(float) // 3.14159
-
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"
go test ./...
👤 Bradford Lamson-Scribner
- Website: bradfordhamilton.io
- Twitter: @lamsonscribner
- Github: @bradford-hamilton
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Give a ⭐️ if this project helped you!