2

I'm trying to add one more index value to the path:

$.data.library.category[2].book using jayway jsonpath in following Json,

 "data":{
    "library":{
    "class":"CRED",
    "category":[{
                "book":"java 2.0"
            },
            {
                "book":"java 3.0"
            }]
    }

but i'm not getting updated response in result json.

My java code:

Configuration conf = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL).addOptions(Option.SUPPRESS_EXCEPTIONS);  
DocumentContext documentContext = JsonPath.using(conf).parse(sampleJson);
documentContext.set(JsonPath.compile("$.data.library.category[2].book"), "java 3.0");

I have checked also with documentContext.add. Nothing works out. Since the array has 0 and 1 index, i can update the value there by JsonPath. But it dont have 2nd index, so not updating. But I need to insert new index in the last of given json as per jsonpath.

1
  • I think the JsonParser use a LIST as backend for the JSON Array. So your have to use .get(index). Commented Jun 22, 2020 at 15:27

2 Answers 2

5

Path $.data.library.category[2].book means that you want to update 3-rd element in array. But there is no 3-rd element yet. You need to create it first. You can add new element to array using $.data.library.category path. By providing a Map object you can define all required keys and values. See below example:

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;

import java.io.File;
import java.util.Collections;

public class JsonPathApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        Configuration conf = Configuration.defaultConfiguration()
                .addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL)
                .addOptions(Option.SUPPRESS_EXCEPTIONS);
        DocumentContext document = JsonPath.using(conf).parse(jsonFile);

        JsonPath pathToArray = JsonPath.compile("$.data.library.category");

        document.add(pathToArray, Collections.singletonMap("book", "java 3.1"));
        document.add(pathToArray, Collections.singletonMap("book", "java 3.2"));

        System.out.println(document.jsonString());
    }
}

Above code prints below JSON:

{
   "data":{
      "library":{
         "class":"CRED",
         "category":[
            {
               "book":"java 2.0"
            },
            {
               "book":"java 3.0"
            },
            {
               "book":"java 3.1"
            },
            {
               "book":"java 3.2"
            }
         ]
      }
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi , in this scenario it wont work for all cases right? For Example: you are hard coding path : $.data.library.category But, same scenario, if Library also an Array i need to Insert in following path: $.data.library[2].category[2] how we achieve this?
@MageshMagi, you need to update a question and describe all corner cases you want to handle.
-1

I believe that Json class from https://github.com/karatelabs/karate can work for you. Given

String json = {
  "data": {
    "library": {
      "class": "CRED",
      "category": [
        {
          "book": "java 2.0"
        },
        {
          "book": "java 3.0"
        }
      ]
    }
  }
}

What you need to do is

Json.of(json).set("$.data.library.category[2].book", "java 3.1")

And your output will be

{
  "data": {
    "library": {
      "class": "CRED",
      "category": [
        {
          "book": "java 2.0"
        },
        {
          "book": "java 3.0"
        },
        {
          "book": "java 3.1"
        }
      ]
    }
  }
}

1 Comment

Unfortunately this will not worked for the scenario where library is also an Array, such as wanted to add $.data.library[1].category[0].book. I believe that this can be done, but maybe is not as simple as just using the set method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.