Read and write multiline JSONL in Go

This package was build for programmatic access of multiline JSON in Go.
If you need CLI for JSON, I highly recommend jq.

$ go install github.com/nikolaydubina/multiline-jsonl@latest

For example, you want to parse input multiline JSONs

$ echo '{
    "from":"github.com/nikolaydubina/jsonl-graph/graph",
    "to":"bufio"
}

{"from":"my-id-1","to":"my-id-2"}
{"from":"my-id-5","to":"my-id-10", "amount": 123}
{"from":"my-id-5","to":"my-id-10", "amount": {"amount": 123, "currency": "KRW"}}

{
    "id": "my-id",
    "number": 123,
    "nested": {
        "title": "big title",
        "nested-level-2": {
            "subtitle": "some other thing",
            "count": 123
        }
    }
}

' | ./multiline-jsonl

Outputs shortened version

{"from":"github.com/nikolaydubina/jsonl-graph/graph","to":"bufio"}
{"from":"my-id-1","to":"my-id-2"}
{"amount":123,"from":"my-id-5","to":"my-id-10"}
{"amount":{"amount":123,"currency":"KRW"},"from":"my-id-5","to":"my-id-10"}
{"id":"my-id","nested":{"nested-level-2":{"count":123,"subtitle":"some other thing"},"title":"big title"},"number":123}

And with -expand flag

{
    "from": "github.com/nikolaydubina/jsonl-graph/graph",
    "to": "bufio"
}
{
    "from": "my-id-1",
    "to": "my-id-2"
}
{
    "amount": 123,
    "from": "my-id-5",
    "to": "my-id-10"
}
{
    "amount": {
        "amount": 123,
        "currency": "KRW"
    },
    "from": "my-id-5",
    "to": "my-id-10"
}
{
    "id": "my-id",
    "nested": {
        "nested-level-2": {
            "count": 123,
            "subtitle": "some other thing"
        },
        "title": "big title"
    },
    "number": 123
}

Here is example from https://github.com/nikolaydubina/jsonl-graph

func NewGraphFromJSONL(r io.Reader) (Graph, error) {
	g := NewGraph()

	scanner := bufio.NewScanner(r)
	scanner.Split(multilinejsonl.SplitMultilineJSONL)

	for scanner.Scan() {
		decoder := json.NewDecoder(bytes.NewReader(scanner.Bytes()))
		decoder.UseNumber()

		var nodeEdge orNodeDataEdgeData
		if err := decoder.Decode(&nodeEdge); err != nil {
			continue
		}

		node, edge, err := nodeEdge.cast()
		if err != nil {
			return g, fmt.Errorf("can not cast: %w", err)
		}

		switch {
		case node != nil:
			g.AddNode(*node)
		case edge != nil:
			g.AddEdge(*edge)
		}
	}

	return g, scanner.Err()
}

Features

  • No reflection
  • Simple Code
  • CLI
  • 84% coverage

Reference:

Similar Resources

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

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

Dec 9, 2022

Easy JSON parsing, stringifying, and accesing

Easy JSON parsing, stringifying, and accesing

Nov 23, 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

Sep 15, 2022

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

Dec 27, 2022

Go encoder and decoder for the NetBPM/PNM image formats. Compatible with Go's image packages.

gpnm This package implements an encoder and decoder for PNM image formats. It can be used with Go's image library. It covers all formats as defined by

Nov 26, 2021

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

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

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

Feb 5, 2022

Json-go - CLI to convert JSON to go and vice versa

Json-go - CLI to convert JSON to go and vice versa

Json To Go Struct CLI Install Go version 1.17 go install github.com/samit22/js

Jul 29, 2022
Related tags
pipe JSONL data to SIEM

SIEMSend UNIX philosophy inspired SIEM connector. The binary work is very simple: get a stream of JSON lines from stdin, send them in batches to a SIE

Nov 9, 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

Jan 5, 2023
Fast and flexible JSON encoder for Go
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

Dec 21, 2022
/ˈdʏf/ - diff tool for YAML files, and sometimes JSON
/ˈ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.

Dec 29, 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

Dec 22, 2022
tson is JSON viewer and editor written in Go
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

Nov 2, 2022
A Small tool for SDWAN performance test and policy validation

sdwan-perf Sdwan-perf is based on golang and could support almost platform for performance and policy validation. SDWAN Performance Test Report +--

Sep 3, 2022
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.

Nov 8, 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

Nov 26, 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

Jun 26, 2022