Jsonic is the complete set of utilities to handle json data

Related tags

JSON jsonic
Overview

JSONIC

GoDoc Release Report Coverage Status Mentioned in Awesome Go

Jsonic is the complete set of utilities to handle json data. There's no need to define structs anymore. It's completely safe to perform nested queries in the JSON. The strong typed methods part of this library will help you have the desired result without writing any extra code.

Installation

go get github.com/sinhashubham95/jsonic

Understanding the query path

Jsonic uses a unique and simple way to query the elements in a json. It's easy but unique, so you need to understand the same for using Jsonic.

Consider the following json.

{
  "a": {
    "x": "p",
    "arr": [
      {
        "a": "b",
        "c.d": {
          "e": "f"
        }
      }
    ]
  },
  "c": "d",
  "a.x": {
    "y": "q"
  },
  "a.x.y": {
    "z": "r"
  }
}

Though, practically such a JSON won't exist, but still Jsonic is intelligent enough to handle even this. Go through the below table carefully and it will help you understand the path schema.

Path Result Comments
{EMPTY_STRING} entire json empty string returns the entire json if no empty string exists in the key
. entire json dot returns the entire json if no dot exists in the key
a {"x": "p", "arr": [{ "a": "b", "c.d": { "e": "f" } }]} it returns the entire json tree of a
a.x p multiple options here, but the first preference goes to the tree of a
a.x.y q multiple options here, the first preference will be given to a.x
a.x.y.z r there is only a single possibility here
a.arr [{ "a": "b", "c.d": { "e": "f" } }] it returns the entire array denoting the json tree of a.arr
a.arr[0] { "a": "b", "c.d": { "e": "f" } } it returns the first element of the array
a.arr[0].a b it returns the element for key a of the first element of array
a.arr[0].c.d.e f

As you would have understood, if there are multiple JSON trees satisfying the path, and the path looks something like this a.b.c.d, then the preferences will be in the following order - a > a.b > a.b.c > a.b.c.d.

Consider another json.

{
  "": "a",
  ".": "b"
}

Here the paths resolve in a different manner.

Path Result Comments
{EMPTY_STRING} a empty string returns the entire json if no empty string exists in the key
. b empty string returns the entire json if no empty string exists in the key

How to Use?

Jsonic allows you to process the JSON bytes. You can create a new instance of Jsonic for every JSON you have and you can get the child JSON trees using the set of utilities it provides.

Create a New Instance

This will create a new instance using the JSON bytes provided as it's data to be used on.

import (
  "github.com/sinhashubham95/jsonic"
)

func New() {
  json := "{\"naruto\": \"rocks\"}"
  j, err := jsonic.New([]byte(json))
  // perform any sort of operations on the json using the instance created
}

Create a child instance

On the Jsonic created, you can provide a child path and get a new instance with the child JSON tree satisfying the path provided as it's data.

import (
  "github.com/sinhashubham95/jsonic"
)

func Child() {
  json := "{\"naruto\": \"rocks\"}"
  j, err := jsonic.New([]byte(json))
  if err != nil {
    return
  }
  
  // create a child
  child, err := jsonic.Child("naruto")
  // now if you want to query on the child then use this child instance
}

Get the data at the path

On the Jsonic created, you can get the data at the path specified.

import (
  "github.com/sinhashubham95/jsonic"
)

func Get() {
  json := "{\"naruto\": \"rocks\"}"
  j, err := jsonic.New([]byte(json))
  if err != nil {
    return
  }
  
  // get the data
  data, err := jsonic.Get("naruto")
  // this data will have type interface{} with value "rocks"
}

Get typed data at the path

Though using structs is not required with the wonderful set of utilities Jsonic provides, but even if you like to use that, it is very simple to get your result cast into the struct you want.

import (
  "github.com/sinhashubham95/jsonic"
)

type Detail struct {
  Name string `json:"name"`
}

func GetTyped() {
  json := "{\"characters\": [{\"name\": \"naruto\"}, {\"name\": \"boruto\"}]}"
  j, err := jsonic.New([]byte(json))
  if err != nil {
    return
  }
  
  // get the data
  var data []Detail
  err := jsonic.GetTyped("characters", &data)
  // this data will contain 2 elements with names as naruto and boruto
}

Other Typed Utilities

Apart from the generic query methods mentioned above, Jsonic contains a bunch of others.

import (
  "github.com/sinhashubham95/jsonic"
)

func OtherGetters(j *Jsonic, path string) {
  // primitives
  i, err := j.GetInt(path)                    // int
  i64, err := j.GetInt64(path)                // int64
  f, err := j.GetFloat(path)                  // float32
  f64, err := j.GetFloat64(path)              // float64
  b, err := j.GetBool(path)                   // bool
  s, err := j.GetString(path)                 // string

  // arrays
  a, err := j.GetArray(path)                  // []interface{}
  iArr, err := j.GetIntArray(path)            // []int
  i64Arr, err := j.GetInt64Array(path)        // []int64
  fArr, err := j.GetFloatArray(path)          // []float32
  f64Arr, err := j.GetFloat64Array(path)      // []float64
  bArr, err := j.GetBoolArray(path)           // []bool
  sArr, err := j.GetStringArray(path)         // []string

  // maps
  m, err := j.GetMap(path)                    // map[string]interface{}
  iMap, err := j.GetIntMap(path)              // map[string]int
  i64Map, err := j.GetInt64Map(path)          // map[string]int64
  fMap, err := j.GetFloatMap(path)            // map[string]float32
  f64Map, err := j.GetFloat64Map(path)        // map[string]float64
  bMap, err := j.GetBoolMap(path)             // map[string]bool
  sMap, err := j.GetStringMap(path)           // map[string]string
}
Issues
Releases(v1.1.0)
Owner
Shubham Sinha
Shubham Sinha
An example of how to parse json data using go....a typical step for preparing data prior to uploading to a db.

JSON parser using GO An example of parsing json data in go, when you already know the schema of the data Example input: { "num_listings":"36",

null 0 Jan 12, 2022
Get JSON values quickly - JSON parser for Go

get json values quickly GJSON is a Go package that provides a fast and simple way to get values from a json document. It has features such as one line

Josh Baker 9.6k Jan 15, 2022
JSON diff library for Go based on RFC6902 (JSON Patch)

jsondiff jsondiff is a Go package for computing the diff between two JSON documents as a series of RFC6902 (JSON Patch) operations, which is particula

William Poussier 140 Jan 13, 2022
Fast JSON encoder/decoder compatible with encoding/json for Go

Fast JSON encoder/decoder compatible with encoding/json for Go

Masaaki Goshima 1.3k Jan 15, 2022
Package json implements encoding and decoding of JSON as defined in RFC 7159

Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions

High Performance, Kubernetes Native Object Storage 2 Aug 9, 2021
A simple Go package to Query over JSON/YAML/XML/CSV Data

A simple Go package to Query over JSON Data. It provides simple, elegant and fast ODM like API to access, query JSON document Installation Install the

Saddam H 1.8k Jan 13, 2022
library for working amorphous data (as when you decode json into an interface{})

Introduction Decoding json into an interface{} produces an hierarchical arrangement of four data types: float64, string are 'primative types' and form

Chuck Luciano 9 Jul 5, 2021
Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang

kazaam Description Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang. This functionality provides

Qntfy 205 Sep 17, 2021
A tool to aggregate and mine data from JSON reports of Go tests.

teststat A tool to aggregate and mine data from JSON reports of Go tests. Why? Mature Go projects often have a lot of tests, and not all of them are i

Viacheslav Poturaev 3 Jan 3, 2022
Jsonmask use for mask sensitive data from json format

Jsonmask use for mask sensitive data from json format Installation go get -u github.com/rkritchat/jsonmask Code example package main import ( "fmt"

rkritchat 1 Nov 25, 2021
Senml-go - a Golang module for the JSON-based SenML sensor data format

ThingWave SenML module for Golang This is a Golang module for the JSON-based Sen

ThingWave 0 Jan 2, 2022
Abstract JSON for golang with JSONPath support

Abstract JSON Abstract JSON is a small golang package provides a parser for JSON with support of JSONPath, in case when you are not sure in its struct

Stepan Pyzhov 92 Jan 12, 2022
Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

fastjson - fast JSON parser and validator for Go Features Fast. As usual, up to 15x faster than the standard encoding/json. See benchmarks. Parses arb

Aliaksandr Valialkin 1.4k Jan 14, 2022
Small utility to create JSON objects

gjo Small utility to create JSON objects. This was inspired by jpmens/jo. Support OS Mac Linux Windows Requirements Go 1.1.14~ Git Installtion Build $

skanehira 99 Jan 23, 2022
A Go package for handling common HTTP JSON responses.

go-respond A Go package for handling common HTTP JSON responses. Installation go get github.com/nicklaw5/go-respond Usage The goal of go-respond is to

Nick Law 44 Jan 21, 2022
JSON query in Golang

gojq JSON query in Golang. Install go get -u github.com/elgs/gojq This library serves three purposes: makes parsing JSON configuration file much easie

Qian Chen 178 Dec 30, 2021
Automatically generate Go (golang) struct definitions from example JSON

gojson gojson generates go struct definitions from json or yaml documents. Example $ curl -s https://api.github.com/repos/chimeracoder/gojson | gojson

Aditya Mukerjee 2.5k Jan 17, 2022
A JSON diff utility

JayDiff A JSON diff utility. Install Downloading the compiled binary Download the latest version of the binary: releases extract the archive and place

Guillaume de Sagazan 83 Nov 22, 2021
Fast and flexible JSON encoder for Go

Jettison Jettison is a fast and flexible JSON encoder for the Go programming language, inspired by bet365/jingo, with a richer features set, aiming at

William Poussier 114 Dec 28, 2021