This module contains functions for working with JSON data. You can use it to process JSON that is embedded in other file formats. For example, you can query JSON that is stored as lines in a large text file by using json:parse-as-xml
with the text:collection
function.
Processing large JSON files in parallel is not currently supported.
To use the built-in functions in your query, you must import the JSON module as follows:
import module "oxh:json";
The JSON module contains the following functions:
Parses a JSON value as XML.
About Converting JSON Objects to XML
JSON objects are similar to Avro maps and are converted to the same XML structure. See "Reading Maps."
For example, the following JSON object is converted to an XML element:
{ "user" : "john", "full_name" : "John Doe", "age" : 45 }
The object is modeled as the following element:
<oxh:item> <oxh:entry key="user">john</oxh:entry> <oxh:entry key="full_name">John Doe</oxh:entry> <oxh:entry key="age">45</oxh:entry> </oxh:item>
About Converting JSON Arrays to XML
JSON arrays are similar to Avro arrays and are converted to the same XML structure. See "Reading Arrays."
For example, the following JSON array is converted to an XML element:
[ "red", "blue", "green" ]
The array is modeled as the following element:
<oxh:item> <oxh:item>red</oxh:item> <oxh:item>blue</oxh:item> <oxh:item>green</oxh:item> </oxh:item>
About Converting Other JSON Types
The other JSON values are mapped as follows:
Retrieves an entry from a JSON object modeled as XML.
json:get($key as xs:string?, $obj as node()?) as element(oxh:entry)?
Or
json:get($key as xs:string?) as element(oxh:entry)?
The value of the following XPath expression:
$obj/oxh:entry[@key eq $key]
If $input
not present, the behavior is identical to calling the two-argument function using the context item for $obj
. See the Notes.
The following are equivalent:
$var/json:get("key") json:get("key", $var) $var/oxh:entry[@key eq "key"]
$var
is a JSON object modeled as XML. See "Reading Maps."
These examples query the following text file in HDFS.
mydata/users-json.txt { "user" : "john", "full name" : "John Doe", "age" : 45 } { "user" : "kelly", "full name" : "Kelly Johnson", "age" : 32 } { "user" : "laura", "full name" : "Laura Smith", "age" : null } { "user" : "phil", "full name" : "Phil Johnson", "age" : 27 }
The following query selects the names of users that are older than 30:
import module "oxh:text"; import module "oxh:json"; for $line in text:collection("mydata/users-json.txt") let $user := json:parse-as-xml($line) where $user/json:get("age") gt 30 return text:put-text($user/json:get("full name"))
This query generates text files that contain the following lines:
John Doe Kelly Johnson
The next query selects the names of employees that have a null age value:
import module "oxh:text"; import module "oxh:json"; for $line in text:collection("mydata/users-json.txt") let $user := json:parse-as-xml($line) where $user/json:get("age")/nilled() return text:put-text($user/json:get("full name"))
This query generates a text file that contains the following line:
Laura Smith