Skip to content

jsonqueryvsmxj

MIT 3 1 250
58.1 thousand (month) Oct 15 2019 v1.3.5(21 days ago)
617 2 1 MIT
Feb 09 2017 58.1 thousand (month) per(11 months ago)

jsonquery is a Go library that allows you to parse and extract data from JSON documents using JSONPath expressions. JSONPath is similar to XPath, but it is designed specifically for working with JSON documents.

The jsonquery library allows you to traverse the JSON tree structure and extract values using JSONPath expressions. It provides a simple and intuitive API for querying the JSON data, and it is built on top of the popular jsoniter library.

mxj is a Go library for working with JSON and XML data. It allows you to convert between JSON and XML, merge JSON and XML documents, and extract values from JSON and XML using a simple and intuitive API.

One of the main features of mxj is its ability to work with JSON and XML data in a struct-like manner, allowing you to access values using dot notation.

Example Use


package main

import (
  "fmt"
  "log"

  "github.com/antchfx/jsonquery"
)

func main() {
  // Parse the JSON string
  doc, err := jsonquery.Parse([]byte(`
    {
      "name": "John Doe",
      "age": 30,
      "address": {
        "street": "Main St",
        "city": "Anytown",
        "state": "CA",
        "zip": "12345"
      },
      "phones": [
        "555-555-5555",
        "555-555-5556"
      ]
    }
  `))
  if err != nil {
    log.Fatal(err)
  }

  // Extract the name
  name := jsonquery.FindOne(doc, "name")
  fmt.Println(name.InnerText())  // "John Doe"

  // Extract the city
  city := jsonquery.FindOne(doc, "address.city")
  fmt.Println(city.InnerText())  // "Anytown"

  // Extract all phone numbers
  phones := jsonquery.Find(doc, "phones[*]")
  for _, phone := range phones {
    fmt.Println(phone.InnerText())
  }
  // "555-555-5555"
  // "555-555-5556"
}
package main

import (
  "fmt"

  "github.com/clbanning/mxj"
)

func main() {
  // Parse the JSON string
  jsonData := []byte(`
    {
      "name": "John Doe",
      "age": 30,
      "address": {
        "street": "Main St",
        "city": "Anytown",
        "state": "CA",
        "zip": "12345"
      },
      "phones": [
        "555-555-5555",
        "555-555-5556"
      ]
    }
  `)
  mv, err := mxj.NewMapJson(jsonData)
  if err != nil {
    fmt.Println("Error:", err)
    return
  }

  // Extract the name
  name, _ := mv.ValueForPath("name")
  fmt.Println("name:", name)  // "John Doe"

  // Extract the city
  city, _ := mv.ValueForPath("address.city")
  fmt.Println("city:", city)  // "Anytown"

  // Extract all phone numbers
  phones, _ := mv.ValuesForPath("phones")
  for _, phone := range phones {
    fmt.Println("phone:", phone)
  }
  // "555-555-5555"
  // "555-555-5556"
}

Alternatives / Similar


Was this page helpful?