General Codec - Methods - 图1tip

The following list of commonly used methods may be updated later than new code features. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/encoding/gjson

New

  • Description: New can create a Json object with any type of value data. However, due to data access reasons, data should be a map or slice, otherwise it is meaningless.

  • Note: The safe parameter determines whether the Json object is concurrent-safe, defaulting to false.

  • Format:

  1. func New(data interface{}, safe ...bool) *Json
  • Example:
  1. func ExampleNew() {
  2. jsonContent := `{"name":"john", "score":"100"}`
  3. j := gjson.New(jsonContent)
  4. fmt.Println(j.Get("name"))
  5. fmt.Println(j.Get("score"))
  6. // Output:
  7. // john
  8. // 100
  9. }

NewWithTag

  • Description: NewWithTag can create a Json object with any type of value data. However, due to data access reasons, data should be a map or slice, otherwise it is meaningless.

  • Note: The tgts parameter specifies the priority of tag names when converting structs to maps, with multiple tags separated by ','.

  • The safe parameter determines whether the Json object is concurrent-safe, defaulting to false.

  • Format:

  1. func NewWithTag(data interface{}, tags string, safe ...bool) *Json
  • Example:
  1. func ExampleNewWithTag() {
  2. type Me struct {
  3. Name string `tag:"name"`
  4. Score int `tag:"score"`
  5. Title string
  6. }
  7. me := Me{
  8. Name: "john",
  9. Score: 100,
  10. Title: "engineer",
  11. }
  12. j := gjson.NewWithTag(me, "tag", true)
  13. fmt.Println(j.Get("name"))
  14. fmt.Println(j.Get("score"))
  15. fmt.Println(j.Get("Title"))
  16. // Output:
  17. // john
  18. // 100
  19. // engineer
  20. }

NewWithOptions

  • Description: NewWithOptions can create a Json object with any type of value data. However, due to data access reasons, data should be a map or slice, otherwise it is meaningless.

  • Format:

  1. func NewWithOptions(data interface{}, options Options) *Json
  • Example:
  1. func ExampleNewWithOptions() {
  2. type Me struct {
  3. Name string `tag:"name"`
  4. Score int `tag:"score"`
  5. Title string
  6. }
  7. me := Me{
  8. Name: "john",
  9. Score: 100,
  10. Title: "engineer",
  11. }
  12. j := gjson.NewWithOptions(me, gjson.Options{
  13. Tags: "tag",
  14. })
  15. fmt.Println(j.Get("name"))
  16. fmt.Println(j.Get("score"))
  17. fmt.Println(j.Get("Title"))
  18. // Output:
  19. // john
  20. // 100
  21. // engineer
  22. }
  1. func ExampleNewWithOptions_UTF8BOM() {
  2. jsonContent := `{"name":"john", "score":"100"}`
  3. content := make([]byte, 3, len(jsonContent)+3)
  4. content[0] = 0xEF
  5. content[1] = 0xBB
  6. content[2] = 0xBF
  7. content = append(content, jsonContent...)
  8. j := gjson.NewWithOptions(content, gjson.Options{
  9. Tags: "tag",
  10. })
  11. fmt.Println(j.Get("name"))
  12. fmt.Println(j.Get("score"))
  13. // Output:
  14. // john
  15. // 100
  16. }

Load

  • Description: Load loads content from the specified file path and creates a Json object from it.

  • Format:

  1. func Load(path string, safe ...bool) (*Json, error)
  • Example:
  1. func ExampleLoad() {
  2. jsonFilePath := gtest.DataPath("json", "data1.json")
  3. j, _ := gjson.Load(jsonFilePath)
  4. fmt.Println(j.Get("name"))
  5. fmt.Println(j.Get("score"))
  6. notExistFilePath := gtest.DataPath("json", "data2.json")
  7. j2, _ := gjson.Load(notExistFilePath)
  8. fmt.Println(j2.Get("name"))
  9. // Output:
  10. // john
  11. // 100
  12. }
  1. func ExampleLoad_Xml() {
  2. jsonFilePath := gtest.DataPath("xml", "data1.xml")
  3. j, _ := gjson.Load(jsonFilePath)
  4. fmt.Println(j.Get("doc.name"))
  5. fmt.Println(j.Get("doc.score"))
  6. }

LoadJson

  • Description: LoadJson creates a Json object from the given content in JSON format.

  • Format:

  1. func LoadJson(data interface{}, safe ...bool) (*Json, error)
  • Example:
  1. func ExampleLoadJson() {
  2. jsonContent := `{"name":"john", "score":"100"}`
  3. j, _ := gjson.LoadJson(jsonContent)
  4. fmt.Println(j.Get("name"))
  5. fmt.Println(j.Get("score"))
  6. // Output:
  7. // john
  8. // 100
  9. }

LoadXml

  • Description: LoadXml creates a Json object from the given content in XML format.

  • Format:

  1. func LoadXml(data interface{}, safe ...bool) (*Json, error)
  • Example:
  1. func ExampleLoadXml() {
  2. xmlContent := `<?xml version="1.0" encoding="UTF-8"?>
  3. <base>
  4. <name>john</name>
  5. <score>100</score>
  6. </base>`
  7. j, _ := gjson.LoadXml(xmlContent)
  8. fmt.Println(j.Get("base.name"))
  9. fmt.Println(j.Get("base.score"))
  10. // Output:
  11. // john
  12. // 100
  13. }

LoadIni

  • Description: LoadIni creates a Json object from the given content in INI format.

  • Format:

  1. func LoadIni(data interface{}, safe ...bool) (*Json, error)
  • Example:
  1. func ExampleLoadIni() {
  2. iniContent := `
  3. [base]
  4. name = john
  5. score = 100
  6. `
  7. j, _ := gjson.LoadIni(iniContent)
  8. fmt.Println(j.Get("base.name"))
  9. fmt.Println(j.Get("base.score"))
  10. // Output:
  11. // john
  12. // 100
  13. }

LoadYaml

  • Description: LoadYaml creates a Json object from the given content in YAML format.

  • Format:

  1. func LoadYaml(data interface{}, safe ...bool) (*Json, error)
  • Example:
  1. func ExampleLoadYaml() {
  2. yamlContent :=
  3. `base:
  4. name: john
  5. score: 100`
  6. j, _ := gjson.LoadYaml(yamlContent)
  7. fmt.Println(j.Get("base.name"))
  8. fmt.Println(j.Get("base.score"))
  9. // Output:
  10. // john
  11. // 100
  12. }

LoadToml

  • Description: LoadToml creates a Json object from the given content in TOML format.

  • Format:

  1. func LoadToml(data interface{}, safe ...bool) (*Json, error)
  • Example:
  1. func ExampleLoadToml() {
  2. tomlContent :=
  3. `[base]
  4. name = "john"
  5. score = 100`
  6. j, _ := gjson.LoadToml(tomlContent)
  7. fmt.Println(j.Get("base.name"))
  8. fmt.Println(j.Get("base.score"))
  9. // Output:
  10. // john
  11. // 100
  12. }

LoadContent

  • Description: LoadContent creates a Json object based on the given content. It automatically checks the data type of content, supporting content types such as JSON, XML, INI, YAML, and TOML.

  • Format:

  1. func LoadContent(data interface{}, safe ...bool) (*Json, error)
  • Example:
  1. func ExampleLoadContent() {
  2. jsonContent := `{"name":"john", "score":"100"}`
  3. j, _ := gjson.LoadContent(jsonContent)
  4. fmt.Println(j.Get("name"))
  5. fmt.Println(j.Get("score"))
  6. // Output:
  7. // john
  8. // 100
  9. }
  1. func ExampleLoadContent_UTF8BOM() {
  2. jsonContent := `{"name":"john", "score":"100"}`
  3. content := make([]byte, 3, len(jsonContent)+3)
  4. content[0] = 0xEF
  5. content[1] = 0xBB
  6. content[2] = 0xBF
  7. content = append(content, jsonContent...)
  8. j, _ := gjson.LoadContent(content)
  9. fmt.Println(j.Get("name"))
  10. fmt.Println(j.Get("score"))
  11. // Output:
  12. // john
  13. // 100
  14. }
  1. func ExampleLoadContent_Xml() {
  2. xmlContent := `<?xml version="1.0" encoding="UTF-8"?>
  3. <base>
  4. <name>john</name>
  5. <score>100</score>
  6. </base>`
  7. x, _ := gjson.LoadContent(xmlContent)
  8. fmt.Println(x.Get("base.name"))
  9. fmt.Println(x.Get("base.score"))
  10. // Output:
  11. // john
  12. // 100
  13. }

LoadContentType

  • Description: LoadContentType creates a Json object based on the given content and type. Supported content types are Json, XML, INI, YAML, and TOML.

  • Format:

  1. func LoadContentType(dataType string, data interface{}, safe ...bool) (*Json, error)
  • Example:
  1. func ExampleLoadContentType() {
  2. jsonContent := `{"name":"john", "score":"100"}`
  3. xmlContent := `<?xml version="1.0" encoding="UTF-8"?>
  4. <base>
  5. <name>john</name>
  6. <score>100</score>
  7. </base>`
  8. j, _ := gjson.LoadContentType("json", jsonContent)
  9. x, _ := gjson.LoadContentType("xml", xmlContent)
  10. j1, _ := gjson.LoadContentType("json", "")
  11. fmt.Println(j.Get("name"))
  12. fmt.Println(j.Get("score"))
  13. fmt.Println(x.Get("base.name"))
  14. fmt.Println(x.Get("base.score"))
  15. fmt.Println(j1.Get(""))
  16. // Output:
  17. // john
  18. // 100
  19. // john
  20. // 100
  21. }

IsValidDataType

  • Description: IsValidDataType checks if the given dataType is a valid content type for loading.

  • Format:

  1. func IsValidDataType(dataType string) bool
  • Example:
  1. func ExampleIsValidDataType() {
  2. fmt.Println(gjson.IsValidDataType("json"))
  3. fmt.Println(gjson.IsValidDataType("yml"))
  4. fmt.Println(gjson.IsValidDataType("js"))
  5. fmt.Println(gjson.IsValidDataType("mp4"))
  6. fmt.Println(gjson.IsValidDataType("xsl"))
  7. fmt.Println(gjson.IsValidDataType("txt"))
  8. fmt.Println(gjson.IsValidDataType(""))
  9. fmt.Println(gjson.IsValidDataType(".json"))
  10. // Output:
  11. // true
  12. // true
  13. // true
  14. // false
  15. // false
  16. // false
  17. // false
  18. // true
  19. }

Valid

  • Description: Valid checks if data is a valid JSON data type. The data parameter specifies the JSON formatted data, which can be of type bytes or string.

  • Format:

  1. func Valid(data interface{}) bool
  • Example:
  1. func ExampleValid() {
  2. data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
  3. data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`)
  4. fmt.Println(gjson.Valid(data1))
  5. fmt.Println(gjson.Valid(data2))
  6. // Output:
  7. // true
  8. // false
  9. }

Marshal

  • Description: Marshal is an alias for Encode.

  • Format:

  1. func Marshal(v interface{}) (marshaledBytes []byte, err error)
  • Example:
  1. func ExampleMarshal() {
  2. data := map[string]interface{}{
  3. "name": "john",
  4. "score": 100,
  5. }
  6. jsonData, _ := gjson.Marshal(data)
  7. fmt.Println(string(jsonData))
  8. type BaseInfo struct {
  9. Name string
  10. Age int
  11. }
  12. info := BaseInfo{
  13. Name: "Guo Qiang",
  14. Age: 18,
  15. }
  16. infoData, _ := gjson.Marshal(info)
  17. fmt.Println(string(infoData))
  18. // Output:
  19. // {"name":"john","score":100}
  20. // {"Name":"Guo Qiang","Age":18}
  21. }

MarshalIndent

  • Description: MarshalIndent is an alias for json.MarshalIndent.

  • Format:

  1. func MarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error)
  • Example:
  1. func ExampleMarshalIndent() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. infoData, _ := gjson.MarshalIndent(info, "", "\t")
  11. fmt.Println(string(infoData))
  12. // Output:
  13. // {
  14. // "Name": "John",
  15. // "Age": 18
  16. // }
  17. }

Unmarshal

  • Description: Unmarshal is an alias for DecodeTo.

  • Format:

  1. func Unmarshal(data []byte, v interface{}) (err error)
  • Example:
  1. func ExampleUnmarshal() {
  2. type BaseInfo struct {
  3. Name string
  4. Score int
  5. }
  6. var info BaseInfo
  7. jsonContent := "{\"name\":\"john\",\"score\":100}"
  8. gjson.Unmarshal([]byte(jsonContent), &info)
  9. fmt.Printf("%+v", info)
  10. // Output:
  11. // {Name:john Score:100}
  12. }

Encode

  • Description: Encode serializes any type value into a byte array with content in JSON format.

  • Format:

  1. func Encode(value interface{}) ([]byte, error)
  • Example:
  1. func ExampleEncode() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. infoData, _ := gjson.Encode(info)
  11. fmt.Println(string(infoData))
  12. // Output:
  13. // {"Name":"John","Age":18}
  14. }

MustEncode

  • Description: MustEncode performs the Encode operation but will panic if any error occurs.

  • Format:

  1. func MustEncode(value interface{}) []byte
  • Example:
  1. func ExampleMustEncode() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. infoData := gjson.MustEncode(info)
  11. fmt.Println(string(infoData))
  12. // Output:
  13. // {"Name":"John","Age":18}
  14. }

EncodeString

  • Description: EncodeString serializes any type value into a string with content in JSON format.

  • Format:

  1. func EncodeString(value interface{}) (string, error)
  • Example:
  1. func ExampleEncodeString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. infoData, _ := gjson.EncodeString(info)
  11. fmt.Println(infoData)
  12. // Output:
  13. // {"Name":"John","Age":18}
  14. }

MustEncodeString

  • Description: MustEncodeString serializes any type value into a string with content in JSON format but will panic if any error occurs.

  • Format:

  1. func MustEncodeString(value interface{}) string
  • Example:
  1. func ExampleMustEncodeString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. infoData := gjson.MustEncodeString(info)
  11. fmt.Println(infoData)
  12. // Output:
  13. // {"Name":"John","Age":18}
  14. }

Decode

  • Description: Decode decodes data in JSON format to interface{}. The data parameter can be []byte or string.

  • Format:

  1. func Decode(data interface{}, options ...Options) (interface{}, error)
  • Example:
  1. func ExampleDecode() {
  2. jsonContent := `{"name":"john","score":100}`
  3. info, _ := gjson.Decode([]byte(jsonContent))
  4. fmt.Println(info)
  5. // Output:
  6. // map[name:john score:100]
  7. }

DecodeTo

  • Description: DecodeTo decodes data in JSON format to the specified interface type variable v. The data parameter can be []byte or string. The v parameter should be of pointer type.

  • Format:

  1. func DecodeTo(data interface{}, v interface{}, options ...Options) (err error)
  • Example:
  1. func ExampleDecodeTo() {
  2. type BaseInfo struct {
  3. Name string
  4. Score int
  5. }
  6. var info BaseInfo
  7. jsonContent := "{\"name\":\"john\",\"score\":100}"
  8. gjson.DecodeTo([]byte(jsonContent), &info)
  9. fmt.Printf("%+v", info)
  10. // Output:
  11. // {Name:john Score:100}
  12. }

DecodeToJson

  • Description: DecodeToJson encodes data in JSON format into a json object. The data parameter can be []byte or string.

  • Format:

  1. func DecodeToJson(data interface{}, options ...Options) (*Json, error)
  • Example:
  1. func ExampleDecodeToJson() {
  2. jsonContent := `{"name":"john","score":100}"`
  3. j, _ := gjson.DecodeToJson([]byte(jsonContent))
  4. fmt.Println(j.Map())
  5. // May Output:
  6. // map[name:john score:100]
  7. }

SetSplitChar

  • Description: SetSplitChar sets the level delimiter for data access.

  • Format:

  1. func (j *Json) SetSplitChar(char byte)
  • Example:
  1. func ExampleJson_SetSplitChar() {
  2. data :=
  3. `{
  4. "users" : {
  5. "count" : 2,
  6. "list" : [
  7. {"name" : "Ming", "score" : 60},
  8. {"name" : "John", "score" : 99.5}
  9. ]
  10. }
  11. }`
  12. if j, err := gjson.DecodeToJson(data); err != nil {
  13. panic(err)
  14. } else {
  15. j.SetSplitChar('#')
  16. fmt.Println("John Score:", j.Get("users#list#1#score").Float32())
  17. }
  18. // Output:
  19. // John Score: 99.5
  20. }

SetViolenceCheck

  • Description: SetViolenceCheck enables/disables violent check for data level access.

  • Format:

  1. func (j *Json) SetViolenceCheck(enabled bool)
  • Example:
  1. func ExampleJson_SetViolenceCheck() {
  2. data :=
  3. `{
  4. "users" : {
  5. "count" : 100
  6. },
  7. "users.count" : 101
  8. }`
  9. if j, err := gjson.DecodeToJson(data); err != nil {
  10. fmt.Println(err)
  11. } else {
  12. j.SetViolenceCheck(true)
  13. fmt.Println("Users Count:", j.Get("users.count"))
  14. }
  15. // Output:
  16. // Users Count: 101
  17. }

ToJson

  • Description: ToJson returns the JSON content as a []byte type.

  • Format:

  1. func (j *Json) ToJson() ([]byte, error)
  • Example:
  1. func ExampleJson_ToJson() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. jsonBytes, _ := j.ToJson()
  12. fmt.Println(string(jsonBytes))
  13. // Output:
  14. // {"Age":18,"Name":"John"}
  15. }

ToJsonString

  • Description: ToJsonString returns the JSON content as a string type.

  • Format:

  1. func (j *Json) ToJsonString() (string, error)
  • Example:
  1. func ExampleJson_ToJsonString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. jsonStr, _ := j.ToJsonString()
  12. fmt.Println(jsonStr)
  13. // Output:
  14. // {"Age":18,"Name":"John"}
  15. }

ToJsonIndent

  • Description: ToJsonIndent returns the indented JSON content as a []byte type.

  • Format:

  1. func (j *Json) ToJsonIndent() ([]byte, error)
  • Example:
  1. func ExampleJson_ToJsonIndent() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. jsonBytes, _ := j.ToJsonIndent()
  12. fmt.Println(string(jsonBytes))
  13. // Output:
  14. //{
  15. // "Age": 18,
  16. // "Name": "John"
  17. //}
  18. }

ToJsonIndentString

  • Description: ToJsonIndentString returns the indented JSON content as a string type.

  • Format:

  1. func (j *Json) ToJsonIndentString() (string, error)
  • Example:
  1. func ExampleJson_ToJsonIndentString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. jsonStr, _ := j.ToJsonIndentString()
  12. fmt.Println(jsonStr)
  13. // Output:
  14. //{
  15. // "Age": 18,
  16. // "Name": "John"
  17. //}
  18. }

MustToJson

  • Description: MustToJson returns the JSON content as a []byte type, and if any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToJson() []byte
  • Example:
  1. func ExampleJson_MustToJson() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. jsonBytes := j.MustToJson()
  12. fmt.Println(string(jsonBytes))
  13. // Output:
  14. // {"Age":18,"Name":"John"}
  15. }

MustToJsonString

  • Description: MustToJsonString returns the JSON content as a string type, and if any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToJsonString() string
  • Example:
  1. func ExampleJson_MustToJsonString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. jsonStr := j.MustToJsonString()
  12. fmt.Println(jsonStr)
  13. // Output:
  14. // {"Age":18,"Name":"John"}
  15. }

MustToJsonIndent

  • Description: MustToJsonStringIndent returns the indented JSON content as a []byte type, and if any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToJsonIndent() []byte
  • Example:
  1. func ExampleJson_MustToJsonIndent() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. jsonBytes := j.MustToJsonIndent()
  12. fmt.Println(string(jsonBytes))
  13. // Output:
  14. //{
  15. // "Age": 18,
  16. // "Name": "John"
  17. //}
  18. }

MustToJsonIndentString

  • Description: MustToJsonStringIndent returns the indented JSON content as a string type, and if any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToJsonIndentString() string
  • Example:
  1. func ExampleJson_MustToJsonIndentString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. jsonStr := j.MustToJsonIndentString()
  12. fmt.Println(jsonStr)
  13. // Output:
  14. //{
  15. // "Age": 18,
  16. // "Name": "John"
  17. //}
  18. }

ToXml

  • Description: ToXml returns content in XML format as a []byte type.

  • Format:

  1. func (j *Json) ToXml(rootTag ...string) ([]byte, error)
  • Example:
  1. func ExampleJson_ToXml() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. xmlBytes, _ := j.ToXml()
  12. fmt.Println(string(xmlBytes))
  13. // Output:
  14. // <doc><Age>18</Age><Name>John</Name></doc>
  15. }

ToXmlString

  • Description: ToXmlString returns content in XML format as a string type.

  • Format:

  1. func (j *Json) ToXmlString(rootTag ...string) (string, error)
  • Example:
  1. func ExampleJson_ToXmlString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. xmlStr, _ := j.ToXmlString()
  12. fmt.Println(string(xmlStr))
  13. // Output:
  14. // <doc><Age>18</Age><Name>John</Name></doc>
  15. }

ToXmlIndent

  • Description: ToXmlIndent returns indented content in XML format as a []byte type.

  • Format:

  1. func (j *Json) ToXmlIndent(rootTag ...string) ([]byte, error)
  • Example:
  1. func ExampleJson_ToXmlIndent() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. xmlBytes, _ := j.ToXmlIndent()
  12. fmt.Println(string(xmlBytes))
  13. // Output:
  14. //<doc>
  15. // <Age>18</Age>
  16. // <Name>John</Name>
  17. //</doc>
  18. }

ToXmlIndentString

  • Description: ToXmlIndentString returns indented content in XML format as a string type.

  • Format:

  1. func (j *Json) ToXmlIndentString(rootTag ...string) (string, error)
  • Example:
  1. func ExampleJson_ToXmlIndentString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. xmlStr, _ := j.ToXmlIndentString()
  12. fmt.Println(string(xmlStr))
  13. // Output:
  14. //<doc>
  15. // <Age>18</Age>
  16. // <Name>John</Name>
  17. //</doc>
  18. }

MustToXml

  • Description: MustToXml returns content in XML format as a []byte type. If any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToXml(rootTag ...string) []byte
  • Example:
  1. func ExampleJson_MustToXml() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. xmlBytes := j.MustToXml()
  12. fmt.Println(string(xmlBytes))
  13. // Output:
  14. // <doc><Age>18</Age><Name>John</Name></doc>
  15. }

MustToXmlString

  • Description: MustToXmlString returns content in XML format as a string type. If any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToXmlString(rootTag ...string) string
  • Example:
  1. func ExampleJson_MustToXmlString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. xmlStr := j.MustToXmlString()
  12. fmt.Println(string(xmlStr))
  13. // Output:
  14. // <doc><Age>18</Age><Name>John</Name></doc>
  15. }

MustToXmlIndent

  • Description: MustToXmlStringIndent returns indented content in XML format as a []byte type. If any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToXmlIndent(rootTag ...string) []byte
  • Example:
  1. func ExampleJson_MustToXmlIndent() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. xmlBytes := j.MustToXmlIndent()
  12. fmt.Println(string(xmlBytes))
  13. // Output:
  14. //<doc>
  15. // <Age>18</Age>
  16. // <Name>John</Name>
  17. //</doc>
  18. }

MustToXmlIndentString

  • Description: MustToXmlStringIndentString returns indented content in XML format as a string type. If any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToXmlIndentString(rootTag ...string) string
  • Example:
  1. func ExampleJson_MustToXmlIndentString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. xmlStr := j.MustToXmlIndentString()
  12. fmt.Println(string(xmlStr))
  13. // Output:
  14. //<doc>
  15. // <Age>18</Age>
  16. // <Name>John</Name>
  17. //</doc>
  18. }

ToYaml

  • Description: ToYaml returns content in YAML format as a []byte type.

  • Format:

  1. func (j *Json) ToYaml() ([]byte, error)
  • Example:
  1. func ExampleJson_ToYaml() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. YamlBytes, _ := j.ToYaml()
  12. fmt.Println(string(YamlBytes))
  13. // Output:
  14. //Age: 18
  15. //Name: John
  16. }

ToYamlIndent

  • Description: ToYamlIndent returns indented content in YAML format as a []byte type.

  • Format:

  1. func (j *Json) ToYamlIndent(indent string) ([]byte, error)
  • Example:
  1. func ExampleJson_ToYamlIndent() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. YamlBytes, _ := j.ToYamlIndent("")
  12. fmt.Println(string(YamlBytes))
  13. // Output:
  14. //Age: 18
  15. //Name: John
  16. }

ToYamlString

  • Description: ToYamlString returns content in YAML format as a string type.

  • Format:

  1. func (j *Json) ToYamlString() (string, error)
  • Example:
  1. func ExampleJson_ToYamlString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. YamlStr, _ := j.ToYamlString()
  12. fmt.Println(string(YamlStr))
  13. // Output:
  14. //Age: 18
  15. //Name: John
  16. }

MustToYaml

  • Description: MustToYaml returns content in YAML format as a []byte type. If any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToYaml() []byte
  • Example:
  1. func ExampleJson_MustToYaml() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. YamlBytes := j.MustToYaml()
  12. fmt.Println(string(YamlBytes))
  13. // Output:
  14. //Age: 18
  15. //Name: John
  16. }

MustToYamlString

  • Description: MustToYamlString returns content in YAML format as a string type. If any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToYamlString() string
  • Example:
  1. func ExampleJson_MustToYamlString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. YamlStr := j.MustToYamlString()
  12. fmt.Println(string(YamlStr))
  13. // Output:
  14. //Age: 18
  15. //Name: John
  16. }

ToToml

  • Description: ToToml returns content in TOML format as a []byte type.

  • Format:

  1. func (j *Json) ToToml() ([]byte, error)
  • Example:
  1. func ExampleJson_ToToml() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. TomlBytes, _ := j.ToToml()
  12. fmt.Println(string(TomlBytes))
  13. // Output:
  14. //Age = 18
  15. //Name = "John"
  16. }

ToTomlString

  • Description: ToTomlString returns content in TOML format as a string type.

  • Format:

  1. func (j *Json) ToTomlString() (string, error)
  • Example:
  1. func ExampleJson_ToTomlString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. TomlStr, _ := j.ToTomlString()
  12. fmt.Println(string(TomlStr))
  13. // Output:
  14. //Age = 18
  15. //Name = "John"
  16. }

MustToToml

  • Description: MustToToml returns content in TOML format as a []byte type. If any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToToml() []byte
  • Example:
  1. func ExampleJson_MustToToml() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. TomlBytes := j.MustToToml()
  12. fmt.Println(string(TomlBytes))
  13. // Output:
  14. //Age = 18
  15. //Name = "John"
  16. }

MustToTomlString

  • Description: MustToTomlString returns content in TOML format as a string type. If any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToTomlString() string
  • Example:
  1. func ExampleJson_MustToTomlString() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. TomlStr := j.MustToTomlString()
  12. fmt.Println(string(TomlStr))
  13. // Output:
  14. //Age = 18
  15. //Name = "John"
  16. }

ToIni

  • Description: ToIni returns content in INI format as a []byte type.

  • Format:

  1. func (j *Json) ToIni() ([]byte, error)
  • Example:
  1. func ExampleJson_ToIni() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. IniBytes, _ := j.ToIni()
  12. fmt.Println(string(IniBytes))
  13. // May Output:
  14. //Name=John
  15. //Age=18
  16. }

ToIniString

  • Description: ToIniString returns content in INI format as a string type.

  • Format:

  1. func (j *Json) ToIniString() (string, error)
  • Example:
  1. func ExampleJson_ToIniString() {
  2. type BaseInfo struct {
  3. Name string
  4. }
  5. info := BaseInfo{
  6. Name: "John",
  7. }
  8. j := gjson.New(info)
  9. IniStr, _ := j.ToIniString()
  10. fmt.Println(string(IniStr))
  11. // Output:
  12. //Name=John
  13. }

MustToIni

  • Description: MustToIni returns content in INI format as a []byte type. If any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToIni() []byte
  • Example:
  1. func ExampleJson_MustToIni() {
  2. type BaseInfo struct {
  3. Name string
  4. }
  5. info := BaseInfo{
  6. Name: "John",
  7. }
  8. j := gjson.New(info)
  9. IniBytes := j.MustToIni()
  10. fmt.Println(string(IniBytes))
  11. // Output:
  12. //Name=John
  13. }

MustToIniString

  • Description: MustToIniString returns content in INI format as a string type. If any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustToIniString() string
  • Example:
  1. func ExampleJson_MustToIniString() {
  2. type BaseInfo struct {
  3. Name string
  4. }
  5. info := BaseInfo{
  6. Name: "John",
  7. }
  8. j := gjson.New(info)
  9. IniStr := j.MustToIniString()
  10. fmt.Println(string(IniStr))
  11. // Output:
  12. //Name=John
  13. }

MarshalJSON

  • Description: MarshalJSON implements the json.Marshal interface MarshalJSON.

  • Format:

  1. func (j Json) MarshalJSON() ([]byte, error)
  • Example:
  1. func ExampleJson_MarshalJSON() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. jsonBytes, _ := j.MarshalJSON()
  12. fmt.Println(string(jsonBytes))
  13. // Output:
  14. // {"Age":18,"Name":"John"}
  15. }

UnmarshalJSON

  • Description: UnmarshalJSON implements the json.Unmarshal interface UnmarshalJSON.

  • Format:

  1. func (j *Json) UnmarshalJSON(b []byte) error
  • Example:
  1. func ExampleJson_UnmarshalJSON() {
  2. jsonStr := `{"Age":18,"Name":"John"}`
  3. j := gjson.New("")
  4. j.UnmarshalJSON([]byte(jsonStr))
  5. fmt.Println(j.Map())
  6. // Output:
  7. // map[Age:18 Name:John]
  8. }

UnmarshalValue

  • Description: UnmarshalValue is an interface implementation for setting any type of value to Json.

  • Format:

  1. func (j *Json) UnmarshalValue(value interface{}) error
  • Example:
  1. func ExampleJson_UnmarshalValue_Yaml() {
  2. yamlContent :=
  3. `base:
  4. name: john
  5. score: 100`
  6. j := gjson.New("")
  7. j.UnmarshalValue([]byte(yamlContent))
  8. fmt.Println(j.Var().String())
  9. // Output:
  10. // {"base":{"name":"john","score":100}}
  11. }
  1. func ExampleJson_UnmarshalValue_Xml() {
  2. xmlStr := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>`
  3. j := gjson.New("")
  4. j.UnmarshalValue([]byte(xmlStr))
  5. fmt.Println(j.Var().String())
  6. // Output:
  7. // {"doc":{"name":"john","score":"100"}}
  8. }

MapStrAny

  • Description: MapStrAny implements the interface method MapStrAny().

  • Format:

  1. func (j *Json) MapStrAny() map[string]interface{}
  • Example:
  1. func ExampleJson_MapStrAny() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. fmt.Println(j.MapStrAny())
  12. // Output:
  13. // map[Age:18 Name:John]
  14. }

Interfaces

  • Description: Interfaces implements the interface method Interfaces().

  • Format:

  1. func (j *Json) Interfaces() []interface{}
  • Example:
  1. func ExampleJson_Interfaces() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. infoList := []BaseInfo{
  7. BaseInfo{
  8. Name: "John",
  9. Age: 18,
  10. },
  11. BaseInfo{
  12. Name: "Tom",
  13. Age: 20,
  14. },
  15. }
  16. j := gjson.New(infoList)
  17. fmt.Println(j.Interfaces())
  18. // Output:
  19. // [{John 18} {Tom 20}]
  20. }

Interface

  • Description: Interface returns the value of the Json object.

  • Format:

  1. func (j *Json) Interface() interface{}
  • Example:
  1. func ExampleJson_Interface() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. fmt.Println(j.Interface())
  12. var nilJ *gjson.Json = nil
  13. fmt.Println(nilJ.Interface())
  14. // Output:
  15. // map[Age:18 Name:John]
  16. // <nil>
  17. }

Var

  • Description: Var returns the value of the Json object as type *gvar.Var.

  • Format:

  1. func (j *Json) Var() *gvar.Var
  • Example:
  1. func ExampleJson_Var() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. fmt.Println(j.Var().String())
  12. fmt.Println(j.Var().Map())
  13. // Output:
  14. // {"Age":18,"Name":"John"}
  15. // map[Age:18 Name:John]
  16. }

IsNil

  • Description: IsNil checks whether the Json object’s value is nil.

  • Format:

  1. func (j *Json) IsNil() bool
  • Example:
  1. func ExampleJson_IsNil() {
  2. data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
  3. data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`)
  4. j1, _ := gjson.LoadContent(data1)
  5. fmt.Println(j1.IsNil())
  6. j2, _ := gjson.LoadContent(data2)
  7. fmt.Println(j2.IsNil())
  8. // Output:
  9. // false
  10. // true
  11. }

Get

  • Description: Get retrieves and returns the value according to the specified pattern. If the pattern is ".", it will return all values of the current Jsonobject. If nopatternis found, it returnsnil`.

  • Format:

  1. func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var
  • Example:
  1. func ExampleJson_Get() {
  2. data :=
  3. `{
  4. "users" : {
  5. "count" : 1,
  6. "array" : ["John", "Ming"]
  7. }
  8. }`
  9. j, _ := gjson.LoadContent(data)
  10. fmt.Println(j.Get("."))
  11. fmt.Println(j.Get("users"))
  12. fmt.Println(j.Get("users.count"))
  13. fmt.Println(j.Get("users.array"))
  14. var nilJ *gjson.Json = nil
  15. fmt.Println(nilJ.Get("."))
  16. // Output:
  17. // {"users":{"array":["John","Ming"],"count":1}}
  18. // {"array":["John","Ming"],"count":1}
  19. // 1
  20. // ["John","Ming"]
  21. }

GetJson

  • Description: GetJson retrieves the value specified by pattern and converts it into a non-concurrent-safe Json object.

  • Format:

  1. func (j *Json) GetJson(pattern string, def ...interface{}) *Json
  • Example:
  1. func ExampleJson_GetJson() {
  2. data :=
  3. `{
  4. "users" : {
  5. "count" : 1,
  6. "array" : ["John", "Ming"]
  7. }
  8. }`
  9. j, _ := gjson.LoadContent(data)
  10. fmt.Println(j.GetJson("users.array").Array())
  11. // Output:
  12. // [John Ming]
  13. }

GetJsons

  • Description: GetJsons retrieves the value specified by pattern and converts it into a slice of non-concurrent-safe Json objects.

  • Format:

  1. func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json
  • Example:
  1. func ExampleJson_GetJsons() {
  2. data :=
  3. `{
  4. "users" : {
  5. "count" : 3,
  6. "array" : [{"Age":18,"Name":"John"}, {"Age":20,"Name":"Tom"}]
  7. }
  8. }`
  9. j, _ := gjson.LoadContent(data)
  10. jsons := j.GetJsons("users.array")
  11. for _, json := range jsons {
  12. fmt.Println(json.Interface())
  13. }
  14. // Output:
  15. // map[Age:18 Name:John]
  16. // map[Age:20 Name:Tom]
  17. }

GetJsonMap

  • Description: GetJsonMap retrieves the value specified by pattern and converts it into a map of non-concurrent-safe Json objects.

  • Format:

  1. func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json
  • Example:
  1. func ExampleJson_GetJsonMap() {
  2. data :=
  3. `{
  4. "users" : {
  5. "count" : 1,
  6. "array" : {
  7. "info" : {"Age":18,"Name":"John"},
  8. "addr" : {"City":"Chengdu","Company":"Tencent"}
  9. }
  10. }
  11. }`
  12. j, _ := gjson.LoadContent(data)
  13. jsonMap := j.GetJsonMap("users.array")
  14. for _, json := range jsonMap {
  15. fmt.Println(json.Interface())
  16. }
  17. // May Output:
  18. // map[City:Chengdu Company:Tencent]
  19. // map[Age:18 Name:John]
  20. }

Set

  • Description: Set sets the value of the specified pattern. It supports data level access by default using the '.' character.

  • Format:

  1. func (j *Json) Set(pattern string, value interface{}) error
  • Example:
  1. func ExampleJson_Set() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. j.Set("Addr", "ChengDu")
  12. j.Set("Friends.0", "Tom")
  13. fmt.Println(j.Var().String())
  14. // Output:
  15. // {"Addr":"ChengDu","Age":18,"Friends":["Tom"],"Name":"John"}
  16. }

MustSet

  • Description: MustSet performs the Set operation, but if any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustSet(pattern string, value interface{})
  • Example:
  1. func ExampleJson_MustSet() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. j.MustSet("Addr", "ChengDu")
  12. fmt.Println(j.Var().String())
  13. // Output:
  14. // {"Addr":"ChengDu","Age":18,"Name":"John"}
  15. }

Remove

  • Description: Remove deletes the value of the specified pattern. It supports data level access by default using the '.' character.

  • Format:

  1. func (j *Json) Remove(pattern string) error
  • Example:
  1. func ExampleJson_Remove() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. j.Remove("Age")
  12. fmt.Println(j.Var().String())
  13. // Output:
  14. // {"Name":"John"}
  15. }

MustRemove

  • Description: MustRemove performs Remove, but if any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustRemove(pattern string)
  • Example:
  1. func ExampleJson_MustRemove() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. j.MustRemove("Age")
  12. fmt.Println(j.Var().String())
  13. // Output:
  14. // {"Name":"John"}
  15. }

Contains

  • Description: Contains checks if the value of the specified pattern exists.

  • Format:

  1. func (j *Json) Contains(pattern string) bool
  • Example:
  1. func ExampleJson_Contains() {
  2. type BaseInfo struct {
  3. Name string
  4. Age int
  5. }
  6. info := BaseInfo{
  7. Name: "John",
  8. Age: 18,
  9. }
  10. j := gjson.New(info)
  11. fmt.Println(j.Contains("Age"))
  12. fmt.Println(j.Contains("Addr"))
  13. // Output:
  14. // true
  15. // false
  16. }

Len

  • Description: Len returns the length/size of a value according to the specified pattern. The type of the pattern value should be a slice or a map. If the target value is not found or the type is invalid, it returns -1.

  • Format:

  1. func (j *Json) Len(pattern string) int
  • Example:
  1. func ExampleJson_Len() {
  2. data :=
  3. `{
  4. "users" : {
  5. "count" : 1,
  6. "nameArray" : ["Join", "Tom"],
  7. "infoMap" : {
  8. "name" : "Join",
  9. "age" : 18,
  10. "addr" : "ChengDu"
  11. }
  12. }
  13. }`
  14. j, _ := gjson.LoadContent(data)
  15. fmt.Println(j.Len("users.nameArray"))
  16. fmt.Println(j.Len("users.infoMap"))
  17. // Output:
  18. // 2
  19. // 3
  20. }

Append

  • Description: Append appends a value to the Json object using the specified pattern. The type of the pattern value should be a slice.

  • Format:

  1. func (j *Json) Append(pattern string, value interface{}) error
  • Example:
  1. func ExampleJson_Append() {
  2. data :=
  3. `{
  4. "users" : {
  5. "count" : 1,
  6. "array" : ["John", "Ming"]
  7. }
  8. }`
  9. j, _ := gjson.LoadContent(data)
  10. j.Append("users.array", "Lily")
  11. fmt.Println(j.Get("users.array").Array())
  12. // Output:
  13. // [John Ming Lily]
  14. }

MustAppend

  • Description: MustAppend performs Append, but if any error occurs, it will panic.

  • Format:

  1. func (j *Json) MustAppend(pattern string, value interface{})
  • Example:
  1. func ExampleJson_MustAppend() {
  2. data :=
  3. `{
  4. "users" : {
  5. "count" : 1,
  6. "array" : ["John", "Ming"]
  7. }
  8. }`
  9. j, _ := gjson.LoadContent(data)
  10. j.MustAppend("users.array", "Lily")
  11. fmt.Println(j.Get("users.array").Array())
  12. // Output:
  13. // [John Ming Lily]
  14. }

Map

  • Description: Map converts the current Json object to a map[string]interface{}. It returns nil if conversion fails.

  • Format:

  1. func (j *Json) Map() map[string]interface{}
  • Example:
  1. func ExampleJson_Map() {
  2. data :=
  3. `{
  4. "users" : {
  5. "count" : 1,
  6. "info" : {
  7. "name" : "John",
  8. "age" : 18,
  9. "addr" : "ChengDu"
  10. }
  11. }
  12. }`
  13. j, _ := gjson.LoadContent(data)
  14. fmt.Println(j.Get("users.info").Map())
  15. // Output:
  16. // map[addr:ChengDu age:18 name:John]
  17. }

Array

  • Description: Array converts the current Json object to a []interface{}. It returns nil if conversion fails.

  • Format:

  1. func (j *Json) Array() []interface{}
  • Example:
  1. func ExampleJson_Array() {
  2. data :=
  3. `{
  4. "users" : {
  5. "count" : 1,
  6. "array" : ["John", "Ming"]
  7. }
  8. }`
  9. j, _ := gjson.LoadContent(data)
  10. fmt.Println(j.Get("users.array"))
  11. // Output:
  12. // ["John","Ming"]
  13. }

Scan

  • Description: Scan automatically calls the Struct or Structs function to perform conversion based on the type of the pointer parameter.

  • Format:

  1. func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error
  • Example:
  1. func ExampleJson_Scan() {
  2. data := `{"name":"john","age":"18"}`
  3. type BaseInfo struct {
  4. Name string
  5. Age int
  6. }
  7. info := BaseInfo{}
  8. j, _ := gjson.LoadContent(data)
  9. j.Scan(&info)
  10. fmt.Println(info)
  11. // May Output:
  12. // {john 18}
  13. }

Dump

  • Description: Dump prints the Json object in a more readable way.

  • Format:

  1. func (j *Json) Dump()
  • Example:
  1. func ExampleJson_Dump() {
  2. data := `{"name":"john","age":"18"}`
  3. j, _ := gjson.LoadContent(data)
  4. j.Dump()
  5. // May Output:
  6. //{
  7. // "name": "john",
  8. // "age": "18",
  9. //}
  10. }