JSON input files
Wednesday, 7 February 2018 · 2 messages · last reply 8 Feb 2018
JR
Jonathan Robie7 Feb 2018 22:39 #
I would like to add a JSON file to a collection that is otherwise composed of XML files. I'm hitting two issues:
1. Is there any way to import both JSON and XML files at the same time when creating a database?
2. I would like to import the JSON as an XQuery/XPath map, without mapping it to XML. Any way to do that?
Thanks!
Jonathan
P.S. the input JSON looks like this:
{
"ααρων": { "gk": 2,
"strongs": [
2
],
"lemma": "Ἀαρών",
"transliteration": "Aarōn",
"frequencyCount": 5,
"definition": "Aaron, pr. name, indecl., the brother of Moses (Exod. 4:14), Lk. 1:15; Acts 7:40; Heb. 5:4; 7:11; 9:4*"
},
When I import it, I get this:
<json type="object">
<ααρων type="object">
<gk type="number">2</gk>
<strongs type="array"> <_ type="number">2</_>
</strongs>
<lemma>Ἀαρών</lemma>
<transliteration>Aarōn</transliteration>
<frequencyCount type="number">5</frequencyCount>
<definition>Aaron, pr. name, indecl., the brother of Moses (Exod.
4:14), Lk. 1:15; Acts 7:40; Heb. 5:4; 7:11; 9:4*</definition>
</ααρων>
<αβαδδων type="object">
<gk type="number">3</gk>etc.
CG
Christian Grün8 Feb 2018 12:33 #
Hi Jonathan,
Quoted text · Jonathan Robie · 2 lines
> 1. Is there any way to import both JSON and XML files at the same time when > creating a database?
Currently, only one input parser is supported at the same time. You’ll either have to…
• import your data in various steps (a CREATE for all XML files, followed by an ADD for JSON files, and, optionally and eventually, OPTIMIZE), or
• use XQuery (
db:create, db:add, etc. [1], and possibly the File Module) to define and convert all input files in one go.Quoted text · Jonathan Robie · 2 lines
> 2. I would like to import the JSON as an XQuery/XPath map, without mapping > it to XML. Any way to do that?
There store data in the database in a representation different to XML (apart from our binary storage feature… But this one does not provide any other features, such as indexing). However, in many cases, it’s efficient enough to convert database entries to maps via
fn:serialize (or json:serialize) and fn:parse-json:
1. Example: document {
<json type="object">
<ααρων type="object">
<gk type="number">2</gk>
<strongs type="array">
<_ type="number">2</_>
</strongs>
<lemma>Ἀαρών</lemma>
</ααρων>
</json>
}
=> serialize(map { 'method': 'json' })
=> parse-json()2. Example:
declare function local:json-xml-to-map(
$json as document-node(element(json))
) as map(*) {
parse-json(json:serialize($json))
};
local:json-xml-to-map(db:open('db', 'my.json'))Quite obviously, if you don’t want to store data in the database, you can always use
fn:json-doc ;)
Hope this helps,
Christian
[1] http://docs.basex.org/wiki/Database_ModuleWritten for BaseX 8.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery