Setting a JSON Object from an input field - json

I basically have the following flow:
XML -> JSON -> Spring MVC -> jsp page, which is displayed as a table with editable fields.
How do make is so that when i edit a field value it correct updates the Json Object?
I have used some nasty hacking at the moment, as i know (testing) the values/object I am going to get, so im just parsing the JSON string in javascript and sending that back.
So i can then just convert the json object (with new values) and post it back.
Cheers.

You could use the serialize method from prototypejs.
http://www.prototypejs.org/api/form/serialize
just by calling .serialize() you'll be able to get the updated object.

Related

Convert and Transform JSON HTTP request to XML

I need to create a Logic Apps workflow with three steps:
When HTTP Request is received (JSON)
Convert Json from request to XML
Save XML file to FTP
What I have done so far:
Add action "When HTTP Request is received"
Add Liquid to Convert JSON to XML
(but i don't see option JSON to XML...Only Tranform JSON to JSON, JSON to
TEXT, XML to JSON, XML to TEXT)
Add action "FTP - Create file"
I also created Integration Account and try to add map for mapping JSON to XML, but I can't find any examples/templates to do this...
Is it possible at all ? Maybe there is another way to convert between these two formats ?
When you just want to convert a JSON payload to an XML file, without doing any transformation to the data, you can use the built-in xml() function of the Workflow Definition Language.
Detailed info in the docs: Workflow Definition Language reference #xml
I've made a small test Logic App to demo your usecase. It looks like this:
As you can see I use the xml function on the triggerbody #xml(triggerBody()) as an input for my FTP file content.
Remark: This will only work if your JSON message has a single rootnode. Otherwise the xml conversion will fail. You'll get this error:
The provided value cannot be converted to XML: 'JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifying a DeserializeRootElementName.
You can work around that by concatenating a rootnode to your JSON payload. The function then would look like: #xml(json(concat('{\"rootnode\":',triggerBody(),'}')))
Good luck testing this out. Let me know if you need more help with this.

ReactJS validate JSON Object with JSON Schema

In my ReactJS app, I want to validate a JSON object coming from API. I have the valid JSON schema and I want to make sure the coming object is in the correct structure before passing it.
Is there any React plugin which I can use for this.
If you want to draw the UI based on the json object after schema validation, then react-json-schema-form is your friend

Pass JSON object vs JSON string in HTTP POST

I'm building a REST API in JAVA and C# and I was wondering about the way I should pass data to those services.
What I'm familiar with as the right way is to send JSON object as the data in the POST body:
{name:'Dor'}
but I can also pass a string and parse the JSON in my service:
'{name:'Dor'}'
What is the preferable way from performance factor? or any other factors?
Basically, if you need to send the json data across via jquery, then we need to use stringify, else the data would be serialized into to key=value pair.
So, you cannot send the json object directly via jquery ajax method.
How it works behind the hood:
In $.ajax function, if we provide data as
data :{key1:"value1", key2:"value2"}
is serialized to key1=value1&key2=value2
if we provide data as
data :'{key1:"value1", key2:"value2"}' or JSON.stringify({key1:"value1", key2:"value2"})
is sent as {key1:"value1", key2:"value2"}
So, what we can conclude is that, we cannot pass json object directly via jquery, we can send only json string. Hope this clarifies everyone.

How to get JSON output from Cloudant CouchDB?

I'm new to cloudant and still figuring out various APIs that it provides for access and retrieval.
So far, I've used findbyIndex using selector string which returns JSON as de-serialised object of the POJO supplied.
For a certain use case which I'm working- I need to get JSON only and not the de-seralised object. Is there any way or API from which I can get the actual cloudant doc as JSON only ?
If you know the ID of the document you want to read only the raw JSON, you can use the find method to get the raw inputstream from the request. Currently using the findByIndex method you can only get a deserialised java object back, so you could always use a Map object for your POJO. Although that being said it might be possible to pass the type String for the class to deserialise, however I have not tested this.

Tell modelbinding that MVC action parameter is JSON

I am using an upload control to send a file to a JsonResult, but I am also sending up a JSON string as a second parameter. This is all getting posted with the Content-Type:multipart/form-data;
[HttpPost]
public JsonResult UploadDocument(HttpPostedFileBase file, DocumentViewModel model)
{ ... }
I know MVC is capable of binding directly to a viewmodel if the content type is set to application/json but I don't think it's possible for me to set that in this case.
Is there any way for me to get MVC to automatically bind my posted json string to model?
That's not possible out-of-the-box. You will have to manually deserialize the JSON string parameter that you would read from the request to your view model inside the controller action or write a custom model binder for it that will do the job. Ideally you shouldn't be posting the model data as a JSON string but rather respect the content type you specified : multipart/form-data. So the correct way to handle this scenario is to modify the client code that is sending the request in order to respect the content type.
As I was unable to change the content-type I found this blog to be exactly what i needed.
"... our whole request stream(data) won’t be json string. Only the guest parameter will be supplied as json string..."
http://ishwor.cyberbudsonline.com/2012/07/fun-with-aspnet-mvc-3-custom-json-model-binder.html