Skip to content

bradford-hamilton/dora

Repository files navigation

Dora backpack with JSON

Welcome to dora the JSON explorer 👋

Using go version 1.14 Using go version 1.14 Go Report Card godoc License: MIT

Dora makes exploring JSON fast, painless, and elegant.

NOTE:

  • dora's main focus was for teaching the content through a medium blog post. In other words, dora is not currently a stable tool.

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 {
  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

Query Syntax

  1. All queries start with $.

  2. 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.
  3. Access arrays by index with bracket notation [].

  4. 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
  5. 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!