Parsing JSON response in JAVA - json

I have a standard java http client. I am receiving a JSON response while invoking an HTTP operation.
The response looks like as below :
//OK[25,24,23,22,21,20,19,8,8,8,8,8,18,17,16,15,14,8,13,12,11,10,9,9,8,8,8,7,4,6,5,4,3,2,34,1,["[Ljava.lang.String;/2600011424","17501-20000-39010","1152963","Not Available","Open","2222","01/15/2012","0.00","1663.07","02/09/2012","02/15/2012","4446.36","2783.29","2038.71","02407.65","03/11/2012","486.76","07135.7900","0","01/26/2012","001122 AAA BBB CCC","1","Y","01/23/2012","E"],0,5]
Though the content type is application/json in the response, I am not sure on how to process this JSON format.
Any help is appreciated.

Check JSON in Java. Should be pretty easy to use.

That isn't actually JSON, but if you strip off the //OK from the front then it would be.
Strip through characters then run it through a JSON parser. Several are listed just after the diagrams on the JSON homage.

Related

Can the Azure Storage REST API send the response in JSON format?

I am developing a library for working with various types of cloud based queue services.
One of those services is the Azure Queue Storage REST API.
For the Amazon SQS service I can send an Accept: application/json header and the response is in JSON format.
Since JSON is a format that is supported by many APIs and XML is not fun to work with, I would prefer the Azure Storage REST API to also return a response in JSON format.
I have tried to set the Accept: application/json header to no avail. The responses are all in XML format with Content-Type: application/xml, which is obviously not what I was asking for.
Currently all code is in C with dependencies on a couple of libraries, including cURL and jansson, although for this question that doesn't really matter. It's just that I would like the library to be as simple and lightweight as possible.
I have a hard time digging through all kinds of documentation. Most topics I can find are about sending JSON within a message. But that's not what I'm going for.
Is it even possible to receive the actual responses in JSON? I would really like to drop my libxml2 dependency.
As pointed by #Tom Because the documentation is stating that it only return XML, I would personally write an azure function who becomes an adaptor which basically takes your request, sends it to azure queue storage, retrieves the xml response and then converts the xml response to json and return the json to the caller (which will be your C code).
A sample python code to convert xml to json is shown below:
import xmltodict
import json
text = ''
xpars = xmltodict.parse(text)
json = json.dumps(xpars)
print(json)
A sample xml message
text = '<QueueMessagesList>
<QueueMessage>
<MessageId>string-message-id</MessageId>
<InsertionTime>insertion-time</InsertionTime>
<ExpirationTime>expiration-time</ExpirationTime>
<PopReceipt>opaque-string-receipt-data</PopReceipt>
<TimeNextVisible>time-next-visible</TimeNextVisible>
<DequeueCount>integer</DequeueCount>
<MessageText>message-body</MessageText>
</QueueMessage>
</QueueMessagesList>'
And the response will be :
{
"QueueMessagesList": {
"QueueMessage": {
"MessageId": "string-message-id",
"InsertionTime": "insertion-time",
"ExpirationTime": "expiration-time",
"PopReceipt": "opaque-string-receipt-data",
"TimeNextVisible": "time-next-visible",
"DequeueCount": "integer",
"MessageText": "message-body"
}
}
}
Please Note: This whole thing can also be done using a Logic App in azure.
I have only shown the XML to JSON converter part here, but it is really straightforward to write an HTTP Trigger Azure Function to do the same. Or you can even write this converter into your C code as well.
Hope this helps you in moving on with your library development.

How to disable response parsing in Karate?

I'm using Karate 0.9.5. and am testing an endpoint that returns a 125MB json response (I know, shouldn't do that over json -- but am stuck for now). How can I disable Karate from parsing the response json and just treat it as plain text? The response takes milliseconds to come over the wire, but Karate just hangs trying to parse the response. I don't need to validate the response, just check for 200 OK.
Thanks.
You can't. I suggest you write a Java util (should be just 5-10 lines of code) to do a GET or whatever to this end-point and call it from Karate and save to something like target/temp.json or discard the response.
Refer to Java interop: https://github.com/intuit/karate#calling-java
If you use karate-apache then the Apache HTTP Client will be in the classpath. So you can refer to the docs: https://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/fundamentals.html#d5e49
Also look at the end of this test for an alternative example of making a GET / POST in Java: ProxyServerTest.java

Is there any online tool that can be POST requested with json payload to be pretty printed?

There are plenty of online json viewers. Yet if there are any that may be POST requested with json payload so that incoming json string might be pretty printed? Not just copy/paste as usual?
Use https://webhook.site/. It provides a unique json receiver for you that shows your http requests.
Here's two sites you can try for your requests:
https://www.hurl.it
http://gurujsonrpc.appspot.com
Here is the link which i use to parse the json data i get from services.
All you have to do is to copy the json and paste in it, it will give you a very clear preview of the data.
json parser

Why do I get numeric characters in my raw JSON response from WCF?

Switching from XML to JSON on the fly in a WCF RESTful service. When I POST some JSON to the service from Fiddler, I get the right JSON back, but if you look in the RAW response, you get the JSON message that is sandwiched in between 2 numbers, like this:
Is this problematic for parsing the JSON? Like I said, the actual JSON looks fine.

Does OData4j request a response in JSON format or convert the response to JSON format? (Android)

I've been using the following code to create my android OData service consumer.
Services = ODataConsumer
.newBuilder("http://xxx.xxx.xxx.xxx:xxxxx/WCFDataServices.svc/")
.setFormatType(FormatType.JSON).build();
What I want to know is when the client makes a request through the Services consumer will the request make the server create a JSON formatted response or will the OData4j/consumer convert the response to JSON format.
Thanks in advance for the help. :)
It requests a JSON response from the server using the Accept request header. It does no conversion.
See: http://code.google.com/p/odata4j/source/browse/odata4j-jersey/src/main/java/org/odata4j/jersey/consumer/ODataJerseyClient.java#175
Hope that helps,
- john
Odata defalut format is ATOM. If you want you can change it to JSON, as you already doing.
"FormatType.JSON"
OData4j/consumer APIs are responsible to convert the response type whatever format you have defined.