Changes

Jump to navigation Jump to search
516 bytes removed ,  18:38, 1 December 2023
m
Text replacement - "syntaxhighlight" to "pre"
|- valign="top"
| {{Code|format}}
| Specifies the format for converting JSON data([[#Conversion Formats|see above]]).| {{Code|direct}}, {{Code|attributes}}, {{Code|basic}}, {{Code|jsonml}}, {{Code|xquery}}
| {{Code|direct}}
| ''parse'', ''serialize''
|- valign="top"
| {{Code|lax}}
| Specifies if a [[Conversion Module#Keys|lax approach is conversion rules]] are used to convert QNames to JSON names.
| {{Code|yes}}, {{Code|no}}
| {{Code|no}}
| Indicates if whitespace should be added to the output with the aim of improving human legibility. If the parameter is set as in the query prolog, it overrides the {{Code|indent}} [[Serialization|serialization parameter]].
| {{Code|yes}}, {{Code|no}}
| {{Code|no}} {{Announce|Default changed with Version 10}}
| ''serialize''
|}
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|json:doc|$uri as xs:string|item()?}}<br/pre>{{Func|json:doc|( $uri href as xs:string, $options as map(*)?| := map { }) as item()?}}<br/pre>
|- valign="top"
| '''Summary'''
|Fetches the JSON document referred to by the given {{Code|$urihref}} and converts it to an XQuery value. The {{Code|$options}} argument can be used to control the way the input is converted.
|- valign="top"
| '''Errors'''
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|json:parse|$string as xs:string?|item()?}}<br/pre>{{Func|json:parse|( $string value as xs:string?, $options as map(*)?| := map { }) as item()?}}</pre>
|- valign="top"
| '''Summary'''
|Converts the JSON {{Code|$stringvalue}} to an XQuery value. If the input can be successfully parsed, it can be serialized back to the original JSON representation. The {{Code|$options}} argument can be used to control the way the input is converted.
|- valign="top"
| '''Errors'''
{| width='100%'
|- valign="top"
| width='120' | '''SignaturesSignature'''|{{Func|json:serialize|$input as item()?|xs:string}}<br/pre>{{Func|json:serialize|( $input as item()?, $options as map(*)?| := map { }) as xs:string}}</pre>
|- valign="top"
| '''Summary'''
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
let $database := "database"
for $name in file:list('.', false(), '*.json')
let $json := json:parse($file)
return db:add($database, $json, $name)
</syntaxhighlightpre>
'''Example 2: Converts a simple JSON string to XML and back'''
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
json:parse('{}')
</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="xml">
<json type="object"/>
</syntaxhighlightpre>
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
(: serialize result as plain text :)
declare option output:method 'text';
json:serialize(<json type="object"/>)
</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="'xquery"'>
{ }
</syntaxhighlightpre>
'''Example 3: Converts a JSON string with simple objects and arrays'''
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
json:parse('{
"title": "Talk On Travel Pool",
"generator": "http://www.flickr.com/"
}')
</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="xml">
<json type="object">
<title>Talk On Travel Pool</title>
<generator>http://www.flickr.com/</generator>
</json>
</syntaxhighlightpre>
'''Example 4: Converts a JSON string with different data types'''
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
let $options := map { 'merge': true() }
return json:parse('{
]
}', $options)
</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="xml">
<json numbers="age code" arrays="phone" objects="json address value">
<first__name>John</first__name>
</phone>
</json>
</syntaxhighlightpre>
==JsonML Format==
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
for $doc in collection('json')
let $name := document-uri($doc)
let $json := json:serialize($doc, map { 'format': 'jsonml' })
return file:write($name, $json)
</syntaxhighlightpre>
'''Example 2: Converts an XML document with elements and text'''
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
json:serialize(doc('flickr.xml'), map { 'format': 'jsonml' })
</syntaxhighlightpre>
'''flickr.xml:'''
<syntaxhighlight pre lang="xml">
<flickr>
<title>Talk On Travel Pool</title>
<generator>http://www.flickr.com/</generator>
</flickr>
</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="json">
["flickr",
["title",
["generator",
"http://www.flickr.com/"]]
</syntaxhighlightpre>
'''Example 3: Converts a document with nested elements and attributes to JsonML'''
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
json:serialize(doc('input.xml'), map { 'format': 'jsonml' })
</syntaxhighlightpre>
'''input.xml:'''
<syntaxhighlight pre lang="xml">
<address id='1'>
<!-- comments will be discarded -->
<phone type='home'>212 555-1234</phone>
</address>
</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="json">
["address", {"id":"1"},
["last_name",
["phone", {"type":"home"},
"212 555-1234"]]
</syntaxhighlightpre>
==XQuery Format==
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
let $input := '{
"Title": "Drinks",
$k || ': ' || string-join($v, ', ')
})
</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="json">
Author: Jim Daniels, Jack Beam
Title: Drinks
</syntaxhighlightpre>
'''Example 2: Converts XQuery data to JSON'''
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
for $item in (
true(),
map { 'format': 'xquery', 'indent': 'no' }
)
</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="json">
true
"ABC"
[1,2,3,4,5]
{"Key":"Value"}
</syntaxhighlightpre>
=Errors=
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu