I want to combine multiple JSON objects into a single array
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!
java json type-conversion
add a comment |
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!
java json type-conversion
1
Check this : stackoverflow.com/questions/16495702/…
– SIW
Jan 1 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
– Cemre Suler
Jan 1 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?
– Nikhil
Jan 1 at 12:36
add a comment |
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!
java json type-conversion
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!
java json type-conversion
java json type-conversion
asked Jan 1 at 11:22
Cemre SulerCemre Suler
32
32
1
Check this : stackoverflow.com/questions/16495702/…
– SIW
Jan 1 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
– Cemre Suler
Jan 1 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?
– Nikhil
Jan 1 at 12:36
add a comment |
1
Check this : stackoverflow.com/questions/16495702/…
– SIW
Jan 1 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
– Cemre Suler
Jan 1 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?
– Nikhil
Jan 1 at 12:36
1
1
Check this : stackoverflow.com/questions/16495702/…
– SIW
Jan 1 at 11:27
Check this : stackoverflow.com/questions/16495702/…
– SIW
Jan 1 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
– Cemre Suler
Jan 1 at 11:29
Thanks for your reply, but in that question the input is already formatted, I just have a file with those three objects
– Cemre Suler
Jan 1 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?– Nikhil
Jan 1 at 12:36
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?– Nikhil
Jan 1 at 12:36
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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)
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
– Cemre Suler
Jan 1 at 11:39
you can convert it into a function which adds the data to existing object
– Cognoscis
Jan 1 at 11:45
add a comment |
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 ObjectMapper
class 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
Isn'tObjectMapper
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 Asija
Jan 2 at 4:46
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.
– Michael Gantman
Jan 2 at 8:19
yes, i know it is quite the same, but in the case ofObjectMapper
, one will need to create an extra bean, but withorg.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 thanHashMap
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.
– Jatin Asija
Jan 2 at 8:58
I guess so Jatin. So let the OP decide which one he wants
– Michael Gantman
Jan 2 at 9:09
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995025%2fi-want-to-combine-multiple-json-objects-into-a-single-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 1 at 11:54


Pshemo
95.4k15131193
95.4k15131193
answered Jan 1 at 11:49
Jatin AsijaJatin Asija
78111
78111
add a comment |
add a comment |
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)
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
– Cemre Suler
Jan 1 at 11:39
you can convert it into a function which adds the data to existing object
– Cognoscis
Jan 1 at 11:45
add a comment |
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)
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
– Cemre Suler
Jan 1 at 11:39
you can convert it into a function which adds the data to existing object
– Cognoscis
Jan 1 at 11:45
add a comment |
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)
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)
answered Jan 1 at 11:34
CognoscisCognoscis
12819
12819
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
– Cemre Suler
Jan 1 at 11:39
you can convert it into a function which adds the data to existing object
– Cognoscis
Jan 1 at 11:45
add a comment |
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
– Cemre Suler
Jan 1 at 11:39
you can convert it into a function which adds the data to existing object
– Cognoscis
Jan 1 at 11:45
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
– Cemre Suler
Jan 1 at 11:39
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
– Cemre Suler
Jan 1 at 11:39
you can convert it into a function which adds the data to existing object
– Cognoscis
Jan 1 at 11:45
you can convert it into a function which adds the data to existing object
– Cognoscis
Jan 1 at 11:45
add a comment |
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 ObjectMapper
class 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
Isn'tObjectMapper
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 Asija
Jan 2 at 4:46
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.
– Michael Gantman
Jan 2 at 8:19
yes, i know it is quite the same, but in the case ofObjectMapper
, one will need to create an extra bean, but withorg.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 thanHashMap
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.
– Jatin Asija
Jan 2 at 8:58
I guess so Jatin. So let the OP decide which one he wants
– Michael Gantman
Jan 2 at 9:09
add a comment |
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 ObjectMapper
class 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
Isn'tObjectMapper
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 Asija
Jan 2 at 4:46
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.
– Michael Gantman
Jan 2 at 8:19
yes, i know it is quite the same, but in the case ofObjectMapper
, one will need to create an extra bean, but withorg.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 thanHashMap
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.
– Jatin Asija
Jan 2 at 8:58
I guess so Jatin. So let the OP decide which one he wants
– Michael Gantman
Jan 2 at 9:09
add a comment |
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 ObjectMapper
class 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
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 ObjectMapper
class 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
answered Jan 1 at 12:27


Michael GantmanMichael Gantman
2,074719
2,074719
Isn'tObjectMapper
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 Asija
Jan 2 at 4:46
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.
– Michael Gantman
Jan 2 at 8:19
yes, i know it is quite the same, but in the case ofObjectMapper
, one will need to create an extra bean, but withorg.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 thanHashMap
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.
– Jatin Asija
Jan 2 at 8:58
I guess so Jatin. So let the OP decide which one he wants
– Michael Gantman
Jan 2 at 9:09
add a comment |
Isn'tObjectMapper
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 Asija
Jan 2 at 4:46
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.
– Michael Gantman
Jan 2 at 8:19
yes, i know it is quite the same, but in the case ofObjectMapper
, one will need to create an extra bean, but withorg.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 thanHashMap
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.
– Jatin Asija
Jan 2 at 8:58
I guess so Jatin. So let the OP decide which one he wants
– Michael Gantman
Jan 2 at 9:09
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 Asija
Jan 2 at 4:46
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 Asija
Jan 2 at 4:46
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.
– Michael Gantman
Jan 2 at 8:19
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.
– Michael Gantman
Jan 2 at 8:19
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.– Jatin Asija
Jan 2 at 8:58
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.– Jatin Asija
Jan 2 at 8:58
I guess so Jatin. So let the OP decide which one he wants
– Michael Gantman
Jan 2 at 9:09
I guess so Jatin. So let the OP decide which one he wants
– Michael Gantman
Jan 2 at 9:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995025%2fi-want-to-combine-multiple-json-objects-into-a-single-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Check this : stackoverflow.com/questions/16495702/…
– SIW
Jan 1 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
– Cemre Suler
Jan 1 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?– Nikhil
Jan 1 at 12:36