Unable to split json response? - java

This is my method
public String buildJsonData(String username , String message)
{
JsonObject jsonObject = Json.createObjectBuilder().add("Username",username+":"+message).build();
StringWriter stringWriter = new StringWriter();
try(JsonWriter jsonWriter = Json.createWriter(stringWriter))
{
jsonWriter.write(jsonObject);
}
catch(Exception e)
{
System.out.print("buildJsonData ="+e);
}
return stringWriter.toString();
}
If i input username as john and message as hello.I get output as
{"Username":"john:hello"}
But I want output without braces and doublequotes I want my output as
John:hello
I tried to split it using array[0] but didn't get the output.Is it possible in json to get my desired output(without braces and quotes).

On the sending end, you would put the Username and Message entities into a JSONObject and send the resulting string over the network.
On the receiving end, you would unmarshal the JSON to extract the entities. You can then format them however you like.
Please read about JSON encoding here.
This is a simple example:
private String getResponse(){
JSONObject json = new JSONObject();
try {
json.put("Username", "John");
json.put("Message", "Hellow");
} catch (JSONException e) {
e.printStackTrace();
}
return json.toString();
}
private void receiver(){
try {
JSONObject response = new JSONObject(getResponse());
String username = response.getString("Username");
String message = response.getString("Message");
System.out.println(String.format("%s : %s", username,message));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Your structure is not really JSON.
A json structure would be like
{
Username : "John",
Message : "Hello"
}
Anf if your want to really use JSON, there is not way to remove braces and quotes. This IS Json.
If you want to output only the part you quoted, store the json value in a variable
String myoutput = stringWriter.toString();
And then remove the parts you don't want with replace() or a regexp

Braces are part of the JSON notation - they indicate an object. If you remove them, then it's not JSON any more. Same goes for double quotes.You are creating your JSON object as:
Json.createObjectBuilder().add("Username",username+":"+message)
This creates an object with property named Username and value john:hello. Again, this is the JSON notation. It's not intended to be read directly, but to facilitate data transfer between applications (on the same or different devices).
If all you want to create is john:message, then instead of creating a JSON object, you should simply do:
String result = username + ":" + message;
return result;

Related

Java JsonObject: Converting to invalid string

I am using objectMapper to convert object as string. While converting mappper is creating invalid string. How can I keep JsonNode/JsonObject string intact while using object mapper.
JsonObject:
{
"provision": " purpose of usefuleness.\n\n shall not use personal-provided facilities.\n\nWe shall not be required to pay you.
}
Is Converted to
{
"provision": " purpose of usefuleness.
\shall not use personal-provided facilities.
\We shall not be required to pay you.
}
Used:
new ObjectMapper().writeValuesAsString(json);
How can keep the original String intact.
with
new JsonObject(String)
or
when using
new ObjectMapper().writeValuesAsString(json);
Just unquote special character \n
String body = "{\"provision\" : \"purpose of usefuleness.\\n\\n shall not use personal-provided facilities.\\n\\nWe shall not be required to pay you.\"}";
ObjectMapper mapper = new ObjectMapper();
String result = null;
try {
JsonNode node = mapper.readTree(body);
result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(result);
{
"provision" : "purpose of usefuleness.\n\n shall not use personal-provided facilities.\n\nWe shall not be required to pay you."
}

How to parse json in a beam pipeline?

My data is of newline delimited json form and looks like shown below. I am reading this type of data from a Kafka topic.
{"sender":"S1","senderHost":"ip-10-20-30-40","timestamp":"2018-08-13T16:17:12.874Z","topic":"test","messageType":"type_1","data":{"name":"John Doe", "id":"12DROIY321"}}
I want to build an apache Beam pipeline which reads this data from Kafka, parses this json format to give me an output as shown below:
S1,2018-08-13T16:17:12.874Z,type_1,12DROIY321
The output is basically a comma delimited string consisting of the sender, timestamp, messageType and id from within data.
My code so far is as below:
public class Pipeline1{
public static void main(String[] args){
PipelineOptions options = PipelineOptionsFactory.create();
// Create the Pipeline object with the options we defined above.
Pipeline p = Pipeline.create(options);
p.apply(KafkaIO.<Long, String>read()
.withBootstrapServers("localhost:9092")
.withTopic("test")
.withKeyDeserializer(LongDeserializer.class)
.withValueDeserializer(StringDeserializer.class)
.updateConsumerProperties(ImmutableMap.of("auto.offset.reset", (Object)"earliest"))
// We're writing to a file, which does not support unbounded data sources. This line makes it bounded to
// the first 35 records.
// In reality, we would likely be writing to a data source that supports unbounded data, such as BigQuery.
.withMaxNumRecords(35)
.withoutMetadata() // PCollection<KV<Long, String>>
)
.apply(Values.<String>create())
.apply(TextIO.write().to("test"));
p.run().waitUntilFinish();
}
}
I am unable to figure out how to parse the json to get the required csv format within the pipeline. Using the code above, I am able to write the same json lines into a file, and using the code below, i can parse the json, but can anyone please help me figure out how to accomplish this as an additional step with the beam pipeline logic?
JSONParser parser = new JSONParser();
Object obj = null;
try {
obj = parser.parse(strLine);
} catch (ParseException e) {
e.printStackTrace();
}
JSONObject jsonObject = (JSONObject) obj;
String sender = (String) jsonObject.get("sender");
String messageType = (String) jsonObject.get("messageType");
String timestamp = (String) jsonObject.get("timestamp");
System.out.println(sender+","+timestamp+","+messageType);
According to the documentation, you will need to write a transformation (or find one that matches your use case).
https://beam.apache.org/documentation/programming-guide/#composite-transforms
The documentation also provides an excellent example.
Example that should produce your output:
.apply(Values.<String>create())
.apply(
"JSONtoData", // the transform name
ParDo.of(new DoFn<String, String>() { // a DoFn as an anonymous inner class instance
#ProcessElement
public void processElement(#Element String word, OutputReceiver<String> out) {
JSONParser parser = new JSONParser();
Object obj = null;
try {
obj = parser.parse(strLine);
} catch (ParseException e) {
e.printStackTrace();
}
JSONObject jsonObject = (JSONObject) obj;
String sender = (String) jsonObject.get("sender");
String messageType = (String) jsonObject.get("messageType");
String timestamp = (String) jsonObject.get("timestamp");
out.output(sender+","+timestamp+","+messageType);
}
}));
To return CSV values, just change the generics to:
new DoFn<String, YourCSVClassHere>()
OutputReceiver<YourCSVClassHere> out
I didn't test this code, use at own risk.

JAVA get Byte[] nested in JSONObject

i need to get a response from a org.json.JSONObject (args).
new Emitter.Listener() {
#Override
public void call(Object... args) {
try {
JSONObject response = new JSONObject(args[0].toString());
JSONObject data = (JSONObject) response.get("data");
Object defaultResponce = data.get("default");
Log.d(TAG, defaultResponce + "");
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
Object defaultResponce = data.get("default"); equates to a 10 character, String "[B#ffc06c8"
The following image is the args response in the debugger. i need to get the pointed out value.
how do i retreive the value as it in in the debugger?
I think you got mixed up a bit...
Object defaultResponce = data.get("default");
Log.d(TAG, defaultResponce + "");
--> The Log.d prints the reference (defaultResponce + "" converts the byte[] to string). Is this why you think it's a string?
check out if defaultResponce is an instance of byte[] or whatever you are looking for.
Anyway, you can always use reflection to get to the field you want if the standard API doesn't return what you are looking for.

Parsing Representation in Restlet

I am getting JSONException when I try to put in JSONObject.
#Post
public String someCode(Representation rep) throws ResourceException{
try {
rep.getText();
} catch (IOException e) {
LOGGER.error("Error in receiving data from Social", e);
}
try {
JSONObject json = new JSONObject(rep);
String username = json.getString("username");
String password = json.getString("password");
String firstname = json.getString("firstname");
String lastname = json.getString("lastname");
String phone = json.getString("phone");
String email = json.getString("email");
LOGGER.info("username: "+username); //JsonException
LOGGER.info("password: "+password);
LOGGER.info("firstname: "+firstname);
LOGGER.info("lastname: "+lastname);
LOGGER.info("phone: "+phone);
LOGGER.info("email: "+email);
} catch (JSONException e) {
e.printStackTrace();
}
return "200";
}
ERROR LOG:
org.json.JSONException: JSONObject["username"] not found.
at org.json.JSONObject.get(JSONObject.java:516)
at org.json.JSONObject.getString(JSONObject.java:687)
NOTE:
When I try to print rep.getText(), I get the following data:
username=user1&password=222222&firstname=Kevin&lastname=Tak&phone=444444444&email=tka%40gmail.com
Your rep object isn't a JSON object. I actually think that when you pass it to JSONObject(), it only captures a weird string. I suggest to parse it into an array :
Map<String, String> query_pairs = new LinkedHashMap<String, String>();
String query = rep.getText();
String[] pairs = query.split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
}
What you are Receiving in the POST is HTTP Form encoded data not JSON.
Restlet can and does handle these objects natively providing the Form object to interact with them. rather than new JSONObject(String) try new Form(String), for example:
String data = rep.getText();
Form form = new Form(data);
String username = form.getFirstValue("username");
I leave the remainder as an exercise to the reader.
Alternatively you will need to adjust the client submitting the data to encode it in JSON see http://www.json.org/ for the description of this syntax.
For reference the Form class is org.restlet.data.Form in the core Restlet library.

Parse text from webpage in Java (not html)

I am using this code to download a string from a website:
static public String getLast() throws IOException {
String result = "";
URL url = new URL("https://www.bitstamp.net/api/ticker/");
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream()));
String str;
while ((str = in.readLine()) != null) {
result += str;
}
in.close();
return result;
}
When I print the result of this method, this is what I get:
{"high": "349.90", "last": "335.23", "timestamp": "1384198415", "bid": "335.00", "volume": "33743.67611671", "low": "300.28", "ask": "335.23"}
That's exactly what is shown when you open the URL. This works fine for me, but if there is a more efficient way to do this please let me know.
What I need to extract is 335.23. This number is constantly changing, but the words such as "high", "last", "timestamp", etc always stay the same. I need to extract the 335.23 as a double. Is this possible?
Edit:
SOLVED
String url = "https://www.bitstamp.net/api/ticker/";
try {
JsonFactory factory = new JsonFactory();
JsonParser jParser = factory.createParser(new URL(url));
while (jParser.nextToken() != JsonToken.END_OBJECT) {
String fieldname = jParser.getCurrentName();
if ("last".equals(fieldname)) {
jParser.nextToken();
System.out.println(jParser.getText());
break;
}
}
jParser.close();
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JarException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This is JSON. Use a good parser like Jackson. There are also good Tutorials available.
The response is a json. Use a java JSON Parser and get value for "high" element.
One of the java json parsers is available on (http://www.json.org/java/index.html)
JSONObject obj = new JSONObject(" .... ");
String pageName = obj.getString("high");
The data String that you have received is known as JSON encoding. JSON (JavaScript Object Notation) is a lightweight data-interchange format. Use a fine grain simple json encoder and decoder to encode and decode data.

Categories