"{\"statusCode\":400,\"throttle\":{\"retryAfter\":null,\"intRetryAfter\":0,\"throttled\":false},\"code\":\"FAILED\",\"description\":\"{\\\"statusCode\\\":400,\\\"code\\\":\\\"FAILED\\\",\\\"description\\\":\\\"\\\\\\\"{ \\\\\\\\\\\\\\\"response\\\\\\\\\\\\\\\":[{\\\\\\\\\\\\\\\"code\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"SUCCESS\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"description\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"SUCCESSFUL MESSAGE\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"Id\\\\\\\\\\\\\\\":123},{\\\\\\\\\\\\\\\"code\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"NOT_FOUND\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"description\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"FAILED MESSAGE\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"errors\\\\\\\\\\\\\\\":[{\\\\\\\\\\\\\\\"Error\\\\\\\\\\\\\\\":{\\\\\\\\\\\\\\\"message\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"Id not found\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"reason\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"ID_NOT_FOUND\\\\\\\\\\\\\\\"}}],\\\\\\\\\\\\\\\"Id\\\\\\\\\\\\\\\":1234}]}\\\\\\\"\\\",\\\"throttle\\\":{\\\"retryAfter\\\":null,\\\"intRetryAfter\\\":0,\\\"throttled\\\":false}}\",\"adId\":0}"
I am receiving a response from a network call, which contains an array with a combination of success and failure responses, I would like to extract them out from this escaped JSON. In the above example I got two code one is a SUCCESS CODE and other is a FAILED CODE, I would like to extract them out and map to a common class.
I tried parsing this JSON using StringEscapeUtils.unescapeJava(stringToBeParsed), but I am still not able to map all the fields, is there a more efficient way to do it? where I can extract response array and map individual field to a common class ?
PS: I am new to java, and not sure about an efficient way to do this here. Writing my own parser would be an overkill to this problem I feel.
Related
I have the following csv file (In production the number of records can range from 20k-100k and has many fields )
id,firstname,lastname,email,profession
100,Betta,Wandie,Betta.Wandie#gmail.com,developer
101,Janey,Firmin,Janey.Firmin#gmail.com,doctor
I need to convert this to json and do further processing.
CSV->JSON->PROCESS FURTHER
.I am able to convert it to JSON directly using the code given here
directly convert CSV file to JSON file using the Jackson library
But i want do validations for json like if lastname has null value then ignore that record or id is missing then ignore that record.
How can i handle the validation?I am using Java 8 and spring boot latest version
I have done something similar by using JavaScript (Nashorn). Yes, that is nasty, but it works, and it is astonishingly fast!
Unfortunately, I do not have the source code at hand …
Why I did it that way had the same reasons as #chrylis-on strike implies in their comment: the validation is much easier if you have an object for each JSON record. But as I was lazy, and there was definitely no need for the Java object I had to create for this, I had the idea with the JavaScript script inside my Java program.
Basically, a JSON String is the source for a JavaScript program; you can assign it directly to a JavaScript variable and then you can access the records as array elements and the fields by their name. So your JavaScript code walks through the records and drops those that do not match your validation rules.
Now the Java part: the keyword here is JSR223; it is an API that allows you to execute scripts inside your Java environment. In your case, you have to provide the converted JSON in the context, then you start the script that writes the modified JSON back to the context.
If the converted JSON is too large, you can even use the same technique to check record by record; if you compile the script, it is nearly as fast as native Java.
You can even omit Jackson and let JavaScript do the conversion …
Sorry that I cannot provide code samples; I will try to get hold on them and add them if I get them.
This is going to be a pretty ridiculous question so brace yourselves.
I need to build an URL string which will update some status variables in a website.
Basically, since the structure of the URL must be in JSON's structure, I'm having a lot of trouble getting variables in the middle of the string.
"http://mywebsite.com/index.php?data={\"Number\":\"1234567890001\",\"TS\":\"15/11/15%2008:08:31:44\",\"SER\":\"53543D303B44723D4E616F\"}"
Basically, I need to replace "1234567890001\" for the variable numberX and "53543D303B44723D4E616F\" for the variable serX.
Can someone lend me a hand?
I've tried everything with +'s & company but I'm not being able to pull it up.
Thanks!
Firstly, It's hard to understand why you are not using GET parameter. It will be a lot more easy to get all parameters
However, if you want to stay with this approach, you can simply get your data value and make a replace, like : replace("\\","")
This will provide a String similar to this : {"Number":"1234567890001","TS":"15/11/15%2008:08:31:44","SER":"53543D303B44723D4E616F"}
And then, you can simply parse it using JSON Library like GSON.
This comes from someone who doesn't really know Java.
I have a .json file that contains an array of objects. It is guaranteed that the file is formatted in a correct manner. Is there a simple way to deserialize the entire contents of that file into a List<myObject> (and then serialize it back as an array).
I saw all kinds of code that is more complex than it should, or treats each key in an individual manner, which is something that I don't really need.
My suggestion would be:
in order to get rid off a lot of code and boilerplate use an already developed lib like Gson, Jackson or similar,
take a look how you can model the list you are trying to read... and try to write a POJO Class (online tools can help you to do that)that represents the objects in the list.
try to serialize and deserialize the file's content and
the rest is just enjoy the results...
I am writing a RESTful web service using Java which should be able to take as (POST) input an unknown number of parameter name, value pairs. I am thinking what is the best way to achieve this.
The POST input will be JSON with the expected format being:
{"input" :
{ "x" = 1, "y"="Hello, "z"="1.2.3.4",... }
}
The parameter values are expected to be a mix of int/long/float/string etc. The expected behavior is to save this input to the DB and provide it back on request. The tricky part is with the parameter names being unknown, I cannot write getter and setter methods. Is there a simple way for handling this?
Any pointers in the right direction will be very much appreciated. Can I read this as a List? Do I need to write Custom MessageBodyReaders? Is there some other way of achieving this? The only option I can think of is to have these sent as a file and parse it but am hoping that there are simpler/cleaner solutions. Thank you for any pointers...
If all you need to do is save it and make it available on request, why parse it at all? Simply write the json as a string to your database, and return it as a string while setting the response type to "application/json". The one caveat being you're going to have to have some way to identify which json string you want and associate a unique id that you're going to be able to determine when you're requesting the specific json string in the future.
Something worth noting here would be that when you don't parse something, you don't actually know what it is. Even though a lot of modern libraries/frameworks sanitize against sql injection/xss already, it would be worth it to verify that this is being done in your server if you don't already know for sure.
That being said, if you're still feeling as though you need to parse the json, I would look into gson: http://code.google.com/p/google-gson/
JSON stands for JavaScript Object Notation. But how come languages like php, java, c etc can also communication each other with json.
What I want to know is that, am i correct to say that json is not limited to js only, but served as a protocol for applications to communicate with each other over the network, which is the same purpose as XML?
JSON cannot handle complex data hierarchies like XML can (attributes, namespaces, etc.), but on the other hand you don't get the same overhead with JSON as you get with XML (if you don't need the complex data structures).
Since JSON is plain text with a special notation for JS to interpret, it's an easy protocol to adopt in other languages.
It is easy for a JS script to parse JSON, since it can be done using 'eval' in which the JS enginge can use its full power.
On the other hand, it is more complicated to generate JSON from within JS. Usually one uses the JSON package from www.json.org in which an object can easily be serialised using JSON.stringify, but it is implemented in JS so its not running with optimal performance.
So serialising JSON is about the same complexity using JS as when using Java, PHP or any other server side language.
Therefore, in my opinion, JSON is best suited when there is asymmetry between produce/consumer e.g. a web server that generates a lot of data that is consumed by the web application. Not the other way around.
But! When one choses JSON as data format it should be used in both directions, not XML<>JSON. Except for when simple get requests are used to retrieve JSON data.
yes, JSON is also wildly used as a data exchange protocol much like XML.
Typically a program (not written in JavaScript) needs a JSON library to parse and create JSON objects (although you can probably create them even without one).
Your right - it's a light weight data interchange format -- more details at: http://www.json.org
You are completely correct. JSON definition of how data should be formatted. It is more light weight than XML and therefore well suited to things like AJAX where you want to send data back and forth to the server quickly.