Magesh Magi
Magesh Magi

Reputation: 21

How to Add New Array Index in JSON using JsonPath?

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.

Upvotes: 2

Views: 8186

Answers (2)

Rodrigo Bittencourt
Rodrigo Bittencourt

Reputation: 97

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

Upvotes: -1

Michał Ziober
Michał Ziober

Reputation: 38655

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

Upvotes: 5

Related Questions