JSON
To users of dynamic and/or scripting languages like Python, JavaScript, Ruby, Lua, and many more, the JavaScript Object Notation (or JSON) is almost second nature. It is a natural way of expressing integers, strings, lists, and mappings (a.ka. dictionaries, hashes, objects).
In JavaScript…
In JavaScript, the format actually runs! We can simply call eval():
Evaluating JSON strings:
var tree = eval('({"type": "addition", "children": [{"type": "integer", "val": 5}, {"type": "multiplication", "children": [{"type": "integer", "val": 3}, {"type": "integer", "val": 2}]}]})');
Just a javascript object:
{
"type": "addition",
"children": [
{
"type": "integer",
"val": 5
},
{
"type": "multiplication",
"children": [
{
"type": "integer",
"val": 3
},
{
"type": "integer",
"val": 2
}
]
}
]
}
However, for security reasons, json2.js is preferred.
Navigating JSON structures
Keep in mind that in JavaScript at least, navigating a JSON structure is the same as navigating a pile of objects. It can be a little tedious. We’re really just chasing references down the structure that we’ve defined. For example:
tree.children[1].children[0].type
It’s just javascript, but we’ll see that finding items in trees is a common problem. Now let’s look at some techniques for doing this in XML.
XPath
- Book Slides http://www.brics.dk/ixwt/xpath.pdf
- Suggested Reading: Chapter 3
XML & XPath Tools
- http://stackoverflow.com/questions/15461737/how-to-execute-xpath-one-liners-from-shell
xmllintvia thelibxml2-utilspackage on linuxxmllintincluded by default in mac os x or installed via thelibxml2homebrew packagexmllintvia http://xmlsoft.org/sources/win32/ on windows
- http://www.xpathtester.com/xpath