Skip to content
Walk through JSON with Go
Go
Branch: master
Clone or download
romanyx Merge pull request #3 from romanyx/feature/modules
Modules dependency management
Latest commit 89961b4 Aug 31, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Return error on walk Jul 2, 2018
.gitignore Modules dependency management Aug 31, 2018
.travis.yml Modules dependency management Aug 31, 2018
LICENSE Implementation ready Jun 29, 2018
README.md Return error on walk Jul 2, 2018
go.mod Modules dependency management Aug 31, 2018
jwalk.go Return error on walk Jul 2, 2018
jwalk_test.go Test fixes Jun 29, 2018
make_value.go Implementation ready Jun 29, 2018
value.go Implementation ready Jun 29, 2018

README.md

GoDoc Go Report Card Build Status

jwalk

Jwalk is built on top of easyjson/jlexer, and allows to easily unmarshal any JSON input with arbitrary key names by walking through it.

Usage

package main

import (
	"fmt"
	"log"

	"github.com/romanyx/jwalk"
)

const input = `{
	"key1": [{
		"id": 1
	}, {
		"id": 2
	}],
	"key2": [1,2],
	"key3": {
		"id": 1,
		"name": null
	}
}`

func main() {
	i, err := jwalk.Parse([]byte(input))
	if err != nil {
		log.Fatal(err)
	}

	switch v := i.(type) {
	case jwalk.ObjectWalker:
		v.Walk(func(key string, value interface{}) error {
			fmt.Println(key + ":")
			switch v := value.(type) {
			case jwalk.ObjectsWalker:
				v.Walk(func(obj jwalk.ObjectWalker) error {
					fmt.Println("\t-")
					obj.Walk(func(key string, value interface{}) error {
						if v, ok := value.(jwalk.Value); ok {
							fmt.Println("\t", key+":", v.Interface())
						}
						return nil
					})
					return nil
				})
			case jwalk.Value:
				fmt.Println("\t", v.Interface())
			case jwalk.ObjectWalker:
				v.Walk(func(key string, value interface{}) error {
					if v, ok := value.(jwalk.Value); ok {
						fmt.Println("\t", key+":", v.Interface())
					}
					return nil
				})
			}
			return nil
		})
	}
}

Testing

go test

Contributing

Please feel free to submit issues, fork the repository and send pull requests!

You can’t perform that action at this time.