How to determine if a JSON key has been set to null or not provided
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 |
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 '18 at 1:09
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 '18 at 1:12
@xxy I think he means like having fields "foo" and "bar" and only passing{"foo":"foo"}
(nobar
)
– Phil
Nov 20 '18 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 '18 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 '18 at 1:28
add a comment |
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 '18 at 1:07
Yash BansalYash Bansal
466
466
not passing a specific parameter means passing "" ? or passing "{}" ?
– xxy
Nov 20 '18 at 1:09
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 '18 at 1:12
@xxy I think he means like having fields "foo" and "bar" and only passing{"foo":"foo"}
(nobar
)
– Phil
Nov 20 '18 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 '18 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 '18 at 1:28
add a comment |
not passing a specific parameter means passing "" ? or passing "{}" ?
– xxy
Nov 20 '18 at 1:09
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 '18 at 1:12
@xxy I think he means like having fields "foo" and "bar" and only passing{"foo":"foo"}
(nobar
)
– Phil
Nov 20 '18 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 '18 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 '18 at 1:28
not passing a specific parameter means passing "" ? or passing "{}" ?
– xxy
Nov 20 '18 at 1:09
not passing a specific parameter means passing "" ? or passing "{}" ?
– xxy
Nov 20 '18 at 1:09
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 '18 at 1:12
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 '18 at 1:12
@xxy I think he means like having fields "foo" and "bar" and only passing
{"foo":"foo"}
(no bar
)– Phil
Nov 20 '18 at 1:13
@xxy I think he means like having fields "foo" and "bar" and only passing
{"foo":"foo"}
(no bar
)– Phil
Nov 20 '18 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 '18 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 '18 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 '18 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 '18 at 1:28
add a comment |
2 Answers
2
active
oldest
votes
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 '18 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 '18 at 1:49
add a comment |
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 |
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%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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 '18 at 1:49
add a comment |
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 '18 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 '18 at 1:49
add a comment |
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 '18 at 1:40
xxyxxy
455311
455311
Ohh! How can I forget this... Thanks
– Yash Bansal
Nov 20 '18 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 '18 at 1:49
add a comment |
Ohh! How can I forget this... Thanks
– Yash Bansal
Nov 20 '18 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 '18 at 1:49
Ohh! How can I forget this... Thanks
– Yash Bansal
Nov 20 '18 at 23:25
Ohh! How can I forget this... Thanks
– Yash Bansal
Nov 20 '18 at 23:25
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 '18 at 1:49
If this method can solve your problem, please mark it as "accepted" to help more people.
– xxy
Nov 21 '18 at 1:49
add a comment |
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 |
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 |
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 '18 at 1:41
Steven SpunginSteven Spungin
6,93132331
6,93132331
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.
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 '18 at 1:09
Why do you need to know this? Won't the result be the same anyway?
– Phil
Nov 20 '18 at 1:12
@xxy I think he means like having fields "foo" and "bar" and only passing
{"foo":"foo"}
(nobar
)– Phil
Nov 20 '18 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 '18 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 '18 at 1:28