How to determine if a JSON key has been set to null or not provided
up vote
1
down vote
favorite
I am using gson to convert JSON to my POJO. If I am not passing a specific parameter (the whole parameter, not just the value) in my JSON, it is automatically initialized as null.
Is there a way to find out difference between the above null and the null I get, when I am passing that parameter value as null.
P.S. I cannot change the default conversion from JSON to POJO
java json
add a comment |
up vote
1
down vote
favorite
I am using gson to convert JSON to my POJO. If I am not passing a specific parameter (the whole parameter, not just the value) in my JSON, it is automatically initialized as null.
Is there a way to find out difference between the above null and the null I get, when I am passing that parameter value as null.
P.S. I cannot change the default conversion from JSON to POJO
java json
not passing a specific parameter means passing "" ? or passing "{}" ?
– xxy
Nov 20 at 1:09
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 at 1:12
@xxy I think he means like having fields "foo" and "bar" and only passing{"foo":"foo"}
(nobar
)
– Phil
Nov 20 at 1:13
Thanks for commenting. Not passing that field means: { 'title': 'ComputingandInformationsystems', 'id': 1, 'children': 'true' } In the above JSON, two cases: Case 1: Not passing: { 'id': 1, 'children': 'true' } Case 2: Passing null { 'title': null, 'id': 1, 'children': 'true' }
– Yash Bansal
Nov 20 at 1:21
@Phil: I need to know them, as both the scenarios are different and I need to handle them differently
– Yash Bansal
Nov 20 at 1:28
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am using gson to convert JSON to my POJO. If I am not passing a specific parameter (the whole parameter, not just the value) in my JSON, it is automatically initialized as null.
Is there a way to find out difference between the above null and the null I get, when I am passing that parameter value as null.
P.S. I cannot change the default conversion from JSON to POJO
java json
I am using gson to convert JSON to my POJO. If I am not passing a specific parameter (the whole parameter, not just the value) in my JSON, it is automatically initialized as null.
Is there a way to find out difference between the above null and the null I get, when I am passing that parameter value as null.
P.S. I cannot change the default conversion from JSON to POJO
java json
java json
asked Nov 20 at 1:07
Yash Bansal
396
396
not passing a specific parameter means passing "" ? or passing "{}" ?
– xxy
Nov 20 at 1:09
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 at 1:12
@xxy I think he means like having fields "foo" and "bar" and only passing{"foo":"foo"}
(nobar
)
– Phil
Nov 20 at 1:13
Thanks for commenting. Not passing that field means: { 'title': 'ComputingandInformationsystems', 'id': 1, 'children': 'true' } In the above JSON, two cases: Case 1: Not passing: { 'id': 1, 'children': 'true' } Case 2: Passing null { 'title': null, 'id': 1, 'children': 'true' }
– Yash Bansal
Nov 20 at 1:21
@Phil: I need to know them, as both the scenarios are different and I need to handle them differently
– Yash Bansal
Nov 20 at 1:28
add a comment |
not passing a specific parameter means passing "" ? or passing "{}" ?
– xxy
Nov 20 at 1:09
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 at 1:12
@xxy I think he means like having fields "foo" and "bar" and only passing{"foo":"foo"}
(nobar
)
– Phil
Nov 20 at 1:13
Thanks for commenting. Not passing that field means: { 'title': 'ComputingandInformationsystems', 'id': 1, 'children': 'true' } In the above JSON, two cases: Case 1: Not passing: { 'id': 1, 'children': 'true' } Case 2: Passing null { 'title': null, 'id': 1, 'children': 'true' }
– Yash Bansal
Nov 20 at 1:21
@Phil: I need to know them, as both the scenarios are different and I need to handle them differently
– Yash Bansal
Nov 20 at 1:28
not passing a specific parameter means passing "" ? or passing "{}" ?
– xxy
Nov 20 at 1:09
not passing a specific parameter means passing "" ? or passing "{}" ?
– xxy
Nov 20 at 1:09
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 at 1:12
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 at 1:12
@xxy I think he means like having fields "foo" and "bar" and only passing
{"foo":"foo"}
(no bar
)– Phil
Nov 20 at 1:13
@xxy I think he means like having fields "foo" and "bar" and only passing
{"foo":"foo"}
(no bar
)– Phil
Nov 20 at 1:13
Thanks for commenting. Not passing that field means: { 'title': 'ComputingandInformationsystems', 'id': 1, 'children': 'true' } In the above JSON, two cases: Case 1: Not passing: { 'id': 1, 'children': 'true' } Case 2: Passing null { 'title': null, 'id': 1, 'children': 'true' }
– Yash Bansal
Nov 20 at 1:21
Thanks for commenting. Not passing that field means: { 'title': 'ComputingandInformationsystems', 'id': 1, 'children': 'true' } In the above JSON, two cases: Case 1: Not passing: { 'id': 1, 'children': 'true' } Case 2: Passing null { 'title': null, 'id': 1, 'children': 'true' }
– Yash Bansal
Nov 20 at 1:21
@Phil: I need to know them, as both the scenarios are different and I need to handle them differently
– Yash Bansal
Nov 20 at 1:28
@Phil: I need to know them, as both the scenarios are different and I need to handle them differently
– Yash Bansal
Nov 20 at 1:28
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
use default value in your classes. When the field not pass in json string the value will be the default. example code like below.
public class User {
private String name;
private String age="not set";
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
public class GsonClient {
public static void main(String args) {
String usersJson = "[ { "name": "henry" }, { "name": "justin","age":null } ]";
Gson gson = new GsonBuilder().serializeNulls().create();
User usersWithAge = gson.fromJson(usersJson, User.class);
for (User user : usersWithAge) {
System.out.println(user);
}
}
}
output is here
User{name='henry', age=not set}
User{name='justin', age=null}
Ohh! How can I forget this... Thanks
– Yash Bansal
Nov 20 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 at 1:49
add a comment |
up vote
0
down vote
Object.keys(theObject) will contain the key if the value is set to null or undefined. Also theObject.hasOwnProperty(...) will let you know.
So convert the JSON to an object and inspect the keys to see if the value was marshalled as undefined, null, or not present.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
use default value in your classes. When the field not pass in json string the value will be the default. example code like below.
public class User {
private String name;
private String age="not set";
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
public class GsonClient {
public static void main(String args) {
String usersJson = "[ { "name": "henry" }, { "name": "justin","age":null } ]";
Gson gson = new GsonBuilder().serializeNulls().create();
User usersWithAge = gson.fromJson(usersJson, User.class);
for (User user : usersWithAge) {
System.out.println(user);
}
}
}
output is here
User{name='henry', age=not set}
User{name='justin', age=null}
Ohh! How can I forget this... Thanks
– Yash Bansal
Nov 20 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 at 1:49
add a comment |
up vote
0
down vote
accepted
use default value in your classes. When the field not pass in json string the value will be the default. example code like below.
public class User {
private String name;
private String age="not set";
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
public class GsonClient {
public static void main(String args) {
String usersJson = "[ { "name": "henry" }, { "name": "justin","age":null } ]";
Gson gson = new GsonBuilder().serializeNulls().create();
User usersWithAge = gson.fromJson(usersJson, User.class);
for (User user : usersWithAge) {
System.out.println(user);
}
}
}
output is here
User{name='henry', age=not set}
User{name='justin', age=null}
Ohh! How can I forget this... Thanks
– Yash Bansal
Nov 20 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 at 1:49
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
use default value in your classes. When the field not pass in json string the value will be the default. example code like below.
public class User {
private String name;
private String age="not set";
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
public class GsonClient {
public static void main(String args) {
String usersJson = "[ { "name": "henry" }, { "name": "justin","age":null } ]";
Gson gson = new GsonBuilder().serializeNulls().create();
User usersWithAge = gson.fromJson(usersJson, User.class);
for (User user : usersWithAge) {
System.out.println(user);
}
}
}
output is here
User{name='henry', age=not set}
User{name='justin', age=null}
use default value in your classes. When the field not pass in json string the value will be the default. example code like below.
public class User {
private String name;
private String age="not set";
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
public class GsonClient {
public static void main(String args) {
String usersJson = "[ { "name": "henry" }, { "name": "justin","age":null } ]";
Gson gson = new GsonBuilder().serializeNulls().create();
User usersWithAge = gson.fromJson(usersJson, User.class);
for (User user : usersWithAge) {
System.out.println(user);
}
}
}
output is here
User{name='henry', age=not set}
User{name='justin', age=null}
answered Nov 20 at 1:40
xxy
435311
435311
Ohh! How can I forget this... Thanks
– Yash Bansal
Nov 20 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 at 1:49
add a comment |
Ohh! How can I forget this... Thanks
– Yash Bansal
Nov 20 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 at 1:49
Ohh! How can I forget this... Thanks
– Yash Bansal
Nov 20 at 23:25
Ohh! How can I forget this... Thanks
– Yash Bansal
Nov 20 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 at 1:49
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 at 1:49
add a comment |
up vote
0
down vote
Object.keys(theObject) will contain the key if the value is set to null or undefined. Also theObject.hasOwnProperty(...) will let you know.
So convert the JSON to an object and inspect the keys to see if the value was marshalled as undefined, null, or not present.
add a comment |
up vote
0
down vote
Object.keys(theObject) will contain the key if the value is set to null or undefined. Also theObject.hasOwnProperty(...) will let you know.
So convert the JSON to an object and inspect the keys to see if the value was marshalled as undefined, null, or not present.
add a comment |
up vote
0
down vote
up vote
0
down vote
Object.keys(theObject) will contain the key if the value is set to null or undefined. Also theObject.hasOwnProperty(...) will let you know.
So convert the JSON to an object and inspect the keys to see if the value was marshalled as undefined, null, or not present.
Object.keys(theObject) will contain the key if the value is set to null or undefined. Also theObject.hasOwnProperty(...) will let you know.
So convert the JSON to an object and inspect the keys to see if the value was marshalled as undefined, null, or not present.
answered Nov 20 at 1:41
Steven Spungin
6,45822230
6,45822230
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53384815%2fhow-to-determine-if-a-json-key-has-been-set-to-null-or-not-provided%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
not passing a specific parameter means passing "" ? or passing "{}" ?
– xxy
Nov 20 at 1:09
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 at 1:12
@xxy I think he means like having fields "foo" and "bar" and only passing
{"foo":"foo"}
(nobar
)– Phil
Nov 20 at 1:13
Thanks for commenting. Not passing that field means: { 'title': 'ComputingandInformationsystems', 'id': 1, 'children': 'true' } In the above JSON, two cases: Case 1: Not passing: { 'id': 1, 'children': 'true' } Case 2: Passing null { 'title': null, 'id': 1, 'children': 'true' }
– Yash Bansal
Nov 20 at 1:21
@Phil: I need to know them, as both the scenarios are different and I need to handle them differently
– Yash Bansal
Nov 20 at 1:28