Convert Figma logo to code with AI

bitly logogo-simplejson

a Go package to interact with arbitrary JSON

3,753
497
3,753
33

Top Related Projects

14,102

Get JSON values quickly - JSON parser for Go

One of the fastest alternative JSON parser for Go that does not require schema

Fast JSON serializer for golang.

13,330

A high-performance 100% compatible drop-in replacement of "encoding/json"

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

3,445

For parsing, creating and editing unknown or dynamic JSON in Go

Quick Overview

go-simplejson is a Go package that provides a simple and flexible way to interact with JSON data. It allows for easy reading, manipulation, and creation of JSON structures without the need for predefined structs, making it particularly useful for working with dynamic or unknown JSON structures.

Pros

  • Simple and intuitive API for JSON manipulation
  • No need to define structs for JSON parsing
  • Supports chaining methods for convenient access to nested data
  • Lightweight and has no external dependencies

Cons

  • Less type-safe compared to using structs for known JSON structures
  • May be slower for large JSON datasets compared to struct-based parsing
  • Limited built-in functionality for complex JSON operations
  • Requires manual type assertions when retrieving values

Code Examples

  1. Parsing JSON and accessing nested values:
js, _ := simplejson.NewJson([]byte(`{
    "user": {
        "name": "John",
        "age": 30
    }
}`))
name, _ := js.Get("user").Get("name").String()
fmt.Println(name) // Output: John
  1. Creating and modifying JSON:
js := simplejson.New()
js.Set("name", "Alice")
js.Set("age", 25)
js.SetPath([]string{"address", "city"}, "New York")
jsonBytes, _ := js.MarshalJSON()
fmt.Println(string(jsonBytes))
// Output: {"address":{"city":"New York"},"age":25,"name":"Alice"}
  1. Working with JSON arrays:
js, _ := simplejson.NewJson([]byte(`{
    "fruits": ["apple", "banana", "cherry"]
}`))
fruits, _ := js.Get("fruits").Array()
for i, fruit := range fruits {
    fmt.Printf("%d: %s\n", i, fruit)
}
// Output:
// 0: apple
// 1: banana
// 2: cherry

Getting Started

To use go-simplejson in your Go project, follow these steps:

  1. Install the package:

    go get github.com/bitly/go-simplejson
    
  2. Import the package in your Go code:

    import "github.com/bitly/go-simplejson"
    
  3. Start using go-simplejson in your code:

    js, err := simplejson.NewJson([]byte(`{"key": "value"}`))
    if err != nil {
        // Handle error
    }
    value, _ := js.Get("key").String()
    fmt.Println(value) // Output: value
    

Competitor Comparisons

14,102

Get JSON values quickly - JSON parser for Go

Pros of gjson

  • Faster performance, especially for large JSON documents
  • No need to unmarshal the entire JSON; can directly access specific paths
  • Supports a powerful path syntax for querying JSON

Cons of gjson

  • Read-only access; doesn't support modifying JSON
  • Less flexible for complex JSON manipulation tasks
  • Steeper learning curve for its path syntax

Code Comparison

go-simplejson:

js, _ := simplejson.NewJson([]byte(`{"name":{"first":"John","last":"Doe"}}`))
firstName, _ := js.Get("name").Get("first").String()

gjson:

json := `{"name":{"first":"John","last":"Doe"}}`
firstName := gjson.Get(json, "name.first").String()

Summary

gjson excels in performance and direct path access, making it ideal for quickly retrieving specific values from large JSON documents. However, it's limited to read-only operations and may require more time to master its path syntax.

go-simplejson offers more flexibility for JSON manipulation and has a simpler API, but it may be slower for large documents and requires unmarshaling the entire JSON structure.

Choose gjson for fast, read-only access to specific JSON values, and go-simplejson for more complex JSON manipulation tasks or when you need to modify the JSON structure.

One of the fastest alternative JSON parser for Go that does not require schema

Pros of jsonparser

  • Higher performance and lower memory usage due to zero-allocation approach
  • Supports parsing without prior schema knowledge
  • Allows working directly with JSON payloads as []byte

Cons of jsonparser

  • Less intuitive API compared to go-simplejson
  • Limited support for modifying JSON structures
  • Requires more manual error handling

Code Comparison

jsonparser:

value, dataType, offset, err := jsonparser.Get(data, "user", "name")
if err != nil {
    // Handle error
}

go-simplejson:

js, err := simplejson.NewJson(data)
if err != nil {
    // Handle error
}
name, err := js.Get("user").Get("name").String()

Key Differences

  • jsonparser focuses on performance and efficiency, while go-simplejson prioritizes ease of use
  • go-simplejson provides a more flexible and chainable API for JSON manipulation
  • jsonparser is better suited for large-scale JSON processing tasks with known structures
  • go-simplejson is more convenient for quick prototyping and working with dynamic JSON structures

Use Cases

  • Choose jsonparser for high-performance JSON parsing in production environments
  • Opt for go-simplejson when rapid development and code readability are priorities
  • Consider jsonparser for projects with strict memory constraints
  • Use go-simplejson when working with frequently changing JSON structures or when extensive JSON manipulation is required

Fast JSON serializer for golang.

Pros of easyjson

  • Significantly faster performance due to code generation
  • Supports custom MarshalJSON/UnmarshalJSON methods
  • Provides a command-line tool for generating marshaler/unmarshaler code

Cons of easyjson

  • Requires code generation step, adding complexity to the build process
  • Less flexible for handling dynamic or unknown JSON structures
  • Steeper learning curve compared to simplejson's straightforward API

Code Comparison

easyjson:

//easyjson:json
type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

// Usage
person := &Person{Name: "John", Age: 30}
data, _ := person.MarshalJSON()

simplejson:

js, _ := simplejson.NewJson([]byte(`{"name":"John","age":30}`))
name, _ := js.Get("name").String()
age, _ := js.Get("age").Int()

Summary

easyjson offers superior performance through code generation but requires an additional build step. It's ideal for projects with known JSON structures and high-performance requirements. simplejson provides a more flexible and intuitive API for working with dynamic JSON data, making it suitable for rapid development and scenarios where JSON structures may vary. The choice between the two depends on the specific needs of your project, balancing performance against flexibility and ease of use.

13,330

A high-performance 100% compatible drop-in replacement of "encoding/json"

Pros of json-iterator/go

  • Significantly faster performance, especially for large JSON payloads
  • More memory-efficient, with lower allocation rates
  • Supports both encoding and decoding of JSON data

Cons of json-iterator/go

  • Less intuitive API for simple operations compared to go-simplejson
  • Requires more setup code for basic usage
  • May have a steeper learning curve for beginners

Code Comparison

go-simplejson:

js, _ := simplejson.NewJson([]byte(`{"name":"John","age":30}`))
name, _ := js.Get("name").String()
age, _ := js.Get("age").Int()

json-iterator/go:

var json = jsoniter.ConfigCompatibleWithStandardLibrary
var result struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}
json.Unmarshal([]byte(`{"name":"John","age":30}`), &result)

Summary

json-iterator/go offers superior performance and efficiency, making it ideal for high-performance applications dealing with large JSON datasets. However, go-simplejson provides a more straightforward API for simple JSON operations, which may be preferable for smaller projects or quick prototyping. The choice between the two depends on the specific requirements of your project, balancing ease of use with performance needs.

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

Pros of fastjson

  • Significantly faster JSON parsing and encoding
  • Lower memory usage and fewer allocations
  • Supports streaming JSON parsing for large files

Cons of fastjson

  • Less intuitive API compared to go-simplejson
  • Requires more manual type checking and conversion
  • Limited support for modifying parsed JSON structures

Code Comparison

go-simplejson:

js, _ := simplejson.NewJson([]byte(`{"name":"John","age":30}`))
name, _ := js.Get("name").String()
age, _ := js.Get("age").Int()

fastjson:

var p fastjson.Parser
v, _ := p.Parse(`{"name":"John","age":30}`)
name := string(v.GetStringBytes("name"))
age := v.GetInt("age")

Summary

fastjson offers superior performance and memory efficiency compared to go-simplejson, making it ideal for high-performance applications dealing with large JSON datasets. However, go-simplejson provides a more user-friendly API and easier JSON manipulation. The choice between the two depends on whether raw performance or ease of use is the primary concern for your project.

3,445

For parsing, creating and editing unknown or dynamic JSON in Go

Pros of gabs

  • More feature-rich with additional functionality like array manipulation and path-based operations
  • Generally faster performance, especially for large JSON structures
  • Supports both parsing and creation of JSON data

Cons of gabs

  • Slightly more complex API compared to go-simplejson's simplicity
  • Less widespread adoption and community support
  • May have a steeper learning curve for beginners

Code Comparison

gabs:

json, _ := gabs.ParseJSON([]byte(`{"name":{"first":"John","last":"Doe"}}`))
firstName := json.Path("name.first").Data().(string)
json.Set("Smith", "name", "last")

go-simplejson:

json, _ := simplejson.NewJson([]byte(`{"name":{"first":"John","last":"Doe"}}`))
firstName, _ := json.GetPath("name", "first").String()
json.SetPath([]string{"name", "last"}, "Smith")

Both libraries offer similar functionality for parsing and manipulating JSON data. gabs provides a more concise syntax for accessing nested values using dot notation, while go-simplejson uses a method chaining approach. gabs also offers built-in type assertion, whereas go-simplejson requires explicit type conversion.

Overall, gabs is more feature-rich and performant, while go-simplejson offers a simpler API that may be easier for beginners to grasp. The choice between the two depends on specific project requirements and developer preferences.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

go-simplejson

a Go package to interact with arbitrary JSON

Build Status GoDoc GitHub release

Importing

import github.com/bitly/go-simplejson

Documentation

Visit the docs on Go package discovery & docs