Sending Json looking formatted plain text to be de-serialized - json

If you send plain Json formatted un-serialized text from a web api and have it be de-serialized in a C# function using the jsonConvert.DeserializeObject functional? I have a co-worker who created a web api in the cloud and he sends plain text formatted to look like un-serialized Json which I try to use C# functionality to Deserialize but when I try to convert what is sent into Model classes it fails. I am telling him that the C# JsonConvert.SerializeObject must be used or it won't work. Can someone help clarify this with me?

Your colleague is right. JSON strings need to be de-serialized to turn them into objects.
Moreover, the things you call "plain text formatted to look like JSON" are JSON strings. JSON is plain text (in UTF8, with syntax rules).
Let's say you have some data structure in your program that you want to send over the network. The network can only send series of bytes, so whatever your structure was, you need to turn it into that -- you need to serialize it. JSON is one way of doing that, e.g.
'{"example": "some data"}'
is a string containing JSON. It is serialized, it's just a string of bytes to send over the network.
On the receiving end, you need to deserialize it back into some data structure, some type of hash map or dictionary or whatever it's called in C# probably.
If what you try "fails", you could ask a much more specific question showing what you tried with what data and how exactly it failed.

Related

Parse APEX RESTful WebService reference JSON response stored in CLOB001 column

I am trying to generate a report page in my APEX application, using the data obtained from a REST service response.
I added a new RESTful webservice reference and sepecified a JSON output.
Then I've generated a query/report page, but this is what's being currently displayed:
Instead I want the report to display the contents of data field on the JSON response (A single row with various columns and values)
Is there any straight forward way to show each response element and field on its row and column instead of a single column and row with the whole response, like there is with XML RESTful responses?
Consider whether another output type may be more handy. If you're putting out JSON and expect to handle it in PLSQL, then you do need to realize that PLSQL does not handle that natively.
Want to handle the rest response as json in plsql? Then look into some PLSQL libraries/projects that may do this:
http://reseau.erasme.org/pl-sql-library-for-JSON?lang=en
http://sourceforge.net/projects/pljson/
Then print out the returned HTML through HTP.P calls.
Though honestly, if you want to stick with plsql you would be much better off using XML as a return type, since you can then use all the xml goodies in the database. Though simply spitting out some html structure may not be quite too interesting (but I digress).
Or call the rest service through an ajax call from javascript, and handle the object there. After all, JSON is Javascript Object Notation and should be perfect for javascript, right? Then simply inject your html somewhere in the document. Ideally you would set up a region as a container for this.

Javascript in place of json input step

I am loading data from a mongodb collection to a mysql table through Kettle transformation.
First I extract them using MongodbInput and then I use json input step.
But since json input step has very low performance, I wanted to replace it with a
javacript script.
I am a beginner in Javascript and even though i tried somethings, the kettle javascript script is not recognizing any keywords.
can anyone give me sample code to convert Json data to different columns using javascript?
To solve your problem you need to see three aspects:
Reading from MongoDB
Reading from JSON
Reading from (probably) String
Reading from MongoDB Except if you changed the interface, MongoDB returns not JSON but BSON files (~binary JSON). You need to see the MongoDB documentation about reading and writing BSON: probably something like BSON.to() and BSON.from() but I don't know it by heart.
Reading from JSON Once you have your BSON in JSON format, you can read it using JSON.stringify() which returns a String.
Reading from (probably) String If you want to use the capabilities of JSON (why else would you use JSON?), you also want to use JSON.parse() which returns a JSON object.
My experience is that to send a JSON object from one step to the other, using a String is not a bad idea, i.e. at the end of a JavaScript step, you write your JSON object to a String and at the beginning of the next JavaScript step (can be further down the stream) you parse it back to JSON to work with it.
I hope this answers your question.
PS: writing JavaScript steps requires you to learn JavaScript. You don't have to be a master, but the basics are required. There is no way around it.
you could use the json input step to get the values of this json and put in common rows

Is using multipart/form-data any better then JSON + Base64?

I have a server and I need to upload files along with some fields from the client to the server. I have currently been using standard multipart/form-data.
I have found however that using multipart/form-data is not ideal. Objects on my server may have other objects nested within them, and thus are represented as a JSON object with other JSON objects embedded within.
I would like for the client to start making POST/PUT requests using a JSON representation exactly like it would expect in a GET request to the server, in a REST-ful manner. This way I don't have to flatten the fields which might be nested a couple layers within the JSON object in order to use multipart/form-data.
Problem is, JSON doesn't represent binary data. Multipart/form-data doesn't seem to have a way to represent fields nested within the values of other fields. But it does have much better handling of file-uploads.
I am at a loss for how to design this. Should I just have the client upload JSON with the fields encoded in base64, and take the 25% hit? Or should I have the JSON object being represented as some sort of "json" variable in a Multipart/form-data request, and have the binary files to be uploaded as another variable?
Should I just have the client upload JSON with the fields encoded in
base64, and take the 25% hit?
The hit will be 33% since 4/3=1.33.
Or should I have the JSON object being represented as some sort of
"json" variable in a Multipart/form-data request, and have the binary
files to be uploaded as another variable?
This should work.
You might also consider this approach: send all files using multipart, then get some identificators of files as a response. Put this identificators in your json and send it anyway you like. This approach might be beneficial if you have many scenarios in which you send files: you might always send them to the server with the same request, then get their identificators; after that do with them what you like.

Storing email (extra/non-essential) headers in MySQL as JSON properly

So I'm building an email module in to my software. Beyond essential headers I've decided to simply keep the $headers array and store it in a TEXT column called headers. Basically the only time this JSON stored data will be retrieved is when someone does an AJAX request to look at the extended headers (not loaded by default). I'll pull it from MySQL, convert the JSON to a PHP array and iterate over each of the header fields. So in other words I won't be selectively choosing any parts of the array inside of the JSON (inside of the database), merely as a string to be handled by PHP. In this case is using TEXT data type appropriate? I'm also open to other advice.
Sure, that's fine. You could also serialize and unserialize the array instead of using JSON; I don't think there's any reason to convert it to a JavaScript object if it's only ever going to be used in PHP.

Send PDF as byte[] / JSON problem

I am trying to send a generated PDF file (Apache FOP) to the client. I know this can be done by writing the array to the response stream and by setting the correct content type, length and so on in the servlet. My problem is that the whole app was built based on the idea that it will only receive/send JSON. In the servlet's service() method, I have this:
response.setContentType("application/json");
reqBroker.process(request, response);
RequestBroker is the class who processes the JSON (jackson processor), everything is generic and I cannot change it. On top of this, I have to receive the JSON from the request correctly, to access the data and generate my pdf. So those two lines are necessary. But when I send the response, I need to have another content type so that the pdf is displayed correctly in the browser.
So far, I am able to send the byte array as part of the JSON, but then I don't know how to display the array as PDF on the client (if smth like this is even possible).
I would like some suggestions on how can I send my pdf and set the right header, without messing with the JSON. Thanks.
JSON and byte arrays don't mix.
Instead, you should create an <iframe> and point it to a URL that returns a raw PDF.
Take a look here:How to send pdf in json, it lists couple of approaches that you can consider. The easiest way is to convert the binary data into string by using Base64 compression. In C#, this would mean a call to Convert.FromBase64String. However this has space overhead as Base64 compression means around +33% more memory. If you can get away with it, this is the least complicated solution. in case additional size is an issue you can think about zipping it up.