Write and read JSON from different sources in one line

Related tags

JSON go golang json
Overview

Easy Json (EJ)

Go Report Card Build Status Coverage Status GoDoc Mentioned in Awesome Go

Package ej implements a JSON handler to write and read json succinctly from different sources like files and http requests.

Install

go get -u github.com/lucassscaravelli/ej

Examples

File

package main

type exData struct {
	Hello int
	World []string
}

func main() {

	dataWrite := &exData{
		Hello: 1,
		World: []string{"h", "e", "l", "l", "o"},
	}

	var dataRead exData
	
	// marshal the content of "dataWrite" to JSON and write in "ex.json" file
	if err := ej.JSON(from.File("ex.json")).Write(&dataWrite); err != nil {
		log.Fatal(err)
	}
	
	// read the data of "ex.json" file and unmarshal the JSON to "dataRead" content
	if err := ej.JSON(from.File("ex.json")).ParseToData(&dataRead); err != nil {
		log.Fatal(err)
	}
	
	// equal content
	fmt.Printf("dataWrite: %+v\n", dataWrite)
	fmt.Printf("dataRead: %+v\n", dataRead)
}

HTTP Request

package main

import (
	"log"
	"net/http"

	"github.com/lucassscaravelil/ej"
	"github.com/lucassscaravelil/ej/from"
)

type requestPayload struct {
	NumberToFind int
	Numbers      []int
}

type responseErrorPayload struct {
	StatusCode int
	ErrorTxt   string
}

type responsePayload struct {
	Found  bool
	Number int
}

func writeError(jsonHandler *ej.EJ, status int, err error) {
	jsonHandler.Write(&responseErrorPayload{
		StatusCode: status,
		ErrorTxt:   err.Error(),
	})
}

func main() {

	http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {

		var bodyData requestPayload

		jsonHandler := ej.JSON(from.Request(w, r))

		if err := jsonHandler.ParseToData(&bodyData); err != nil {
			writeError(jsonHandler, http.StatusBadRequest, err)
			return
		}

		found := false
		foundNumber := 0

		for _, number := range bodyData.Numbers {
			if number == bodyData.NumberToFind {
				found = true
				foundNumber = number
				break
			}
		}

		jsonHandler.Write(&responsePayload{
			Found:  found,
			Number: foundNumber,
		})

		return
	})

	log.Fatal(http.ListenAndServe(":8080", nil))
}

HTTP Response

package main

import (
	"log"
	"net/http"

	"github.com/lucassscaravelil/ej"
	"github.com/lucassscaravelil/ej/from"
)

type testData struct {
	Count int
	Txt   string
}

func main() {
	var responseData testData
	response, err := http.Get("http://<url>/any")
	if err != nil {
		log.Fatal(err)
	}

	if err := ej.JSON(Response(response)).ParseToData(&responseData); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("dataRead: %+v\n", responseData)
}
Issues
Releases(v1.1.1)
Owner
Lucas Scaravelli
Lucas Scaravelli
One of the fastest alternative JSON parser for Go that does not require schema

Alternative JSON parser for Go (10x times faster standard library) It does not require you to know the structure of the payload (eg. create structs),

Leonid Bugaev 4.4k Jan 18, 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
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
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
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
/ˈdʏf/ - diff tool for YAML files, and sometimes JSON

dyff is inspired by the way the old BOSH v1 deployment output reported changes from one version to another by only showing the parts of a YAML file that change.

null 518 Jan 20, 2022
HuJSON: JSON for Humans (comments and trailing commas)

HuJSON - Human JSON The HuJSON decoder is a JSON decoder that also allows comments, both /* ... */ and // to end of line trailing commas on arrays and

Tailscale 334 Jan 1, 2022
tson is JSON viewer and editor written in Go

tson tson is JSON viewer and editor written in Go. This tool displays JSON as a tree and you can search and edit key or values. Support OS Mac Linux I

skanehira 125 Dec 14, 2021
JSONata in Go Package jsonata is a query and transformation language for JSON

JSONata in Go Package jsonata is a query and transformation language for JSON. It's a Go port of the JavaScript library JSONata.

Blues Inc 19 Jan 12, 2022
Slow and unreliable JSON parser generator (in progress)

VivaceJSON Fast and reliable JSON parser generator Todo List parse fields parse types generate struct generate (keypath+key) to struct Value Mapping F

null 7 Jan 6, 2022
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
Golang JSON decoder supporting case-sensitive, number-preserving, and strict decoding use cases

Golang JSON decoder supporting case-sensitive, number-preserving, and strict decoding use cases

Kubernetes SIGs 8 Dec 8, 2021
Easy JSON parsing, stringifying, and accesing

Easy JSON parsing, stringifying, and accesing

Neutron 1 Nov 23, 2021
json encoding and decoding

jx Package jx implements encoding and decoding of json [RFC 7159]. Lightweight fork of jsoniter. go get github.com/go-faster/jx Usage and examples Roa

go faster 33 Jan 11, 2022
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
Search and output the value of JSON by it's path.

Search and output the value of JSON by it's path.

Alexander Dawson 0 Dec 19, 2021
A simple GO module providing CRUD and match methods on a User "entity" stored locally as JSON

A simple GO module providing CRUD and match methods on a User "entity" stored locally as JSON. Created for GO language learning purposes. Once finishe

Lorenzo Piersante 0 Jan 1, 2022