jsonquery

package module
v0.0.0-...-a725452 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2019 License: MIT Imports: 9 Imported by: 0

README

jsonquery

Build Status Coverage Status GoDoc Go Report Card

Overview

jsonquery is an XPath query package for JSON document, lets you extract data from JSON documents through an XPath expression.

Getting Started

Install Package

$ go get github.com/antchfx/jsonquery

Load JSON document from URL.
doc, err := jsonquery.LoadURL("http://www.example.com/feed?json")
Load JSON document from string.
s :=`{
    "name":"John",
    "age":31, 
    "city":"New York" 
    }`
doc, err := jsonquery.Parse(strings.NewReader(s))
Load JSON document from io.Reader.
f, err := os.Open("./books.json")
doc, err := jsonquery.Parse(f)
Find authors of all books in the store.
list := jsonquery.Find(doc, "store/book/*/author")
// or equal to
list := jsonquery.Find(doc, "//author")
Find the third book.
book := jsonquery.Find(doc, "//book/*[3]")
Find the last book.
book := jsonquery.Find(doc, "//book/*[last()]")
Find all books with isbn number.
list := jsonquery.Find(doc, "//book/*[isbn]")
Find all books cheapier than 10.
list := jsonquery.Find(doc, "//book/*[price<10]")

Quick Tutorial

func main() {
	s := `{
		"name": "John",
		"age"      : 26,
		"address"  : {
		  "streetAddress": "naist street",
		  "city"         : "Nara",
		  "postalCode"   : "630-0192"
		},
		"phoneNumbers": [
		  {
			"type"  : "iPhone",
			"number": "0123-4567-8888"
		  },
		  {
			"type"  : "home",
			"number": "0123-4567-8910"
		  }
		]
	}`
	doc, _ := jsonquery.Parse(strings.NewReader(s))
	name := jsonquery.FindOne(doc, "name")
	fmt.Printf("name: %s\n", name.InnerText())
	var a []string
	for _, n := range jsonquery.Find(doc, "phoneNumbers/*/number") {
		a = append(a, n.InnerText())
	}
	fmt.Printf("phone number: %s\n", strings.Join(a, ","))
	if n := jsonquery.FindOne(doc, "address/streetAddress"); n != nil {
		fmt.Printf("address: %s\n", n.InnerText())
	}
}

Implement Principle

If you are familiar with XPath and XML, you can quick start to known how write your XPath expression.

{
"name":"John",
"age":30,
"cars": [
	{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
	{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
	{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}

The above JSON document will be convert to similar to XML document by the JSONQuery, like below:

<name>John</name>
<age>30</age>
<cars>
	<element>
		<name>Ford</name>
		<models>
			<element>Fiesta</element>
			<element>Focus</element>
			<element>Mustang</element>
		</models>		
	</element>
	<element>
		<name>BMW</name>
		<models>
			<element>320</element>
			<element>X3</element>
			<element>X5</element>
		</models>		
	</element>
	<element>
		<name>Fiat</name>
		<models>
			<element>500</element>
			<element>Panda</element>
		</models>		
	</element>
</cars>

Notes: element is empty element that have no any name.

List of supported XPath query packages

Name Description
htmlquery XPath query package for the HTML document
xmlquery XPath query package for the XML document
jsonquery XPath query package for the JSON document

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	Parent, PrevSibling, NextSibling, FirstChild, LastChild *Node

	Type NodeType
	Data string

	Value interface{}
	// contains filtered or unexported fields
}

A Node consists of a NodeType and some Data (tag name for element nodes, content for text) and are part of a tree of Nodes.

func Find

func Find(top *Node, expr string) []*Node

Find searches the Node that matches by the specified XPath expr.

func FindOne

func FindOne(top *Node, expr string) *Node

FindOne searches the Node that matches by the specified XPath expr, and returns first element of matched.

func LoadURL

func LoadURL(url string) (*Node, error)

LoadURL loads the JSON document from the specified URL.

func Parse

func Parse(r io.Reader) (*Node, error)

Parse JSON document.

func ParseMap

func ParseMap(m interface{}) (*Node, error)

func (*Node) ChildNodes

func (n *Node) ChildNodes() []*Node

ChildNodes gets all child nodes of the node.

func (*Node) InnerText

func (n *Node) InnerText() string

InnerText gets the value of the node and all its child nodes.

func (*Node) SelectElement

func (n *Node) SelectElement(name string) *Node

SelectElement finds the first of child elements with the specified name.

type NodeNavigator

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

NodeNavigator is for navigating JSON document.

func CreateXPathNavigator

func CreateXPathNavigator(top *Node) *NodeNavigator

CreateXPathNavigator creates a new xpath.NodeNavigator for the specified html.Node.

func (*NodeNavigator) Copy

func (a *NodeNavigator) Copy() xpath.NodeNavigator

func (*NodeNavigator) Current

func (a *NodeNavigator) Current() *Node

func (*NodeNavigator) LocalName

func (a *NodeNavigator) LocalName() string

func (*NodeNavigator) MoveTo

func (a *NodeNavigator) MoveTo(other xpath.NodeNavigator) bool

func (*NodeNavigator) MoveToChild

func (a *NodeNavigator) MoveToChild() bool

func (*NodeNavigator) MoveToFirst

func (a *NodeNavigator) MoveToFirst() bool

func (*NodeNavigator) MoveToNext

func (a *NodeNavigator) MoveToNext() bool

func (*NodeNavigator) MoveToNextAttribute

func (x *NodeNavigator) MoveToNextAttribute() bool

func (*NodeNavigator) MoveToParent

func (a *NodeNavigator) MoveToParent() bool

func (*NodeNavigator) MoveToPrevious

func (a *NodeNavigator) MoveToPrevious() bool

func (*NodeNavigator) MoveToRoot

func (a *NodeNavigator) MoveToRoot()

func (*NodeNavigator) NodeType

func (a *NodeNavigator) NodeType() xpath.NodeType

func (*NodeNavigator) Prefix

func (a *NodeNavigator) Prefix() string

func (*NodeNavigator) String

func (a *NodeNavigator) String() string

func (*NodeNavigator) Value

func (a *NodeNavigator) Value() string

type NodeType

type NodeType uint

A NodeType is the type of a Node.

const (
	// DocumentNode is a document object that, as the root of the document tree,
	// provides access to the entire XML document.
	DocumentNode NodeType = iota
	// ElementNode is an element.
	ElementNode
	// TextNode is the text content of a node.
	TextNode
)

Jump to

Keyboard shortcuts

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