How to determine if a JSON key has been set to null or not provided












1















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










share|improve this question























  • 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"} (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













  • @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
















1















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










share|improve this question























  • 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"} (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













  • @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














1












1








1








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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"} (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













  • @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











  • 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













  • 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












2 Answers
2






active

oldest

votes


















0














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}





share|improve this answer
























  • 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



















0














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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}





    share|improve this answer
























    • 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
















    0














    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}





    share|improve this answer
























    • 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














    0












    0








    0







    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}





    share|improve this answer













    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}






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 1:40









    xxyxxy

    445311




    445311













    • 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











    • 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













    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 1:41









        Steven SpunginSteven Spungin

        6,71832331




        6,71832331






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$