-1

I need to make a school project in Oxygen, using the following API: https://potterapi.com/ I wrote a function to get all the necessary JSON data from the API:

let $apikey := fn:doc("potterapi.key")/apikey/string() return map { "characters": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey), "houses": fn:json-doc("https://www.potterapi.com/v1/houses?key=" || $apikey), "spells": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey) }

I also made an another xml scheme where I define the apikey, and i need to get the data in json format. Can you please help me with this?

  • Where exactly are you stuck? You said your code gets all the necessary JSON data, is the only problem left to serialize it as JSON? Then use `serialize(let $apikey := fn:doc("potterapi.key")/apikey/string() return map { "characters": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey), "houses": fn:json-doc("https://www.potterapi.com/v1/houses?key=" || $apikey), "spells": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey) }, map { 'method' : 'json', 'indent' : true() })`. – Martin Honnen Dec 14 '19 at 10:16
  • Yes, the code should get the necessary data, but i don't know how to "set up" oxygen to get the output as a JSON file. – Zoltan Bilcsik Dec 14 '19 at 10:23

1 Answers1

0

To serialize an XQuery 3.1 map as JSON with XQuery 3.1 you have two options, either use the serialize function

serialize(
  let $apikey := fn:doc("potterapi.key")/apikey/string() return map { "characters": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey), "houses": fn:json-doc("https://www.potterapi.com/v1/houses?key=" || $apikey), "spells": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey) },
  map { 'method' : 'json', 'indent' : true() }
)

or use the needed XQuery option declarations

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method 'json';
declare option output:indent 'yes';

let $apikey := fn:doc("potterapi.key")/apikey/string() return map { "characters": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey), "houses": fn:json-doc("https://www.potterapi.com/v1/houses?key=" || $apikey), "spells": fn:json-doc("https://www.potterapi.com/v1/characters?key=" || $apikey) }
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thanks you Martin, but what should i do with the apikey? Do i have to create an XML file with the apikey in it and make a transformation? – Zoltan Bilcsik Dec 14 '19 at 11:12
  • I thought you had created an XML file named `potterapi.key` with an element `...` that contains your key(instead of the `...`) and that you are reading in with `let $apikey := fn:doc("potterapi.key")/apikey/string()`. – Martin Honnen Dec 14 '19 at 11:20
  • Yes, that's right. But when I Want to make the transformation, the Oxygen gives me some kind of Saxon error. – Zoltan Bilcsik Dec 14 '19 at 11:23
  • Well, then edit your question and describe exactly what you are doing in oXygen, which version of it you use, which version of Saxon you have selected, and of course, which error exactly you get. – Martin Honnen Dec 14 '19 at 11:33
  • Hey Martin, i made it. I don't know why but it did not worked with the serialize function, but it worked with the XQuery option declaration. Thank you again, it helped me a lot ! – Zoltan Bilcsik Dec 14 '19 at 19:38
  • Hey Martin, sorry for bothering you, are you familiar with JSON Scheme? – Zoltan Bilcsik Dec 15 '19 at 20:08
  • @ZoltanBilcsik, no, I am not. – Martin Honnen Dec 15 '19 at 20:12