File: jpath.go

package info (click to toggle)
golang-github-clbanning-mxj 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,200 kB
  • sloc: xml: 176; makefile: 4
file content (202 lines) | stat: -rw-r--r-- 5,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// gitissue #28

/*
(reference: http://goessner.net/articles/JsonPath/)
Let's practice JSONPath expressions by some more examples. We start with a simple JSON structure built after an XML example representing a bookstore (original XML file).

{ "store": {
    "book": [
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}
XPath                JSONPath                 Result
/store/book/author   $.store.book[*].author   the authors of all books in the store
//author             $..author                all authors
/store/*             $.store.*                all things in store, which are some books and a red bicycle.
/store//price        $.store..price           the price of everything in the store.
//book[3]            $..book[2]               the third book
//book[last()]       $..book[(@.length-1)]
                     $..book[-1:]             the last book in order.
//book[position()<3] $..book[0,1]
                     $..book[:2]              the first two books
//book[isbn]         $..book[?(@.isbn)]       filter all books with isbn number
//book[price<10]     $..book[?(@.price<10)]   filter all books cheapier than 10
//*                  $..*                     all Elements in XML document. All members of JSON structure.

*/

package main

import (
	"fmt"

	"github.com/clbanning/mxj/v2"
)

var data = []byte(`
{ "store": {
    "book": [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}`)

func main() {
	m, err := mxj.NewMapJson(data)
	if err != nil {
		fmt.Println("NewMapJson err:", err)
		return
	}

	// $.store.book[*].author   the authors of all books in the store
	list, err := m.ValuesForPath("store.book.author")
	if err != nil {
		fmt.Println("book author err:", err)
		return
	}
	fmt.Println("authors:", list)

	// $..author                all authors
	list, err = m.ValuesForKey("author")
	if err != nil {
		fmt.Println("author err:", err)
		return
	}
	fmt.Println("authors:", list)

	// $.store.*                all things in store, which are some books and a red bicycle.
	list, err = m.ValuesForKey("store")
	if err != nil {
		fmt.Println("store things err:", err)
	}
	fmt.Println("store things:", list)

	// /store//price        $.store..price           the price of everything in the store.
	list, err = m.ValuesForKey("price")
	if err != nil {
		fmt.Println("price of things err:", err)
	}
	fmt.Println("price of things:", list)

	// $..book[2]               the third book
	v, err := m.ValueForPath("store.book[2]")
	if err != nil {
		fmt.Println("price of things err:", err)
	}
	fmt.Println("3rd book:", v)

	// $..book[-1:]             the last book in order
	list, err = m.ValuesForPath("store.book")
	if err != nil {
		fmt.Println("list of books err:", err)
	}
	if len(list) <= 1 {
		fmt.Println("last book:", list)
	} else {
		fmt.Println("last book:", list[len(list)-1:])
	}

	// $..book[:2]              the first two books
	list, err = m.ValuesForPath("store.book")
	if err != nil {
		fmt.Println("list of books err:", err)
	}
	if len(list) <= 2 {
		fmt.Println("1st 2 books:", list)
	} else {
		fmt.Println("1st 2 books:", list[:2])
	}

	// $..book[?(@.isbn)]       filter all books with isbn number
	list, err = m.ValuesForPath("store.book", "isbn:*")
	if err != nil {
		fmt.Println("list of books err:", err)
	}
	fmt.Println("books with isbn:", list)

	// $..book[?(@.price<10)]   filter all books cheapier than 10
	list, err = m.ValuesForPath("store.book")
	if err != nil {
		fmt.Println("list of books err:", err)
	}
	var n int
	for _, v := range list {
		if v.(map[string]interface{})["price"].(float64) >= 10.0 {
			continue
		}
		list[n] = v
		n++
	}
	list = list[:n]
	fmt.Println("books with price < $10:", list)

	// $..*                     all Elements in XML document. All members of JSON structure.
	// 1st where values are not complex elements
	list = m.LeafValues()
	fmt.Println("list of leaf values:", list)

	// $..*                     all Elements in XML document. All members of JSON structure.
	// 2nd every value - even complex elements
	path := "*"
	list = make([]interface{}, 0)
	for {
		v, _ := m.ValuesForPath(path)
		if len(v) == 0 {
			break
		}
		list = append(list, v...)
		path = path + ".*"
	}
	fmt.Println("list of all values:", list)
}