Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change values in a json file using XPath/JsonPath in java

here is the json file

{
    "session":
        {
            "name":"JSESSIONID",
            "value":"5864FD56A1F84D5B0233E641B5D63B52"
        },
    "loginInfo":
        {
            "loginCount":77,
            "previousLoginTime":"2014-12-02T11:11:58.561+0530"
        }
}

I want to change the value of name.by directly giving XPath/JsonPath Like

($.session.name).changevalue("MYSESSINID") this is just a Example

I am correctly using jackson library and using the below code for reading via XPath

ObjectMapper mapper = new ObjectMapper();

        Object jsonObj=mapper.readValue(new File(Json file), Object.class);
        Object name=PropertyUtils.getProperty(jsonObj, "session.name");
        System.out.println("Name:"+name);

so is their a way to change the name by XPath

PropertyUtils.setProperty(jsonObj, "session.value", "new value");

still in the file its not working.

like image 310
vaibhavcool20 Avatar asked Dec 02 '14 07:12

vaibhavcool20


People also ask

Can XPath be used on JSON?

Xpath of a JSON will allow “*” as a wildcard symbol for the member names and it is including the array. It is always applying to the data item of JSON, as we can say that in the same situation xpath is used as combination of an XML document.

What is difference between JSON & JsonPath?

JSONPath creates a uniform standard and syntax to define different parts of a JSON document. JSONPath defines expressions to traverse through a JSON document to reach to a subset of the JSON. This topic is best understood by seeing it in action. We have created a web page which can help you evaluate a JSONPath.

How do I specify JsonPath?

You use a JSONPath expression to traverse the path to an element in the JSON structure. You start at the root node or element, represented by $, and reach the required element in the JSON structure to extract data from it. You can use either the dot-notation or the bracket-notation to form the expressions.

What is Jayway JsonPath?

Jayway JsonPath. A Java DSL for reading JSON documents. Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation.


3 Answers

Using Jayways JsonPath you can:

private static final Configuration configuration = Configuration.builder()
    .jsonProvider(new JacksonJsonNodeJsonProvider())
    .mappingProvider(new JacksonMappingProvider())
    .build();

@Test
public void a_value_can_be_updated(){

    String originalJson = "{\n"
        + "\"session\":\n"
        + "    {\n"
        + "        \"name\":\"JSESSIONID\",\n"
        + "        \"value\":\"5864FD56A1F84D5B0233E641B5D63B52\"\n"
        + "    },\n"
        + "\"loginInfo\":\n"
        + "    {\n"
        + "        \"loginCount\":77,\n"
        + "        \"previousLoginTime\":\"2014-12-02T11:11:58.561+0530\"\n"
        + "    }\n"
        + "}";

    JsonNode updatedJson = JsonPath.using(configuration).parse(originalJson).set("$.session.name", "MYSESSINID").json();

    System.out.println(updatedJson.toString());
}

You can configure the default JsonProvider so you don't have to pass it in all calls.

like image 152
kalle Avatar answered Oct 19 '22 03:10

kalle


PropertyUtils.setProperty(jsonObj, "session.value", "new value");
        PropertyUtils.setProperty(jsonObj, "session.name", "new name");
        mapper.writeValue(Json File ,jsonObj);
like image 25
vaibhavcool20 Avatar answered Oct 19 '22 03:10

vaibhavcool20


the easiest way i found to exchange inside Json (When my Body is a JSONObject)

import com.jayway.jsonpath.JsonPath;


JsonPath.parse(Body).set(fieldPath, Value);
like image 3
Vladi Avatar answered Oct 19 '22 03:10

Vladi