What is the java library to remove/modify a json object based on json path, or how to fix the below issue in...












0















I am trying to implement a function to be able to remove or modify a json object base on a specified json path. For example, if i have a below json string/object:



{
"PersonalDetailsDTO": {
"FirstName": "Mark",
"LastName": "Sully",
"TotalDependent": "2",
"DOB": "19811212",
"SecQuestion": "Some Que",
"SecAnswer": "Some-Ans",
"Mobile": "0123456789",
"Email": "some@validemail.com",
"Title": "Mr",
"EmploymentListDTO": [
{
"Type": "Full-time",
"Probation": true
}
],
"AddressListDTO": [
{
"AddressType": "BUS",
"PostCode": "1234",
"State": "NSW",
"StreetName": "miller",
"StreetNumber": "111",
"StreetType": "Invalid",
"Suburb": "Sydney",
"UnitNumber": "Maximum"
}
]
}
}


And i want to remove element $.PersonalDetailsDTO.AddressListDTO.PostCode.



I've done quite some search, and the one lib i found is JsonPath: http://static.javadoc.io/com.jayway.jsonpath/json-path/2.2.0/com/jayway/jsonpath/JsonPath.html



So i wrote the below code:



public static void main(String args) {
// Prints "Hello, World" to the terminal window.
String jsonString = "{n" +
" "PersonalDetailsDTO": {n" +
" "FirstName":"Mark",n" +
" "LastName":"Sully",n" +
" "Title":"Mr",n" +
" "DOB":"19811201",n" +
" "SecQuestion":"Some Ques",n" +
" "SecAnswer":"Some-Ans",n" +
" "Email":"some@validemail.com",n" +
" "EmploymentListDTO": [n" +
" {n" +
" "Type": "Full-time",n" +
" "Probation": truen" +
" }n" +
" ],n" +
" "AddressListDTO": [n" +
" {n" +
" "AddressType": "Residential",n" +
" "PostCode": "2345",n" +
" "State": "NSW",n" +
" "StreetName": "MEL",n" +
" "StreetNumber": "2",n" +
" "StreetType": "Boulevard",n" +
" "Suburb": "Melbourne",n" +
" "UnitNumber": "345"n" +
" }n" +
" ]n" +
" } n" +
"}";

JSONObject jsonObject = new JSONObject(jsonString);
System.out.println("Before: " + jsonObject.toString());

JsonPath jp = JsonPath.compile("$.PersonalDetailsDTO.AddressListDTO[0].PostCode");
Configuration conf = Configuration.defaultConfiguration();
Object json = conf.jsonProvider().parse(jsonString);
System.out.println("After: " + jp.delete(json, conf).toString());
}


And the console log displays:



Before: {"PersonalDetailsDTO":{"EmploymentListDTO":[{"Type":"Full-time","Probation":true}],"SecAnswer":"Some-Ans","Email":"some@validemail.com","SecQuestion":"Some Ques","FirstName":"Mark","DOB":"19811201","AddressListDTO":[{"StreetName":"MEL","Suburb":"Melbourne","State":"NSW","StreetNumber":"2","UnitNumber":"345","AddressType":"Residential","PostCode":"2345","StreetType":"Boulevard"}],"Title":"Mr","LastName":"Sully"}}


After: {PersonalDetailsDTO={FirstName=Mark, LastName=Sully, Title=Mr, DOB=19811201, SecQuestion=Some Ques, SecAnswer=Some-Ans, Email=some@validemail.com, EmploymentListDTO=[{"Type":"Full-time","Probation":true}], AddressListDTO=[{"AddressType":"Residential","State":"NSW","StreetName":"MEL","StreetNumber":"2","StreetType":"Boulevard","Suburb":"Melbourne","UnitNumber":"345"}]}}


Looks like JsonPath is doing it's job and removing $.PersonalDetailsDTO.AddressListDTO.PostCode. However, there's something very obvious that bothers me:



Looking at the json string produced by .toString() in before and after case, JSONObject API printed a nice string in true json standard format with every double quotes "" present, while the JsonPath .toString produce a customer string format that has some elements in double quote "" while others are not and i can not use it further like JSONObject.



And what i noticed is that although JsonPath claim to accept "java.lang.Object" as parameter in many of its function, what it truely accept is something called "jsonProvider". Not sure if it's causing the weird .toString() behavior.



Anyway, does anyone know how get a nice formatted json string out of JsonPath APIs like remove(), put(), read() and many other? Or to convert the return value to something like JSONObject?



If you know any other Java lib that can do remove/modify element by json path, please feel free to recommand. Thank you!










share|improve this question























  • Questions asking us to recommend a software library are off-topic for Stack Overflow as they tend to attract opinionated answers and spam

    – Michael
    Nov 13 '18 at 9:47
















0















I am trying to implement a function to be able to remove or modify a json object base on a specified json path. For example, if i have a below json string/object:



{
"PersonalDetailsDTO": {
"FirstName": "Mark",
"LastName": "Sully",
"TotalDependent": "2",
"DOB": "19811212",
"SecQuestion": "Some Que",
"SecAnswer": "Some-Ans",
"Mobile": "0123456789",
"Email": "some@validemail.com",
"Title": "Mr",
"EmploymentListDTO": [
{
"Type": "Full-time",
"Probation": true
}
],
"AddressListDTO": [
{
"AddressType": "BUS",
"PostCode": "1234",
"State": "NSW",
"StreetName": "miller",
"StreetNumber": "111",
"StreetType": "Invalid",
"Suburb": "Sydney",
"UnitNumber": "Maximum"
}
]
}
}


And i want to remove element $.PersonalDetailsDTO.AddressListDTO.PostCode.



I've done quite some search, and the one lib i found is JsonPath: http://static.javadoc.io/com.jayway.jsonpath/json-path/2.2.0/com/jayway/jsonpath/JsonPath.html



So i wrote the below code:



public static void main(String args) {
// Prints "Hello, World" to the terminal window.
String jsonString = "{n" +
" "PersonalDetailsDTO": {n" +
" "FirstName":"Mark",n" +
" "LastName":"Sully",n" +
" "Title":"Mr",n" +
" "DOB":"19811201",n" +
" "SecQuestion":"Some Ques",n" +
" "SecAnswer":"Some-Ans",n" +
" "Email":"some@validemail.com",n" +
" "EmploymentListDTO": [n" +
" {n" +
" "Type": "Full-time",n" +
" "Probation": truen" +
" }n" +
" ],n" +
" "AddressListDTO": [n" +
" {n" +
" "AddressType": "Residential",n" +
" "PostCode": "2345",n" +
" "State": "NSW",n" +
" "StreetName": "MEL",n" +
" "StreetNumber": "2",n" +
" "StreetType": "Boulevard",n" +
" "Suburb": "Melbourne",n" +
" "UnitNumber": "345"n" +
" }n" +
" ]n" +
" } n" +
"}";

JSONObject jsonObject = new JSONObject(jsonString);
System.out.println("Before: " + jsonObject.toString());

JsonPath jp = JsonPath.compile("$.PersonalDetailsDTO.AddressListDTO[0].PostCode");
Configuration conf = Configuration.defaultConfiguration();
Object json = conf.jsonProvider().parse(jsonString);
System.out.println("After: " + jp.delete(json, conf).toString());
}


And the console log displays:



Before: {"PersonalDetailsDTO":{"EmploymentListDTO":[{"Type":"Full-time","Probation":true}],"SecAnswer":"Some-Ans","Email":"some@validemail.com","SecQuestion":"Some Ques","FirstName":"Mark","DOB":"19811201","AddressListDTO":[{"StreetName":"MEL","Suburb":"Melbourne","State":"NSW","StreetNumber":"2","UnitNumber":"345","AddressType":"Residential","PostCode":"2345","StreetType":"Boulevard"}],"Title":"Mr","LastName":"Sully"}}


After: {PersonalDetailsDTO={FirstName=Mark, LastName=Sully, Title=Mr, DOB=19811201, SecQuestion=Some Ques, SecAnswer=Some-Ans, Email=some@validemail.com, EmploymentListDTO=[{"Type":"Full-time","Probation":true}], AddressListDTO=[{"AddressType":"Residential","State":"NSW","StreetName":"MEL","StreetNumber":"2","StreetType":"Boulevard","Suburb":"Melbourne","UnitNumber":"345"}]}}


Looks like JsonPath is doing it's job and removing $.PersonalDetailsDTO.AddressListDTO.PostCode. However, there's something very obvious that bothers me:



Looking at the json string produced by .toString() in before and after case, JSONObject API printed a nice string in true json standard format with every double quotes "" present, while the JsonPath .toString produce a customer string format that has some elements in double quote "" while others are not and i can not use it further like JSONObject.



And what i noticed is that although JsonPath claim to accept "java.lang.Object" as parameter in many of its function, what it truely accept is something called "jsonProvider". Not sure if it's causing the weird .toString() behavior.



Anyway, does anyone know how get a nice formatted json string out of JsonPath APIs like remove(), put(), read() and many other? Or to convert the return value to something like JSONObject?



If you know any other Java lib that can do remove/modify element by json path, please feel free to recommand. Thank you!










share|improve this question























  • Questions asking us to recommend a software library are off-topic for Stack Overflow as they tend to attract opinionated answers and spam

    – Michael
    Nov 13 '18 at 9:47














0












0








0








I am trying to implement a function to be able to remove or modify a json object base on a specified json path. For example, if i have a below json string/object:



{
"PersonalDetailsDTO": {
"FirstName": "Mark",
"LastName": "Sully",
"TotalDependent": "2",
"DOB": "19811212",
"SecQuestion": "Some Que",
"SecAnswer": "Some-Ans",
"Mobile": "0123456789",
"Email": "some@validemail.com",
"Title": "Mr",
"EmploymentListDTO": [
{
"Type": "Full-time",
"Probation": true
}
],
"AddressListDTO": [
{
"AddressType": "BUS",
"PostCode": "1234",
"State": "NSW",
"StreetName": "miller",
"StreetNumber": "111",
"StreetType": "Invalid",
"Suburb": "Sydney",
"UnitNumber": "Maximum"
}
]
}
}


And i want to remove element $.PersonalDetailsDTO.AddressListDTO.PostCode.



I've done quite some search, and the one lib i found is JsonPath: http://static.javadoc.io/com.jayway.jsonpath/json-path/2.2.0/com/jayway/jsonpath/JsonPath.html



So i wrote the below code:



public static void main(String args) {
// Prints "Hello, World" to the terminal window.
String jsonString = "{n" +
" "PersonalDetailsDTO": {n" +
" "FirstName":"Mark",n" +
" "LastName":"Sully",n" +
" "Title":"Mr",n" +
" "DOB":"19811201",n" +
" "SecQuestion":"Some Ques",n" +
" "SecAnswer":"Some-Ans",n" +
" "Email":"some@validemail.com",n" +
" "EmploymentListDTO": [n" +
" {n" +
" "Type": "Full-time",n" +
" "Probation": truen" +
" }n" +
" ],n" +
" "AddressListDTO": [n" +
" {n" +
" "AddressType": "Residential",n" +
" "PostCode": "2345",n" +
" "State": "NSW",n" +
" "StreetName": "MEL",n" +
" "StreetNumber": "2",n" +
" "StreetType": "Boulevard",n" +
" "Suburb": "Melbourne",n" +
" "UnitNumber": "345"n" +
" }n" +
" ]n" +
" } n" +
"}";

JSONObject jsonObject = new JSONObject(jsonString);
System.out.println("Before: " + jsonObject.toString());

JsonPath jp = JsonPath.compile("$.PersonalDetailsDTO.AddressListDTO[0].PostCode");
Configuration conf = Configuration.defaultConfiguration();
Object json = conf.jsonProvider().parse(jsonString);
System.out.println("After: " + jp.delete(json, conf).toString());
}


And the console log displays:



Before: {"PersonalDetailsDTO":{"EmploymentListDTO":[{"Type":"Full-time","Probation":true}],"SecAnswer":"Some-Ans","Email":"some@validemail.com","SecQuestion":"Some Ques","FirstName":"Mark","DOB":"19811201","AddressListDTO":[{"StreetName":"MEL","Suburb":"Melbourne","State":"NSW","StreetNumber":"2","UnitNumber":"345","AddressType":"Residential","PostCode":"2345","StreetType":"Boulevard"}],"Title":"Mr","LastName":"Sully"}}


After: {PersonalDetailsDTO={FirstName=Mark, LastName=Sully, Title=Mr, DOB=19811201, SecQuestion=Some Ques, SecAnswer=Some-Ans, Email=some@validemail.com, EmploymentListDTO=[{"Type":"Full-time","Probation":true}], AddressListDTO=[{"AddressType":"Residential","State":"NSW","StreetName":"MEL","StreetNumber":"2","StreetType":"Boulevard","Suburb":"Melbourne","UnitNumber":"345"}]}}


Looks like JsonPath is doing it's job and removing $.PersonalDetailsDTO.AddressListDTO.PostCode. However, there's something very obvious that bothers me:



Looking at the json string produced by .toString() in before and after case, JSONObject API printed a nice string in true json standard format with every double quotes "" present, while the JsonPath .toString produce a customer string format that has some elements in double quote "" while others are not and i can not use it further like JSONObject.



And what i noticed is that although JsonPath claim to accept "java.lang.Object" as parameter in many of its function, what it truely accept is something called "jsonProvider". Not sure if it's causing the weird .toString() behavior.



Anyway, does anyone know how get a nice formatted json string out of JsonPath APIs like remove(), put(), read() and many other? Or to convert the return value to something like JSONObject?



If you know any other Java lib that can do remove/modify element by json path, please feel free to recommand. Thank you!










share|improve this question














I am trying to implement a function to be able to remove or modify a json object base on a specified json path. For example, if i have a below json string/object:



{
"PersonalDetailsDTO": {
"FirstName": "Mark",
"LastName": "Sully",
"TotalDependent": "2",
"DOB": "19811212",
"SecQuestion": "Some Que",
"SecAnswer": "Some-Ans",
"Mobile": "0123456789",
"Email": "some@validemail.com",
"Title": "Mr",
"EmploymentListDTO": [
{
"Type": "Full-time",
"Probation": true
}
],
"AddressListDTO": [
{
"AddressType": "BUS",
"PostCode": "1234",
"State": "NSW",
"StreetName": "miller",
"StreetNumber": "111",
"StreetType": "Invalid",
"Suburb": "Sydney",
"UnitNumber": "Maximum"
}
]
}
}


And i want to remove element $.PersonalDetailsDTO.AddressListDTO.PostCode.



I've done quite some search, and the one lib i found is JsonPath: http://static.javadoc.io/com.jayway.jsonpath/json-path/2.2.0/com/jayway/jsonpath/JsonPath.html



So i wrote the below code:



public static void main(String args) {
// Prints "Hello, World" to the terminal window.
String jsonString = "{n" +
" "PersonalDetailsDTO": {n" +
" "FirstName":"Mark",n" +
" "LastName":"Sully",n" +
" "Title":"Mr",n" +
" "DOB":"19811201",n" +
" "SecQuestion":"Some Ques",n" +
" "SecAnswer":"Some-Ans",n" +
" "Email":"some@validemail.com",n" +
" "EmploymentListDTO": [n" +
" {n" +
" "Type": "Full-time",n" +
" "Probation": truen" +
" }n" +
" ],n" +
" "AddressListDTO": [n" +
" {n" +
" "AddressType": "Residential",n" +
" "PostCode": "2345",n" +
" "State": "NSW",n" +
" "StreetName": "MEL",n" +
" "StreetNumber": "2",n" +
" "StreetType": "Boulevard",n" +
" "Suburb": "Melbourne",n" +
" "UnitNumber": "345"n" +
" }n" +
" ]n" +
" } n" +
"}";

JSONObject jsonObject = new JSONObject(jsonString);
System.out.println("Before: " + jsonObject.toString());

JsonPath jp = JsonPath.compile("$.PersonalDetailsDTO.AddressListDTO[0].PostCode");
Configuration conf = Configuration.defaultConfiguration();
Object json = conf.jsonProvider().parse(jsonString);
System.out.println("After: " + jp.delete(json, conf).toString());
}


And the console log displays:



Before: {"PersonalDetailsDTO":{"EmploymentListDTO":[{"Type":"Full-time","Probation":true}],"SecAnswer":"Some-Ans","Email":"some@validemail.com","SecQuestion":"Some Ques","FirstName":"Mark","DOB":"19811201","AddressListDTO":[{"StreetName":"MEL","Suburb":"Melbourne","State":"NSW","StreetNumber":"2","UnitNumber":"345","AddressType":"Residential","PostCode":"2345","StreetType":"Boulevard"}],"Title":"Mr","LastName":"Sully"}}


After: {PersonalDetailsDTO={FirstName=Mark, LastName=Sully, Title=Mr, DOB=19811201, SecQuestion=Some Ques, SecAnswer=Some-Ans, Email=some@validemail.com, EmploymentListDTO=[{"Type":"Full-time","Probation":true}], AddressListDTO=[{"AddressType":"Residential","State":"NSW","StreetName":"MEL","StreetNumber":"2","StreetType":"Boulevard","Suburb":"Melbourne","UnitNumber":"345"}]}}


Looks like JsonPath is doing it's job and removing $.PersonalDetailsDTO.AddressListDTO.PostCode. However, there's something very obvious that bothers me:



Looking at the json string produced by .toString() in before and after case, JSONObject API printed a nice string in true json standard format with every double quotes "" present, while the JsonPath .toString produce a customer string format that has some elements in double quote "" while others are not and i can not use it further like JSONObject.



And what i noticed is that although JsonPath claim to accept "java.lang.Object" as parameter in many of its function, what it truely accept is something called "jsonProvider". Not sure if it's causing the weird .toString() behavior.



Anyway, does anyone know how get a nice formatted json string out of JsonPath APIs like remove(), put(), read() and many other? Or to convert the return value to something like JSONObject?



If you know any other Java lib that can do remove/modify element by json path, please feel free to recommand. Thank you!







java json jsonpath rest-assured-jsonpath






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 9:33









user1559625user1559625

1,07511741




1,07511741













  • Questions asking us to recommend a software library are off-topic for Stack Overflow as they tend to attract opinionated answers and spam

    – Michael
    Nov 13 '18 at 9:47



















  • Questions asking us to recommend a software library are off-topic for Stack Overflow as they tend to attract opinionated answers and spam

    – Michael
    Nov 13 '18 at 9:47

















Questions asking us to recommend a software library are off-topic for Stack Overflow as they tend to attract opinionated answers and spam

– Michael
Nov 13 '18 at 9:47





Questions asking us to recommend a software library are off-topic for Stack Overflow as they tend to attract opinionated answers and spam

– Michael
Nov 13 '18 at 9:47












1 Answer
1






active

oldest

votes


















0














I don't know JsonPath.



I think you should use jackson which is defacto standard lib when work with JSON in java



aproximate what you are going to do is:



String jsonString = "{"k1": {"k2":"v2"}";

ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree(jsonString);
actualObj.at("/k1/k2").getValueAsInt()


and replace getValueAsInt with any other function






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%2f53277865%2fwhat-is-the-java-library-to-remove-modify-a-json-object-based-on-json-path-or-h%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I don't know JsonPath.



    I think you should use jackson which is defacto standard lib when work with JSON in java



    aproximate what you are going to do is:



    String jsonString = "{"k1": {"k2":"v2"}";

    ObjectMapper mapper = new ObjectMapper();
    JsonNode actualObj = mapper.readTree(jsonString);
    actualObj.at("/k1/k2").getValueAsInt()


    and replace getValueAsInt with any other function






    share|improve this answer






























      0














      I don't know JsonPath.



      I think you should use jackson which is defacto standard lib when work with JSON in java



      aproximate what you are going to do is:



      String jsonString = "{"k1": {"k2":"v2"}";

      ObjectMapper mapper = new ObjectMapper();
      JsonNode actualObj = mapper.readTree(jsonString);
      actualObj.at("/k1/k2").getValueAsInt()


      and replace getValueAsInt with any other function






      share|improve this answer




























        0












        0








        0







        I don't know JsonPath.



        I think you should use jackson which is defacto standard lib when work with JSON in java



        aproximate what you are going to do is:



        String jsonString = "{"k1": {"k2":"v2"}";

        ObjectMapper mapper = new ObjectMapper();
        JsonNode actualObj = mapper.readTree(jsonString);
        actualObj.at("/k1/k2").getValueAsInt()


        and replace getValueAsInt with any other function






        share|improve this answer















        I don't know JsonPath.



        I think you should use jackson which is defacto standard lib when work with JSON in java



        aproximate what you are going to do is:



        String jsonString = "{"k1": {"k2":"v2"}";

        ObjectMapper mapper = new ObjectMapper();
        JsonNode actualObj = mapper.readTree(jsonString);
        actualObj.at("/k1/k2").getValueAsInt()


        and replace getValueAsInt with any other function







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 10:14

























        answered Nov 13 '18 at 10:06









        Maxim RudenkoMaxim Rudenko

        337




        337






























            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%2f53277865%2fwhat-is-the-java-library-to-remove-modify-a-json-object-based-on-json-path-or-h%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

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values