Skip to content

mxj

608 2 1 MIT
per (24 Jul 2023) Feb 09 2017 58.1 thousand (month)

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"

  "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


236 v1.3.3 (8 months ago) Oct 15 2019 compare
401 Release (6 months ago) Feb 08 2019 compare

Other Languages

2,053 1.0.1 (1 year, 9 months ago) Feb 09 2022 compare
529 1.6.1 (2 months ago) Feb 09 2022 compare
368 1.8.8 (9 months ago) Dec 03 2013 compare
209 0.2.25 (1 year, 8 months ago) Feb 09 2022 compare