jsonextract

package module
v3.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2021 License: MIT Imports: 12 Imported by: 1

README

JSONExtract

Go Reference

Package jsonextract is a library for extracting JSON from a given source, such as string, bytes, file, and io.Reader, providing methods for editing and evaluating json values. One of the cases to use this library is to extract all json strings in a scraped HTML page

Installation

JSONExtract require Go v1.14 or higher

$ GO111MODULE go get github.com/tamboto2000/jsonextract/v3
Sources

You can extract jsons from string, []byte, file, and an io.Reader

// from string
jsons, err := jsonextract.FromString(str)

// from []byte
jsons, err := jsonextract.FromBytes(byts)

// from file path
jsons, err := jsonextract.FromFile(path)

// from io.Reader
jsons, err := jsonextract.FromReader(reader)
JSON Kinds

There's 7 different kinds of JSON:

  • Object
  • Array
  • String
  • Integer
  • Float
  • Boolean
  • Null

You can get JSON kind by calling JSON.Kind()

jsons, err := jsonextract.FromBytes(byts)
if err != nil {
    panic(err.Error())
}

for _, json := range jsons {
    // string
    if json.Kind() == jsonextract.String {
        // print string value
        fmt.Println(json.String())
    }
    
    // int
    if json.Kind() == jsonextract.Integer {
        // print integer value, returned integer value is int64
        fmt.Println(json.Integer())
    }
    
    // float
    if json.Kind() == jsonextract.Float {
        // print float value, returned float is float64
        fmt.Println(json.Float())
    }
    
    // and so on...
}

Getter methods, such as JSON.String(), JSON.Integer(), JSON.Float(), etc., will panic if JSON kind did not match the getter methods, for example, when trying to get string value from JSON with kind of Integer. JSON with kind of Null didn't have getter method

Modifying Value

Value inside JSON can be modified by setter methods, such as JSON.SetStr(), JSON.SetInt(), etc.

// integer
if json.Kind() == jsonextract.Integer {
    json.EditInt(23)
}

// string
if json.Kind() == jsonextract.String {
    json.EditStr("Hello World!")
}

// and so on...

Setter methods will panic if JSON kind did not match with setter method, for example, trying to call JSON.SetInt() to JSON with kind of String. JSON with kind of Null didn't have setter method

JSON Object

Json object represented as JSON with kind of Object. For adding a new value, call JSON.AddField() with param key as json field name, and val for the value

json.AddField("name", "Franklin Collin Tamboto")

json.AddField("email", "tamboto2000@gmail.com")

json.AddField("id", 1)

i := 1
json.AddField("id", &i)


json.AddField("qty", int32(23))


json.AddField("userId", uint(2))


json.AddField("height", float32(3.2))

Just like package encoding/json, map and struct will be marshaled as json object. map keys need to be type string or an integer type, otherwise panic will occur

type User struct {
    ID int `json:"id"`
    Name string `json:"name"`
    // field name will be used as json field name if json tag is not present, 
    // so this field will be marshaled as "Email":"value"
    Email string
}

// add struct
json.AddField("user", User{
    ID: 1,
    Name: "Franklin Collin Tamboto",
    Email: "tamboto2000@gmail.com",
})

// add map
json.AddField("item", map[interface{}]interface{}{
    "Name": "ROG Zephyrus M",
    "Qty": 23,
    123: 456,
})

// add value with int as key
json.AddField(123, 456)

// add value with uint8 as key
json.AddField(uint8(432), "123")

To delete an item from object, call JSON.DeleteField() with param key as json field name for deletion. key must be string or an integer type, otherwise panic will occur. JSON.DeleteField() return true if item is exist, otherwise false will returned

// delete with string key
if json.DeleteField("user") {
    fmt.Println("item deleted")
} else {
    fmt.Println("item not exist")
}

// delete with integer key
if json.DeleteField(int32(123)) {
    fmt.Println("item deleted")
} else {
    fmt.Println("item not exist")
}

To access all contained items, call JSON.Object(), this method will return map[interface{}]*JSON

fields := json.Object()
for _, f := range fields  {
    // do something...
}

Get contained items count by calling JSON.Len()

fmt.Println("len:", json.Len())
JSON Array

Json array represented as JSON with kind of Array. For adding a new value, call JSON.AddItem() with param val. Will panic if val is not valid json value

json.AddItem("Hello world")

Get contained items count by calling JSON.Len()

fmt.Println("len:", json.Len())

To delete an item, call json.DeleteItem() with param i as array index

if json.DeleteItem(2) {
    fmt.Println("item deleted")
} else {
    fmt.Println("item is not exist")
}

Get all items by calling JSON.Array()

items := json.Array()
for _, item := range items {
    // do something
}
Additional Note

When an item is edited inside JSON, the contained raw json will automatically re-generated. DO NOT PERFORM ANY SETTER OR GETTER METHODS CONCURRENTLY

Documentation and Examples

See Documentation for more information. There's also some examples you can look up to

License

MIT

Documentation

Overview

Package jsonextract is a library for extracting any valid JSONs from given source

Index

Constants

View Source
const (
	Object = iota
	Array
	String
	Integer
	Float
	Boolean
	Null
)

JSON kinds

Variables

This section is empty.

Functions

func Save

func Save(data []*JSON) error

Save save extracted JSONs to ./extracted_jsons.json

func SaveToPath

func SaveToPath(data []*JSON, path string) error

SaveToPath save extracted JSONs to a file path

Types

type JSON

type JSON struct {
	// contains filtered or unexported fields
}

JSON represent json val

func FromBytes

func FromBytes(byts []byte) ([]*JSON, error)

FromBytes extract JSONs from bytes

func FromFile

func FromFile(path string) ([]*JSON, error)

FromFile extract JSONs from file in path

func FromReader

func FromReader(reader io.Reader) ([]*JSON, error)

FromReader extract JSONs from reader io.Reader

func FromString

func FromString(str string) ([]*JSON, error)

FromString extract JSONs from string

func (*JSON) AddField

func (json *JSON) AddField(key interface{}, val interface{})

AddField add new field to object. Will panic if kind is not Object. Will panic if val is invalid json value. Valid key is string or integer type. If val is map, and key is not string nor an integer type, panic will occur

func (*JSON) AddItem

func (json *JSON) AddItem(val interface{})

AddItem add new item in json array. Will panic if kind is not Array. Will panic if val is invalid json value

func (*JSON) Array

func (json *JSON) Array() []*JSON

Array return array of JSON. Will panic if kind is not Array

func (*JSON) Boolean

func (json *JSON) Boolean() bool

Boolean return bool value. Will panic if kind is not Boolean

func (*JSON) Bytes

func (json *JSON) Bytes() []byte

Bytes return raw bytes of extracted json

func (*JSON) DeleteField

func (json *JSON) DeleteField(key interface{}) bool

DeleteField delete object field by key. Will panic if kind is not Object. key must be string or an integer type, otherwise panic will occur

func (*JSON) DeleteItem

func (json *JSON) DeleteItem(i int) bool

DeleteItem delete element on index i. Will panic if kind is not Array. Will panic if i bigger than items count minus 1 (len(vals)-1, the last index). If item is existed, true is returned, otherwise false

func (*JSON) Float

func (json *JSON) Float() float64

Float return float value. Will panic if kind is not Float

func (*JSON) Integer

func (json *JSON) Integer() int64

Integer return int value. Will panic if kind is not Integer

func (*JSON) Kind

func (json *JSON) Kind() int

Kind return json kind

func (*JSON) Len

func (json *JSON) Len() int

Len return items count inside JSON. If JSON is not Array or Object, Len will return 0, so please check the JSON kind first

func (*JSON) Object

func (json *JSON) Object() map[interface{}]*JSON

Object return map of JSON. Will panic if kind is not Object

func (*JSON) Runes

func (json *JSON) Runes() []rune

Runes return raw runes of extracted json

func (*JSON) SetBool

func (json *JSON) SetBool(b bool)

SetBool set bool value. Will panic if kind is not Boolean

func (*JSON) SetFloat

func (json *JSON) SetFloat(i float64)

SetFloat set float value. Will panic if kind is not Float

func (*JSON) SetInt

func (json *JSON) SetInt(i int64)

SetInt set int value. Will panic if kind is not Integer

func (*JSON) SetStr

func (json *JSON) SetStr(str string)

SetStr set string value. Will panic if kind is not String

func (*JSON) String

func (json *JSON) String() string

String return string value. Will panic if kind is not String

Directories

Path Synopsis
examples
array command
boolean command
edit-val/array command
edit-val/object command
heavy command
null command
numeric command
object command
string command

Jump to

Keyboard shortcuts

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