HTML::Template Perl - html

IIs there a package similar to HTml::Template in perl which takes a JSON object and maps it to a HTML template file? I am building a web application using HTML::Template and will be receiving JSOn from a web services API, things will be made simple if I can templatize this JSOn to HTML instead of doing it the exact way HTML::Template requires.

HTML::Template just takes a data structure consisting of strings, hashes and arrays. JSON maps directly onto that.
$template->param(myData => JSON::Any->new->decode($json_string));

HTML::Template is a rather 'simple' templating engine - I am using quotes because its simplicity let's you do whatever you need in a view part from the Model View Controller architecture.
However, you can not execute arbitrary perl code inside a HTML::Template.
Also, due to the fact that in JSON you could have very complex data structures, I doubt that there are any suitable ways of using JSON in a straight way in your templates.
The only solution I see as possible is for you to use a Perl script that will parse the JSON, create some 'objects' and pass them to your templates. You already have that perl script - is the one that instantiate your HTML::Template object.

ok, a bit late, but:
HTML::Template always wants a hash of arrays of hashes and so on.
and you cannot navigate through the parameter stash.
If you want to do this, you might try out HTML::Template::Compiled which allows you to do this.
<tmpl_var some_hash.key.another_key[23] >
or with alternative delimiters:
[%= some_hash.key.another_key[23] %]
but note the documented differences of the module to HTML::Template.
So you decode your JSON string to a data structure and pass it to the template and then you can access all values somewhere deep in the structure.

Related

How can I query JSON of an XML parsing in a client only app?

Sorry for the inappropriate question. But what do you recommend me to use to structure a library that can put a query arrangement on json formats generated by an XML parsing based on TEI p5? I tried to use GraphQL by converting the interfaces of my Angular application related to parsing information from XML to JSON in type to define a GraphQL schema but I don't think that's the way.
What I have to do is query, client only, some data encoded in XML (also wanting already parsed in JSON) and, for example, search for all occurrences of a specific data.
Do you have any roadmaps to recommend or some JSON query system that might be right for me?
You might take a look at https://www.npmjs.com/package/saxon-js. With SaxonJS you're able to run XPath expression against XML using JavaScript.

How to handle JSON body using Tornado w/ python3

Tornado 4.5.2 using Python3 represents the request body as a byte object instead of a native dictionary. This presents a problem for methods like RequestHandler.get_body_argument() which will not access the field correctly.
My question is how to correctly have tornado parse these bodies into more useable dictionaries so the standard library will work. I've looked throughout tornado's documentation and there's next to nothing on even the existence of this problem.
Am I missing something here or will I need to re-implement those methods myself?
Tornado never automatically parses JSON; it only automatically parses HTML-standard form encoding (the data models of form encoding and JSON are different, so it wouldn't make sense to use the same family of get_argument/get_arguments methods in the less-ambiguous JSON format). If you want to handle JSON requests, it's one line to parse it yourself:
args = tornado.escape.json_decode(self.request.body)

XML To JSON and Vice Versa

Team,
This has been a problem in apps-script development for me. I have been trying to solve it for quite some time.
The XML appscript service I have been using (now deprecated) has been doing excellent, I mean really awesome JOB of parsing the any html file or XML file with the linient flag turned of. I am in love with it.
Also, this service gives you a JSON view of XML file. that is really cool. because I can simply browse to any data item in the debugger, and then type that in code. No need to call many sensless methods when you know exactly what your code is looking for.
These AWESOME features in deprecated XML service make the job a lot easier than the so called XMLService which it replaces.
One issue, I did find with XML service is it is slow. I wrote a parser that does it within 10 seconds that XML Service takes 3 minutes. So, sure it is bad.
But, XMLService does not have those neat features.
IS there any public code or libraries by anyone that does a beautiful job of:
a. Converting XML to JSON view.
b. Parse HTML in linient form.
c. Do it fast
Please help.
Ok, I didn't receive any update on the matter. So, I have written it myself. I simply convert XML to JSON.
Xml2Json will represent an XML string as a JSON object.
If you set flags="attributes", then the element attributes are packaged in XmlAttributes child of the JSONobject.
In XML, there is no way to say, you are about to encounter an array of elements or a single element. So, the parser by default creates a JSON object out of an element unless it counters another xml element with same name, then it converts the Object into an Array.
Here is the Xml2Json Project Key: 18Aji4ggm4A2cXQ1n_sSc6gDQA3Wc4aC5WvQYybAflkdUushu9f2Ogv5o

Returning MarkLogic EVAL REST service output as JSON

I am working on a demo using MarkLogic to store emails exported from Outlook as XML, so that they stay searchable and accessible when I move away from Outlook.
I am using an AngularJS front-end calling either the native MarkLogic REST services of own REST services written in JAVA using Jersey.
MarkLogic SEARCH REST service works very well to get back a list of references to documents based on various search criteria, but I also want to display information stored inside the found documents.
I would like to avoid multiple REST calls and to get back only the needed information, so I am trying to use the EVAL REST service to run an xQuery.
It works well to get XML back (inside a multipart/mixed message) but I don't seem to be able to get JSON instead which would be much more convenient and is very easy with most other MarkLogic REST services.
I could use "json:transform-to-json()" in my xQuery or transform the XML to JSON in my JAVA code, but that does not look very elegant to me.
Is there a more efficient method to get where I am trying to go ?
First, json:transform-to-json seems plenty elegant to me. But of course it's not always the right answer.
I see three options you haven't mentioned.
server-side transforms - REST search supports server-side transforms which transform each document when you perform a bulk read by query. Those server-side transforms could generate any json you need.
search extract-document-data - this the simplest way to extract portions of documents. But it seems best if your documents are json to match your json response. Otherwise you get xml in your json response . . . unless you're ok with that.
custom search snippets - another very powerful way to customize what search returns
All of these options don't require the privileges that eval requires, which is a very good thing. Since eval allows execution of arbitrary code on your server, it requires special privileges and should be used with great care. Two other options before you use eval are (1) custom xquery installed in an http server, and (2) REST extensions.
The answers from Sam are what I would suggest. Specifically I would set a search option for search-extract-document-data (This is a search API option. If you are posting the request, then you can add the option in the XML you post back. If you are using GET, then you need to register the option ahead of time and call it. Relevant URLs to assist:
https://docs.marklogic.com/guide/rest-dev/search#id_48838
https://docs.marklogic.com/guide/search-dev/appendixa#id_44222
As for json.. ML8 will transform content. Use the accept-header or just add format=json to your results...
Example - xml which is what my content is stored as:
http://localhost:8000/v1/search?q=watermellon
...
<search:result index="1" uri="/sample/collections/1.xml" path="fn:doc("/sample/collections/1.xml")" score="34816" confidence="0.5982239" fitness="0.6966695" href="/v1/documents?uri=%2Fsample%2Fcollections%2F1.xml" mimetype="application/xml" format="xml">
<search:snippet>
<search:match path="fn:doc("/sample/collections/1.xml")/x">
<search:highlight>watermellon</search:highlight>
</search:match>
</search:snippet>
</search:result>
...
Example - json which is what my content is stored as:
http://localhost:8000/v1/search?q=watermellon&format=json
...
"index":1,
"uri":"/sample/collections/1.xml",
"path":"fn:doc(\"/sample/collections/1.xml\")",
"score":34816,
"confidence":0.5982239,
"fitness":0.6966695,
"href":"/v1/documents?uri=%2Fsample%2Fcollections%2F1.xml",
"mimetype":"application/xml",
"format":"xml",
"matches":[
{
"path":"fn:doc(\"/sample/collections/1.xml\")/x",
"match-text":[
{
"highlight":"watermellon"
}
]
}
]
}
...
For real heavy-lifting, you can use server-side transforms as in Sam's description. One note about this. Server-side transformations are not part of the search API, but part of the REST API. Just mentioning it so you have some idea of which tool you are using in each case..

Clojure name binding to REST JSON data

I'm a Frontend Engineer, our team is switching many of our old services to micro services written in clojure. The main issue I'm seeing is that clojure naming conventions prefer hyphens to-separate-words in variable names. This means if you straight map variables into JSON any JS consumer would need to access this data using bracket notation e.g. response['to-separate-words']. This is obviously not ideal. I thought this would be a easy best practice to lookup but I've been looking for an hour and it seems like all the docs I read avoid this issue but using single words. Has anyone else dealt with this.
You might use camel-snake-kebab library which supports most of the combinations. You can plug it in into most of the JSON libraries for Clojure (cheshire, cli-json, data.json - as mentioned by Elogent) as they usually have an option to provide a function for handling property name mangling.
For example with cheshire:
Generate JSON with camel case property names:
(cheshire.core/generate-string {:my-clojure-key "abc"}
{:key-fn camel-snake-kebab.core/->camelCaseString})
Result:
{"myClojureKey":"abc"}
Parse JSON to get map with kebab case keys:
(cheshire.core/parse-string "{\"myClojureKey\":\"abc\"}"
camel-snake-kebab.core/->kebab-case-keyword)
Result:
{:my-clojure-key "abc"}
There is also an example for data.json in camel-snake-kebab readme.