gojsonparser

package module
v0.0.0-...-3afb333 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2021 License: MIT Imports: 5 Imported by: 0

README

Go-JSON-Parser codecov

Go-JSON-Parser

Go-JSON-Parser is a small library that parses json strings into maps without having to use a struct with declared fields and tags.

package main

import (
    jsonparser "github.com/WolvenSpirit/go-json-parser"
)

func main() {
    str := `{
        "email":"me@example.com", "token":"xyz", "child":{
            "value": 3
        }
    }
    `
    // Parse 2 dimensional Json
    m, err := jsonparser.Parse2Dimensional(str)
    if err != nil {
        log.Println(err.Error())
    }
    fmt.Println(m["child"].Map["value"])
    // output: "3"

    // Parse one level at a time
    m, err := jsonparser.Parse(str)
    fmt.Println(m["child"])
    // output: "child":{
    //        "value": 3
    //      }

    // Parse the child json object later
    m, err := jsonparser.Parse(m["child"])
    fmt.Println(m["value"])
    // output: "3"
    
    // If you have more depth to your json just do subsequent parses if and when needed
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(r *bufio.Reader) (map[string]string, error)

Parse takes a *bufio.Reader containing a json string and parses it into a map[string]string.

Note: All values are returned as string, please use strconv to parse numeric values.

Parse does only a one dimension parse, if a key has an object then that object will be returned as a string you can additionally call Parse on that object string subsequently to get it in a map[string]string.

func Parse2Dimensional

func Parse2Dimensional(r *bufio.Reader) (map[string]Value, error)

Parse2Dimensional calls Parse also on first child objects.

Types

type Value

type Value struct {
	String string
	Map    map[string]string
}

Value ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL