Changes

Jump to navigation Jump to search
272 bytes added ,  16:31, 27 February 2020
no edit summary
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
let $database := "database"
for $name in file:list('.', false(), '*.json')
let $json := json:parse($file)
return db:add($database, $json, $name)
</presyntaxhighlight>
'''Example 2: Converts a simple JSON string to XML and back'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
json:parse('{}')
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<json type="object"/>
</presyntaxhighlight>
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
(: serialize result as plain text :)
declare option output:method 'text';
json:serialize(<json type="object"/>)
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xquery">
{ }
</presyntaxhighlight>
'''Example 3: Converts a JSON string with simple objects and arrays'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
json:parse('{
"title": "Talk On Travel Pool",
"generator": "http://www.flickr.com/"
}')
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<json type="object">
<title>Talk On Travel Pool</title>
<generator>http://www.flickr.com/</generator>
</json>
</presyntaxhighlight>
'''Example 4: Converts a JSON string with different data types'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
let $options := map { 'merge': true() }
return json:parse('{
]
}', $options)
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<json numbers="age code" arrays="phone" objects="json address value">
<first__name>John</first__name>
</phone>
</json>
</presyntaxhighlight>
==JsonML Format==
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
for $doc in collection('json')
let $name := document-uri($doc)
let $json := json:serialize($doc, map { 'format': 'jsonml' })
return file:write($name, $json)
</presyntaxhighlight>
'''Example 2: Converts an XML document with elements and text'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
json:serialize(doc('flickr.xml'), map { 'format': 'jsonml' })
</presyntaxhighlight>
'''flickr.xml:'''
<pre classsyntaxhighlight lang="brush:xml">
<flickr>
<title>Talk On Travel Pool</title>
<generator>http://www.flickr.com/</generator>
</flickr>
</presyntaxhighlight>
'''Result:'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
json:serialize(doc('input.xml'), map { 'format': 'jsonml' })
</presyntaxhighlight>
'''input.xml:'''
<pre classsyntaxhighlight lang="brush:xml">
<address id='1'>
<!-- comments will be discarded -->
<phone type='home'>212 555-1234</phone>
</address>
</presyntaxhighlight>
'''Result:'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
let $input := '{
"Title": "Drinks",
$k || ': ' || string-join($v, ', ')
})
</presyntaxhighlight>
'''Result:'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
for $item in (
true(),
map { 'format': 'xquery', 'indent': 'no' }
)
</presyntaxhighlight>
'''Result:'''
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu