Coder Social home page Coder Social logo

dora's Introduction

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 is currently a WIP and the 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!

dora's People

Contributors

bradford-hamilton avatar dviramontes avatar stuartleeks avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.