Go package to quickly query and manipulate interface data.

IJSON
PkgGoDev Build and Test Status Go report
Query Interface JSON and set or delete values easily

IJSON is a small but effective utility to deal with dynamic or unknown JSON structures in Go. It's a helpful wrapper for navigating hierarchies of map[string]interface{} OR []interface{}. It is the best solution for one time data access and manipulation.

Other libraries parse the whole json structure in their own format and again to interface if required, not suitable if you have interface{} as input and want the output in same format.

Note - This is not a json parser. It just plays with raw interface data.

Features

  • Very fast in accessing and manipulating top level values.
  • Avoids parsing whole JSON structure to intermediate format. Saves allocations.
  • Easy API to perform query, set or delete operations on raw interface data.
  • One line syntax to chain multiple operations together.

Known limitations

  • Not suitable if you want to perform multiple operations on same data.

Getting started

Installation

go get -u github.com/akshaybharambe14/ijson

Usage and Example

This package provides two types of functions. The functions suffixed with <action>P accept a path separated by ".".

Ex. "#0.friends.#~name"

package main

import (
	"fmt"

	"github.com/akshaybharambe14/ijson"
)

var dataBytes = []byte(`
[
	{
	  "index": 0,
	  "friends": [
		{
		  "id": 0,
		  "name": "Justine Bird"
		},
		{
		  "id": 0,
		  "name": "Justine Bird"
		},
		{
		  "id": 1,
		  "name": "Marianne Rutledge"
		}
	  ]
	}
]
`)

func main() {
	r := ijson.ParseByes(dataBytes).
		GetP("#0.friends.#~name"). // list the friend names for 0th record -
		// []interface {}{"Justine Bird", "Justine Bird", "Marianne Rutledge"}

		Del("#0"). // delete 0th record
		// []interface {}{"Marianne Rutledge", "Justine Bird"}

		Set("tom", "#") // append "tom" in the list
		// []interface {}{"Marianne Rutledge", "Justine Bird", "tom"}

	fmt.Printf("%#v\n", r.Value())

	// returns error if the data type differs than the type expected by query
	fmt.Println(r.Set(1, "name").Error())
}

Path syntax

IJSON follows a specific path syntax to access the data. The implementation sticks to the analogy that, user knows the path. So if caller wants to access an index, the underlying data must be an array otherwise, an error will be returned.

Use functions and methods suffixed by P to provide a "." separated path.

Get

{
	"index": 0,
	"name": { "first": "Tom", "last": "Anderson" },
	"friends": [
		{ "id": 1, "name": "Justine Bird" },
		{ "id": 2, "name": "Justine Rutledge" },
		{ "id": 3, "name": "Marianne Rutledge" }
	]
}

Summary of get operations on above data.

"name.last"    >> "Anderson"                          // GET "last" field from "name" object
"friends.#"    >> 3                                   // GET length of "friends" array
"friends.#~id" >> [ 1, 2, 3 ]                         // GET all values of "id" field from "friends" array
"friends.#0"   >> { "id": 1, "name": "Justine Bird" } // GET "0th" element from "friends" array

Set

Set overwrites the existing data. An error will be returned if the data does not match the query. If the data is nil, it will create the structure.

There is an alternative for datatype mismatch. Use SetF instead of Set function. It will forcefully replace the existing with provided.

Following path syntax sets "Anderson" as a value in empty structure.

"name.last"    >> { "name": { "last": "Anderson" } }  // Create an object and SET value of "last" field in "name" object
"#2"           >> ["", "", "Anderson"]                // Create an array and SET value at "2nd" index
"friends.#"    >> { "friends": [ "Anderson" ] }       // Create an object and APPEND to "friends" array

Delete

While deleting at an index, you have two options. By default, deletes does not preserve order. This helps to save unnecessary allocations as it just replaces the data at given index with last element. Refer following syntax for details.

{
	"index": 0,
	"friends": ["Justine Bird", "Justine Rutledge", "Marianne Rutledge"]
}

Summary of delete operations on above data.

"index"        >> { "friends": [ "Justine Bird", "Justine Rutledge", "Marianne Rutledge" ] } // DELETE "index" field
"friends.#"    >> { "index": 0, "friends": [ "Justine Bird", "Justine Rutledge" ] }          // DELETE last element from "friends" array
"friends.#0"   >> { "index": 0, "friends": [ "Marianne Rutledge", "Justine Rutledge" ] }     // DELETE "0th" element from "friends" array WITHOUT preserving order
"friends.#~0"  >> { "index": 0, "friends": [ "Justine Rutledge", "Marianne Rutledge" ] }     // DELETE "0th" element from "friends" array WITH preserving order

Operations chaining

You can chain multiple operations and check if it succeeds or fails.

    r := ijson.New(data).Get("#0", "friends", "#~name").Del("#0").Set(value, "#")
    if r.Error() != nil {
        ...
    }

    // access value
    _ = r.Value()

Parsing the json

This package uses standard library encoding/json as a json parser. We already have a very wide range of json parsers. I would recommend GJSON. It is probably the fastest, as far as I know.

See ijson.ParseBytes() and ijson.Parse() functions.

Please check following awesome projects, you might find a better match for you.

  1. GJSON, SJSON
  2. FASTJSON
  3. GABS

Contact

Akshay Bharambe @akshaybharambe1

License

IJSON source code is available under the MIT License.

Owner
Akshay Bharambe
Software Developer 👨‍💻️ by passion | Tech Geek | Open for contributions | ❤️ Go | On a mission to spread the knowledge ☑️ | Feel free to get in touch.
Akshay Bharambe
Similar Resources

Package command provide simple API to create modern command-line interface

Package command Package command provide simple API to create modern command-line interface, mainly for lightweight usage, inspired by cobra Usage pack

Jan 16, 2022

fofax is a fofa query tool written in go, positioned as a command-line tool and characterized by simplicity and speed.

fofax is a fofa query tool written in go, positioned as a command-line tool and characterized by simplicity and speed.

fofaX 0x00 Introduction fofax is a fofa query tool written in go, positioned as

Jan 8, 2023

A very simple note-taking CLI you can use from the terminal that uses a SQLite DB to persist, and query, notes.

Note Logger Summary A very simple note-taking CLI you can use from the terminal that uses a SQLite DB to persist, and query, notes. Building/Installin

Apr 14, 2022

💊 A git query language

Gitql Gitql is a Git query language. In a repository path... See more here Reading the code ⚠️ This project was created in 2014 as my first go project

Dec 29, 2022

Query git repositories with SQL. Generate reports, perform status checks, analyze codebases. 🔍 📊

Query git repositories with SQL. Generate reports, perform status checks, analyze codebases. 🔍 📊

askgit is a command-line tool for running SQL queries on git repositories. It's meant for ad-hoc querying of git repositories on disk through a common interface (SQL), as an alternative to patching together various shell commands.

Jan 3, 2023

Oslatlong - CLI to query OSM Nominatim API

oslatlong Quickly conceived app to query OSM's Nominatim geocoding API. How do I

Jul 21, 2022

A Go library and common interface for running local and remote commands

go-runcmd go-runcmd is a Go library and common interface for running local and remote commands providing the Runner interface which helps to abstract

Nov 25, 2021

An os/exec like interface for running a command in a container, and being able to easily interact with stdin, stdout, and other adjustments

dockerexec An "os/exec" like interface for running a command in a container, and being able to easily interact with stdin, stdout, and other adjustmen

Jul 14, 2022

A simple logging interface that supports cross-platform color and concurrency.

A simple logging interface that supports cross-platform color and concurrency.

WLog Package wlog creates simple to use UI structure. The UI is used to simply print to the screen. There a wrappers that will wrap each other to crea

Sep 26, 2022
Comments
  • Add: Base version

    Add: Base version

    1. Add: Path syntax to access nested interface data. See path.go for path syntax and explanation.
    2. Add: Query, Set, Delete operations on interface data.
    3. Add: Support for "." separated path to access values.
    4. Add: Chainable operations to provide flexibility to perform multiple operations in a single line. See ijson.go for details.
Pi-hole data right from your terminal. Live updating view, query history extraction and more!
Pi-hole data right from your terminal. Live updating view, query history extraction and more!

Pi-CLI Pi-CLI is a command line program used to view data from a Pi-Hole instance directly in your terminal.

Dec 12, 2022
F2 is a cross-platform command-line tool for batch renaming files and directories quickly and safely. Written in Go!
F2 is a cross-platform command-line tool for batch renaming files and directories quickly and safely. Written in Go!

F2 is a cross-platform command-line tool for batch renaming files and directories quickly and safely.

Dec 31, 2022
textnote is a command line tool for quickly creating and managing daily plain text notes.

textnote is a command line tool for quickly creating and managing daily plain text notes. It is designed for ease of use to encourage the practice of daily, organized note taking. textnote intentionally facilitates only the management (creation, opening, organizing, and consolidated archiving) of notes, following the philosophy that notes are best written in a text editor and not via a CLI.

Jan 2, 2023
📝 Take notes quickly and expeditiously from terminal
📝 Take notes quickly and expeditiously from terminal

Installation See the last release, where you can find binary files for your ecosystem Curl: curl -sfL https://raw.githubusercontent.com/anonistas/noty

Dec 29, 2022
gomerge is a tool to quickly bulk merge several pull requests from your terminal.
gomerge is a tool to quickly bulk merge several pull requests from your terminal.

Gomerge is a tool to quickly enable you to bulk merge Github pull requests from your terminal. The intention of this tool is to simplfy, and eventually automate the merging of github pull requests. This tool should be able to run on most systems.

Dec 28, 2022
sttr is command line software that allows you to quickly run various transformation operations on the string.
sttr is command line software that allows you to quickly run various transformation operations on the string.

sttr is command line software that allows you to quickly run various transformation operations on the string.

Sep 21, 2021
A simple CLI tool to use the _simulate API of elasticsearch to quickly test pipelines

elasticsearch-pipeline-tester A simple CLI tool to use the _simulate API of elasticsearch to quickly test pipelines usage: pipelinetester [<flags>] <p

Oct 19, 2021
A fork of k3sup that lets you quickly deploy RKE2 clusters via the CLI.

k2sup This is an awful hack of a fork of Alex Ellis' k3sup - a light-weight utility to get from zero to KUBECONFIG, originally with K3s but now with R

Dec 12, 2022
A command line tool for quickly converting Unix timestamps to human readable form.

stamp A command line tool to quickly format a Unix timestamp in a human-readable form. Installation Go is required to build this software. To just bui

Oct 30, 2021
Snippet CLI manger for quickly using code snippets without leaving the terminal
Snippet CLI manger for quickly using code snippets without leaving the terminal

SnipKit - Snippet CLI manager This repository is still work in progress! SnipKit aims to paste code snippets from your favorite snippet manager into y

Dec 27, 2022