MarkLogic REST API - JSON Response - json

I'm using the MarkLogic 7 REST API to build an AngularJS application on top of an XML document database. My documents are in NEWSML-G2 format. I've configured custom query options to return only the title and the creation date for each document that matches the search string. The problem is this.
I have a title element in my XML.
<nitf version="-//IPTC//DTD NITF 3.6//EN" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:newzmeta="http://newz.nl/metadata/" xmlns="http://iptc.org/std/NITF/2006-10-18/">
<head>
<title newz:origin="Generated" xmlns:newz="http://newz.nl/">Test Article</title>
....
</nitf>
In my custom query options, I've used the following line.
<extract-metadata>
<qname elem-ns="http://iptc.org/std/NITF/2006-10-18/" elem-name="title"/>
....
</extract-metadata>
Now, the problem is that when I hit the REST interface to make a query and ask for results in JSON format, I get the following in the response
"metadata":[{"{http://iptc.org/std/NITF/2006-10-18/}title":"Obama assumes the office of the President of the United States","metadata-type":"element"},{" {http://iptc.org/std/nar/2006-10-01/}firstCreated":"2009-01-20T05:00:09","metadata-type":"element"}]
How do I retrieve the title value on the UI side? I get a syntax error if I ask for
result.metadata.{http://iptc.org/std/NITF/2006-10-18/}title
Is there a way specifically to access this value, or to somehow change the name of the element that's returned from the MarkLogic side?

You can do this:
result.metadata.['{http://iptc.org/std/NITF/2006-10-18/}title']
The bracket notation is used to access properties whose names are not valid JavaScript identifiers, preventing you from using the dot notation.

For completeness, an alternative approach would be to write and install a transform for the search response that generates either metadata elements in the empty namespace or a JSON representation that's tuned to your requirements.
But, the simplest way is just to use the bracket notation as Dave suggests.

Related

Should I be using JSON or XML when the order of the properties, of the server-returned object, matters?

Client uses ReactJS and server is using Node, Express and MySQL for the db. I'm making a website that allows users to post news articles, e.g. long bodies of text with images inserted at random points inside them. Think of a website like Medium that allows users to write blog posts and insert images at any point.
My issue here lies with how I am to store the information for each article. Originally, I was going to use XML - use and tags and represent each article as an XML document. This way, when the server parses the XML document after pulling it from the DB, the order of the elements is preserved.
For example:
<<text> /* long body of text (e.g. about 300 - 500 words?) */ </text>
<img> First image path goes here </img>
<text> /* Another body of text! */ </text>
<img> Some cool graph that's relevant for the article </img>
When my server tries to parse this XML document, the order is very important. The contents of the first text tag (line 1 in the code sample above) must be parsed first. Then below that should be the first image (line 2). Once the full XML document is parsed, it should be sent to the client, where React will iterate through the returned object and, create a paragraph or image ReactElement for each object.
I use JSON for most things to represent the objects that client and server exchange. But, I am quite aware that within JSON objects, the order of the keys {key: value}, is not preserved. Therefore, it would be possible that (referring to my silly code snippet above), line 2 could be added to my VirtualDOM before line 1, making the outputted order incorrect.
Therefore, should I be using JSON or XML as 1) the format of the object that the server returns and 2) the representation of each article.
Both XML and JSON have the ability to represent data in a way that preserves order. The nearest JSON equivalent to your XML might be:
[
{"text":"long body of text (e.g. about 300 - 500 words?)"},
{"img":"First image path goes here"},
{"text":"Another body of text!"},
{"img":"Some cool graph that's relevant for the article"}
]
Because order matters, you have to use an array here.
You could argue that this structure looks pretty strange, and you would be right. JSON was never designed for representing document structures. I would strongly question the wisdom of using JSON to represent free-format articles in the way you are proposing. There are some things JSON does better than XML, and there are other things XML does better than JSON, and your application is firmly in the second category.
MySQL's JSON data type does not preserve the order however you can store it as a string which will save idempotency. But you lose the ability to use the MySQL JSON functions, which you might be able to live with if your processing is mainly in your application.

Jmeter - What is the best extractor to use on a json message?

Currently testing system where the output is in the form of formatted json.
As part of my tests I need to extract and validate two values from the json record.
The values both have individual identifiers on them but don't appear in the same part of the record, so I can't just grab a single long string.
Loose format of the information in both cases:
"identifier1": [{"identifier2":"idname","values":["bit_I_want!]}]
In the case of the bit I want, this can either be a single quoted value (e.g. "12345") or multiple quoted values (e.g. "12345","23456","98765").
In both cases I'm only interested in validating the whole string of values, not individual values from the set.
Can anyone recommend which of the various extractors in Jmeter would be best to achieve this?
Many Thanks!
The most obvious choicse seems to be JSON Path Assertion (available via JMeter Plugins), it allows not only executing arbitrary JSON queries but conditionally failing the sampler basing on actual and expected result match.
The recommended way of installing JMeter Plugins and keeping them up-to-date is using JMeter Plugins Manager
JMeter 3.1 comes with JSON Extractor to parse JSON response. you could use this expression $.identifier1[0].values
as the JSON Path to extract the values.
If your JSON response is going to simple always as shown in your question, you could use Regular Expression Extractor as well. Advantage is it is faster than JSON extractor. The regular expression would be "values":\[(.*?)\]
Reference: http://www.testautomationguru.com/jmeter-response-data-extractors-comparison/

Specifiy type when converting from XML to JSON in MarkLogic

Using MarkLogic 8, I'm using a custom XML to JSON conversion for json:transform-to-json, and I've got it working just about right except the conversion is outputting numbers as strings.
Is there a way to specify that the value of a particular element should be a number value, not a string?
I don't see anything in the doc for json:config, but just in case there's something I've missed, or if you have a neat post-processing trick, I'd love to hear about how to solve this problem.
You can do that by defining an XML Schema for the non-string type elements. Just make sure it is available in the context (by loading it into xdmp:schemas-database()), and that it is recognized (your XML needs to have a namespace that matches the XML Schema, and you might wanna use import schema)..
HTH!

Docusign Prefilling fields with Java and XML does not appear to work

I'm trying to send an envelope from a template using the REST API. I'm using Java with XML since the Java example is given with XML only. Here:
http://iodocs.docusign.com/APIWalkthrough/requestSignatureFromTemplate
My template is very simple. It has:
1. A data field called Material1
2. A data field called Quantity1
3. A Full Name field
4. Signature field
5. Date Signed field
Here's the screen shot:
The Java code I'm using is exactly as it appears in the API Walkthrough link that I provided above. The XML that I supply is:
<envelopeDefinition xmlns="http://www.docusign.com/restapi">
<accountId>ZZZZZZZZZZ</accountId>
<status>sent</status>
<emailSubject>DocuSign API Call - Signature request from template
</emailSubject>
<templateId>1886EC14-153E-4E05-AFF8-04F508098E60</templateId>
<templateRoles>
<templateRole>
<name>Michael</name>
<email>michael#company.com</email>
<roleName>Signer1</roleName>
<tabs>
<textTabs>
<text>
<tabLabel>\*Material1</tabLabel>
<value>MTX80HD</value>
</text>
<text>
<tabLabel>\*Quantity1</tabLabel>
<value>11</value>
</text>
</textTabs>
</tabs>
</templateRole>
</templateRoles>
</envelopeDefinition>
However the value of MTX80HD is not being prefilled in the Material1 Field, nor do I see 11 in the Quantity1.
I've read multiple posts here and followed every suggestion that I have found but still can't get the pre population to work. The Full Name and Date Sign are filled in however.
TIA
Edit 1:
OK. I converted the XML to JSON, as #ergin suggested below and the fields are still not prepopulated. So the issue must lie elsewhere and not with XML.
Here's the JSON I'm sending:
{"account":"MyAccountId","status":"sent","emailSubject":"DocuSign API Call - Test signature request from template","templateId":"1886EC14-153E-4E05-AFF8-04F508098E60","templateRoles":[{"name":"Michael","email":"michael#company.com","roleName":"Signer1","tabs":{"textTabs":[{"tabLabel":"Material1","value":"MTX80HD"}]}}]}
The URL I'm sending the above JSON to is:
https://demo.docusign.net/restapi/v2/accounts/MyAccountId/envelopes
The fields I'm trying to populate with the tabs were created on the template as Data Field widgets.
I must be missing something obvious, as I see people posting that they got it to work, eventually.
Hope to hear from anyone who has any ideas.
Thanks.
Do you have multiple tabs that are using the same tabLabel or just one? You only need the escape sequence when you have multiple, however even then you need an extra backslash (). You have this
<tabLabel>\*Material1</tabLabel>
But it should be this instead:
<tabLabel>\\*Material1</tabLabel>
However if you only have one tab with the name Material1 then don't use \\* at all.
Also, if you want to use JSON instead you can look at some of the other API walkthroughs in other languages that show JSON, or you can use the auto-generated REST API help page:
https://www.docusign.net/restapi/help

Insert a Coldfusion struct into a database

If I wanted to save a contact form submission to the database, how can I insert the form scope in as the submission? It's been some time since I used Coldfusion.
The contact forms vary depending on what part of the site it was submitted from, so it needs to scale and handle a form with 5 fields or one with 10 fields. I just want to store the data in a blob table.
Most space efficient way and least complicated to turn back into original shape is using serializeJSON. After that, you can use something like key:value|key:value, or XML representation of your struct.
Cfwddx is also an alternative.
I don't know that there is a way to store a native structure into a database, but have you thought about using JSON to represent your object as key-pair values and then parsing it into a native structure after retrieving it from the database?
There are tags/functions out there that will help you with the encoding and decoding into JSON:
cfJSON Tag
CF8 Serialize JSON
CF8 Deserialize JSON
If you can't normalize the form fields into proper table(s), you can try storing them:
in XML (SQL Server supports XML pretty well), or
in JSON (in a plain varchar field), or
ObjectLoad() & ObjectSave() (CF9 only) to store as blob.
IIRC there are ways to get object load/save functionality in pre-CF9 by tapping into Java. http://www.riaforge.org/ or http://cflib.org/ might have it.