Git Product home page Git Product logo

jsonl's Introduction

JSON Lines (JSONL) golang library

This library provides you with a Reader and a Writer for the JSONL format. It automatically flushes underlying http.ResponseWriter if applicable.

No external dependencies are required for this to run.

JSON Lines is a convenient format for storing structured data that may be processed one record at a time. It works well with unix-style text processing tools and shell pipelines. It's a great format for log files. It's also a flexible format for passing messages between cooperating processes.

Source: jsonlines.org

Examples

Dropping errors to keep code example shorter. You should definitely check them!

Write JSONL (JSON Lines)

The library automatically does the json marshaling for you on Write(in interface{})

package main

import(
    "github.com/simonfrey/jsonl"
    "bytes"
)

func main(){
    buff := bytes.Buffer{}
    
    w := jsonl.NewWriter(&buff)
    w.Write("Hello")
    w.Write("World")
    w.Write(42)
   
	fmt.Println(buff.String())
	// Output: 
	//  "Hello"\n"World"\n42\n
}

Read JSONL (JSON Lines)

More interesting than writing into JSONL is reading it and working with it. This library provides you with two functions to do so.

Read Single Line and unmarshal into struct

If you exactly know what you expect you can directly read a single line from the data into a struct. The library does the json unmarshaling for you.

package main

import(
    "github.com/simonfrey/jsonl"
    "strings"
)

func main(){
    input := "\"Hello\"\n\"World\"\n42\n"
    
    r := jsonl.NewReader(strings.NewReader(input))
   
	outString := ""
	r.ReadSingleLine(&outString)
	
	fmt.Println(outString)
	// Output: 
	//  Hello
}

Read multiple entries with type detection

This is my favorite use case of JSONL. Reading different JSON types from one stream. This allows to build a form of streaming data interface from the server (which sends JSONL with above writer function)

As you see for stricter type safety I use a dedicated Type field in the structs, so we can do a string/byte comparison to figure out what type we are using. If you types do have unique fields you could also check for those.

In the following example you learn how you can read JSONL typesafe in golang.

package main

import(
    "github.com/simonfrey/jsonl"
    "strings"
)


type T1 struct {
    Type  string `json:"type"`
    Count int    `json:"am"`
}
type T2 struct {
    Type    string `json:"type"`
    Comment string `json:"am"`
}


func main(){
	input := "{\"type\":\"T1\",\"am\":2}\n{\"type\":\"T2\",\"am\":\"I am T2\"}\n{\"type\":\"T1\",\"am\":9999}\n"
    
    r := jsonl.NewReader(strings.NewReader(input))
    r.ReadLines(func(data []byte) error {
        switch {
        case bytes.Contains(data, []byte(`"T1"`)):
            // T1 struct type
            t := T1{}
            err := json.Unmarshal(data, &t)
            if err != nil {
            return fmt.Errorf("could not unmarshal into T1: %w", err)
            }
			fmt.Printf("%T: %d\n", t, t.Count)
        case bytes.Contains(data, []byte(`"T2"`)):
            // T2 struct type
            t := T2{}
            err := json.Unmarshal(data, &t)
            if err != nil {
            return fmt.Errorf("could not unmarshal into T2: %w", err)
            }
            fmt.Printf("%T: %s\n", t, t.Comment)
            output += fmt.Sprintf("%T:%s|", t, t.Comment)
        }
        return nil
    })
	
	// Output: 
	//  main.T1: 2
	//  main.T2: I am T2
	//  main.T1: 9999
}

License

MIT

jsonl's People

Contributors

simonfrey avatar atij avatar

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.