# README
JSON Mapper Library for Go
A simple Go library to simplify working with JSON without the need to define structs.
Installation
To install the library, use:
go get github.com/rmordechay/jsonmapper
Create a Mapper
There are multiple ways to create a mapper.
// From bytes
mapper, err := jsonmapper.FromBytes([]byte(jsonBytes))
// From string
mapper, err := jsonmapper.FromString(jsonString)
// From struct
mapper, err := jsonmapper.FromStruct(jsonStruct)
// From file
mapper, err := jsonmapper.FromFile(jsonFilePath)
Read from JSON
Once you have the mapper
, you can read the data easily. Consider the following JSON
jsonString := `{
"name": "Jason",
"age": 43,
"height": 1.87,
"is_funny": false,
"birthday": "1981-10-08",
"features": ["tall", "blue eyes"],
"children": {
"Rachel": {"age": 15, "is_funny": false},
"Sara": {"age": 19, "is_funny": true}
}
}`
Check Types
fmt.Println(mapper.IsObject) // true
fmt.Println(mapper.IsBool) // false
fmt.Println(mapper.IsInt) // false
fmt.Println(mapper.IsFloat) // false
fmt.Println(mapper.IsString) // false
fmt.Println(mapper.IsArray) // false
fmt.Println(mapper.IsNull) // false
Objects
object := mapper.AsObject
// Check if a key exists
var keyExists bool = object.Has("children")
// Get the object's size
var keyExists bool = object.Length()
// Get object's keys
var keys []string = object.Keys()
// Get object's values
values := object.Values()
// Iterating over an object with key, value pair
children := object.GetObject("children")
for key, child := range children.Elements() {
fmt.Println("Child name:", key) // Rachel, Sara
fmt.Println(child.AsObject.GetInt("age")) // 15, 19
fmt.Println(child.AsObject.GetBool("is_funny")) // false, true
}
Arrays
// Get array
array := mapper.AsObject.GetArray("features")
// Get array length
arrayLen := array.Length() // 2
// Get an element of array by index
secondElement := array.Get(1)
// Iterating over an array
for _, feature := range array.Elements() {
fmt.Println(feature.AsString) // tall, ...
}
// Get as a slice of string
var stringArray []string = array.AsStringArray()
// Get as a slice of int
var intArray []int = array.AsIntArray()
// Get as a slice of float
var floatArray []float = array.AsFloatArray()
// Get as a slice of JsonArray
var nestedArray []JsonArray = array.As2DArray()
// Get as a slice of JsonObject
var objectArray []JsonObject = array.AsObjectArray()
Primitive Types
Getting primitive types - string
, int
, float64
or bool
- is similar both for object and array and only differ
in the parameter type (objects take string
as key and arrays take int
as index)
From Object
object := mapper.AsObject
// string
var name string = object.GetString("name") // Jason
// int
var age int = object.GetInt("age") // 15
// float64
var height float64 = object.GetFloat("height") // 1.87
// bool
var isFunny bool = object.GetBool("is_funny") // false
From Array
array := mapper.AsArray
// string
var s string = array.GetString(0)
// int
var i int = array.GetInt(2)
// float64
var f float64 = array.GetFloat(5)
// bool
var b bool = array.GetBool(7)
Time
To get a string as time.Time
var birthday time.Time = array.GetTime(0)
var birthday string = object.GetTime("birthday".Format(time.RFC3339)) // 1981-10-08T00:00:00Z
The following formats are supported:
time.RFC3339
time.RFC850
time.RFC822
time.RFC822Z
time.RFC1123
time.RFC1123Z
time.RFC3339Nano
time.ANSIC
time.UnixDate
time.RubyDate
time.Layout
time.Kitchen
time.Stamp
time.StampMilli
time.StampMicro
time.StampNano
time.DateTime
time.DateOnly
time.TimeOnly
Find Elements
You can search for a nested element.
element := mapper.AsObject.Find("Rachel")
fmt.Println(element.IsObject) // true
fmt.Println(element.Has("is_funny")) // true
Get as JSON String
You can get a string from every JSON element which is a valid JSON
fmt.Println(mapper.AsObject.String())
// output: {"age":43,"children":{"Rachel":{"age":15,"is_funny":false},"Sara":{"age":19,"is_funny":true}},"features":["tall","blue eyes"],"is_funny":false,"name":"Jason"}
fmt.Println(mapper.AsObject.Get("children").String())
// output: {"Rachel":{"age":15,"is_funny":false},"Sara":{"age":19,"is_funny":true}}
or with pretty string
fmt.Println(mapper.AsObject.Get("children").PrettyString())
// output:
// {
// "Rachel": {
// "age": 15,
// "is_funny": false
// },
// "Sara": {
// "age": 19,
// "is_funny": true
// }
// }
Write to JSON
To write a JSON object or array is as simple as reading from it.
Write Object
// Create a new object
obj := jsonmapper.NewObject()
obj.AddKeyValue("name", "Chris")
fmt.Println(obj.String()) // {"name":"Chris"}
Write Array
// Create a new array
arr := jsonmapper.NewArray()
arr.AddElement(15)
arr.AddElement(19)
fmt.Println(arr.String()) // [15,19]
# Packages
No description provided by the author
# Functions
EmptyArray initializes and returns an empty new instance of JsonArray.
EmptyObject initializes and returns an empty new instance of JsonObject.
No description provided by the author
FromBytes parses JSON data from a byte slice.
FromFile reads a JSON file from the given path and parses it into a JsonMapper object.
FromString parses JSON from a string into a JsonMapper object.
FromStruct serializes a Go struct into JSON and parses it into a JsonMapper object.
NewArray initializes and returns a new instance of JsonArray.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewObject initializes and returns a new instance of JsonObject.
No description provided by the author
No description provided by the author
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
JsonArray represents a JSON array.
JsonMapper represents a generic JSON type.
JsonObject represents a JSON object.