alibaba/jsonq

Name: jsonq

Owner: Alibaba

Description: simple json field access for golang

Created: 2015-10-28 11:24:06.0

Updated: 2018-05-23 03:48:53.0

Pushed: 2015-09-29 21:24:41.0

Homepage: null

Size: 223

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

jsonq

Build Status Godoc license

Simplify your golang json usage by extracting fields or items from arrays and objects with a simple, hierarchical query. API Documentation on godoc.org.

This package is meant to make working with complex feeds a bit more easy. If you have simple feeds you want to model with struct types, check out jflect, which will create struct definitions given a json document.

installing

et github.com/jmoiron/jsonq

usage

Given some json data like:


"foo": 1,
"bar": 2,
"test": "Hello, world!",
"baz": 123.1,
"array": [
    {"foo": 1},
    {"bar": 2},
    {"baz": 3}
],
"subobj": {
    "foo": 1,
    "subarray": [1,2,3],
    "subsubobj": {
        "bar": 2,
        "baz": 3,
        "array": ["hello", "world"]
    }
},
"bool": true

Decode it into a map[string]interface{}:

rt (
"strings"
"encoding/json"
"github.com/jmoiron/jsonq"


 := map[string]interface{}{}
:= json.NewDecoder(strings.NewReader(jsonstring))
Decode(&data)
= jsonq.NewQuery(data)

From here, you can query along different keys and indexes:

ata["foo"] -> 1
nt("foo")

ata["subobj"]["subarray"][1] -> 2
nt("subobj", "subarray", "1")

ata["subobj"]["subarray"]["array"][0] -> "hello"
tring("subobj", "subsubobj", "array", "0")

ata["subobj"] -> map[string]interface{}{"subobj": ...}
 err := jq.Object("subobj")

Missing keys, out of bounds indexes, and type failures will return errors. For simplicity, integer keys (ie, {“0”: “zero”}) are inaccessible by jsonq as integer strings are assumed to be array indexes.

The Int and Float methods will attempt to parse numbers from string values to ease the use of many real world feeds which deliver numbers as strings.

Suggestions/comments please tweet @jmoiron


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.