• Stars
    star
    586
  • Rank 76,279 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 12 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

simple json field access for golang

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

go get 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{}:

import (
	"strings"
	"encoding/json"
	"github.com/jmoiron/jsonq"
)

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

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

// data["foo"] -> 1
jq.Int("foo")

// data["subobj"]["subarray"][1] -> 2
jq.Int("subobj", "subarray", "1")

// data["subobj"]["subarray"]["array"][0] -> "hello"
jq.String("subobj", "subsubobj", "array", "0")

// data["subobj"] -> map[string]interface{}{"subobj": ...}
obj, 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

More Repositories

1

sqlx

general purpose extensions to golang's database/sql
Go
16,176
star
2

humanize

python humanize functions
Python
1,677
star
3

modl

golang database modelling library
Go
479
star
4

johnny-cache

johnny cache django caching framework
Python
305
star
5

monet

golang blog
Go
196
star
6

speedparser

feedparser but faster and worse
Python
101
star
7

gowiki

single-file single-executable wiki written in golang
Go
77
star
8

jigo

an attempt at a jinja2 implementation in go
Go
71
star
9

dmc

dmc runs commands via ssh on multiple machines
Go
34
star
10

django-slow-log

django slow request log
Python
32
star
11

micromongo

tiny fast python orm-ish tools for mongodb
Python
27
star
12

mandira

language agnostic logic-light template system
Go
27
star
13

arachne

a complex but scalable web spider
Python
25
star
14

aranha

simple python gevent web spider
Python
23
star
15

python-github

simple python github api2 library
Python
16
star
16

cm

very simple config manager
Go
15
star
17

par2ools

par2 tools (par2ls, par2mv, par2unrar)
Python
12
star
18

contact-form

server that implements an email contact form for an otherwise static website
Go
11
star
19

what-the-sql

online sql test
Go
11
star
20

euler.go

project euler solutions in go
Go
9
star
21

jmoiron.net

personal blog + website written with flask
Python
8
star
22

dotfiles

dotfiles + cm
Vim Script
8
star
23

jinkies

jinkies is a simple jenkins cli
Python
6
star
24

ongaku

http music player for local files streamable to a chromecast via chrome tab streaming
Go
6
star
25

gaspar

generic eventlet+zmq worker library
Python
5
star
26

kokuen

django-app performance tracking w/ statsd + graphite
Python
5
star
27

pdf2zip

converts image-based pdfs to a zipfile full of extracted images
Python
5
star
28

gevent-memcache-bench

python memcached speed tests w/ and w/o gevent
Python
4
star
29

etod

epoch to date
Go
3
star
30

redtape

simple program to create simple html docs from simple markdown files
Python
3
star
31

uromkan

python unicode romaji to kana conversion
Python
3
star
32

iris

python command-line photo management thing
Python
2
star
33

daneel

Python
2
star
34

chapman

chapman python backup nonsense
Python
2
star
35

nvidia-gpu-temp

Python
1
star
36

terminal-schemer

portable style scheme application for mate-terminal and gnome-terminal
Go
1
star
37

cs101

simplistic explorations of basic CS data structures & algorithms
Go
1
star
38

pctilde

pctilde emulates the behavior of the zsh PS1 var '%~'
Go
1
star
39

golang-sdk

Go
1
star
40

ulv-covers-modern

ulv covers for gregtech ceu modern
Java
1
star