Loading JSON from Java API from URL: better call json-doc system function or use JsonBuilder?
Added by Martin Honnen 12 months ago
Looking at Saxon 11 and/or 12 Java, I wonder whether I am overlooking a way to load a JSON document from a URL; it seems JsonBuilder, contrary to DocumentBuilder, has no easy option to pass in a URL to load some JSON from.
I wonder whether I am better off to try to call fn:json-doc
as a system function with the URL as the argument instead of trying to construct an Reader off the response stream of a URLConnection.
Any thoughts or hints whether I am overlooking something appreciated.
Replies (2)
RE: Loading JSON from Java API from URL: better call json-doc system function or use JsonBuilder? - Added by Martin Honnen 12 months ago
I have looked through other parts of the source code whether there might be an example loading JSON over HTTP(S) or a URL, kind of expected or hoped to see that done in Query.java and/or Transform.java with -u
(-u Indicates that the names of the source document and the stylesheet document are URLs
) but it seems -json
doesn't support -u
.
So my question remains, is using fn:json-doc
as a system function the easiest approach to load a JSON document from Java over HTTP(S), to get an XdmValue?
RE: Loading JSON from Java API from URL: better call json-doc system function or use JsonBuilder? - Added by Michael Kay 12 months ago
I would have thought the simplest approach was to get a URLConnection from the URI, get an InputStream from the URLConnection, wrap a Reader around this, and pass the Reader to the JsonBuilder. Yes, we could have made that easier; and yes, an alternative is to call json-doc() as an XdmFunction.
Please register to reply