Git Product home page Git Product logo

gojq's Introduction

gojq

This is a module for easier manipulation of JSON data in Go. As a typed language, Go requires one to know the types of structures and fields in JSON data before deserializing them. This can be too cumbersome, for instance, if you need to access or change a deeply nested value in a JSON object unknown at compile time.

This package was inspired by the GoJSONQ which is way more capable when it comes to ways of accessing data, but it could not modify the underlying JSON object. So, the key feature of gojq is that it supports setting values at properties at any depth in the JSON.

Installation

go get -u github.com/pjovanovic05/gojq

Example use

src := `{
    "foo": {
        "bar": {
            "baz": [
                "seven",
                "of",
                "nine"
            ],
            "counter": 0
        }
    }
}`

jq := gojq.FromBytes([]byte(src))

// Get the value from a deeply nested property
var currentCount int
jq.Select("foo", "bar", "counter").As(&currentCount)
if jq.Err != nil {
	log.Fatal(jq.Err)
}
fmt.Println("The current count is:", currentCount)

// Set a value
currentCount++
jq.Select("foo", "bar", "counter").Set(currentCount)

// iterate array:
it := jq.Select("foo", "bar", "baz").Iterator()
for it.Next() {
	fmt.Println(it.Value())
}

gojq's People

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

marco709394

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.