1

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
Magesh Magi
  • 11
  • 1
  • 2

2 Answers2

2

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"
            }
         ]
      }
   }
}
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • 1
    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? – Magesh Magi Jun 23 '20 at 11:03
  • @MageshMagi, you need to update a question and describe all corner cases you want to handle. – Michał Ziober Jun 24 '20 at 12:44
-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"
        }
      ]
    }
  }
}
  • 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 – Rodrigo Bittencourt Jun 07 '22 at 21:53