0

I'm trying to combine multiple JSON objects into one JSON array and maybe filter on the name of the data (temperature, pressure, altitude).

I tried to use JSON-Simple and Java but I can't get it to work.

Here are the pieces of JSON:

The input I want to convert:

{"temperature" : -12, "sendtimes" : 10000}
{"pressure" : 1000, "sendtimes" : 10001}
{"altitude" : 100.7, "sendtimes" : 10002}`

How I want it after conversion:

{
  "temperaturData": [
      {"temperature": -12, "sendtimes": 10000},
      {"pressure" : 1000, "sendtimes" : 10001},
      {"altitude" : 100.7, "sendtimes" : 10002},
  ]
}

I have no clue on how to do this, thanks to everyone who can help me!

3
  • 1
    Check this : stackoverflow.com/questions/16495702/… Commented Jan 1, 2019 at 11:27
  • Thanks for your reply, but in that question the input is already formatted, I just have a file with those three objects Commented Jan 1, 2019 at 11:29
  • Two questions: Is the input file as is and not enclosed by either {} or []? Is this {"temperature" : -12, "sendtimes" : 10000} in a single line and all other individual {} enclosed data in single lines? Commented Jan 1, 2019 at 12:36

3 Answers 3

1

Using JSON Library

JSONArray arr = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("temperature", "-12");
obj.put("sendtimes", "1000");

arr.put(obj);
obj = new JSONObject();
obj.put("pressure", "1000");
obj.put("sendtimes", "1001");

arr.put(obj);

JSONObject finalObj = new JSONObject();
finalObj.put("temperaturData", arr);
System.out.println(finalObj);

Though what I suggest is that, you should better assign keys to the given inner arrays too, which will make accessing it easy and bug free.

Sign up to request clarification or add additional context in comments.

Comments

0

It is quite simple what you want to achieve. I am using JavaScript, same can be achieved with any other language using similar data structure. What you want to do is create an array and append the values to that array.

var tempData = {"temperaturData" : []}
tempData["temperaturData"].push({"temperature": -12, "sendtimes": 10000})
tempData["temperaturData"].push({"pressure" : 1000, "sendtimes" : 10001})
tempData["temperaturData"].push({"altitude" : 100.7, "sendtimes" : 10002})
JSON.stringify(tempData)

2 Comments

Thank you, but how do I do this if the dataset keeps growing larger, so everytime I run the code it converts it, no matter how many objects there are
you can convert it into a function which adds the data to existing object
0

JSON Library mentioned by Jatin Asija is a very cool and simple JSON library that you efinitely can use to get what you want. However the de-facto standard for working with JSON in java is Jackson JSON library also known as "FasterXML/jackson". All you will have to do is to use ObjectMapperclass and its methods readValue() and writeValueAsString(Object value) to do whatever you want. Here is cool tutorial for ObjectMapper and Here are the Maven dependencies you might need to use it:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

Here is javadoc for ObjectMapper

4 Comments

Isn't ObjectMapper class is about how to serialize Java objects into JSON and deserialize JSON string into Java objects? and even if we can use it to do the above-mentioned task, but isn't that will make it very complex and tedious?
Jatin - yes you are correct that ObjectMapper is for serialization and de-serialization of Java classes into JSON. But your solution is the same - with JSON library you de-serialize JSON strings and then do the operations on the java objects and then serialize them back to JSON Strings. JSON library is very simple and convinient for simple operations (Also my first choice to work with JSON when I just started). But Jackson JSON is by far more powerful and definitely faster, also very tightly integrated with Spring for JSON serialization behind the scienes. So it is ultimately a better choice.
yes, i know it is quite the same, but in the case of ObjectMapper, one will need to create an extra bean, but with org.json, one can simply use any key and value, and as with jackson, it first parse JSON and then map it to bean, making it slower than HashMap which is used in org.json, but if anyone further want to use the bean, then jackson is better, but for the details mentioned above, this doesn't seem to be the case.
I guess so Jatin. So let the OP decide which one he wants

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.