JSON Module (2024)

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.

Built-in Functions for Reading JSON

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:

  • json:parse-as-xml

  • json:get

json:parse-as-xml

Parses a JSON value as XML.

Signature

json:parse-as-xml($arg as xs:string?) as element(*)?

Returns

An XML element that models the JSON value. An empty sequence if $arg is an empty sequence.

Notes

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:

Table 6-4 JSON Type Conversions

JSONXML

null

An empty (nilled) element

true/false

xs:boolean

number

xs:decimal

string

xs:string


json:get

Retrieves an entry from a JSON object modeled as XML.

Signature

json:get($key as xs:string?, $obj as node()?) as element(oxh:entry)?

Or

json:get($key as xs:string?) as element(oxh:entry)?

Parameters

$key

The JSON data key.

$obj

The JSON object value.

Returns

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.

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."

Examples of JSON Functions

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 }
Example 1

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 30return text:put-text($user/json:get("full name"))

This query generates text files that contain the following lines:

John DoeKelly Johnson
Example 2

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
JSON Module (2024)
Top Articles
Axis Bank-Citibank merger complete: What will happen to your Citi credit card, Citi debit card, savings, NRE account, home loan?
Account with Card - FAQ
Great Clips Mount Airy Nc
Urist Mcenforcer
Google Jobs Denver
When is streaming illegal? What you need to know about pirated content
Craigslist Furniture Bedroom Set
Tap Tap Run Coupon Codes
Katie Boyle Dancer Biography
Alaska Bücher in der richtigen Reihenfolge
Transfer Credits Uncc
Gma Deals And Steals Today 2022
Huge Boobs Images
No Hard Feelings Showtimes Near Cinemark At Harlingen
Gon Deer Forum
Houses and Apartments For Rent in Maastricht
Arre St Wv Srj
Watch The Lovely Bones Online Free 123Movies
U Arizona Phonebook
Jbf Wichita Falls
Huntersville Town Billboards
Today Was A Good Day With Lyrics
Spn 520211
Contracts for May 28, 2020
Yog-Sothoth
Betaalbaar naar The Big Apple: 9 x tips voor New York City
Culver's.comsummerofsmiles
Access a Shared Resource | Computing for Arts + Sciences
Danielle Ranslow Obituary
Xpanas Indo
By.association.only - Watsonville - Book Online - Prices, Reviews, Photos
100 Million Naira In Dollars
Was heißt AMK? » Bedeutung und Herkunft des Ausdrucks
Cbs Trade Value Chart Week 10
Www.craigslist.com Syracuse Ny
Envy Nails Snoqualmie
What Are Digital Kitchens & How Can They Work for Foodservice
Dallas City Council Agenda
Mckinley rugzak - Mode accessoires kopen? Ruime keuze
Dollar Tree's 1,000 store closure tells the perils of poor acquisitions
Kelley Blue Book Recalls
Leena Snoubar Net Worth
Vindy.com Obituaries
Inducement Small Bribe
814-747-6702
Citymd West 146Th Urgent Care - Nyc Photos
Pgecom
John Wick: Kapitel 4 (2023)
Wwba Baseball
Mast Greenhouse Windsor Mo
Obituaries in Westchester, NY | The Journal News
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 6198

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.