-1

below is json string :

 {
  "Data1": "",
  "Data2": {
    "id": 1234
  },
  "Data3": [
    {
      "id": "id2",
      "price": [
        {
          "id": "id3"
        }
      ],
      "account": {
        "id": "%s"
      },
      "product": {
        "extension": {
          "sub": "R"
        }
      },
      "reason": [
        {
          "extension": {}
        }
      ],
      "produracteristic": [
        {
          "name": "gt",
          "value": "none"
        }
      ],
      "extension": {

       }
    }
  ],

 "Data4":[
    ....
  }
}

I want to add data under json payload.Data3.extension want to add date:28-08-2023, so it will be ,

  "extension": {
 date:28-08-2023
        }

I have tried using java but that was only working for add element in json array. but in my case I am not adding in array . I am adding it to an object under array. Please help me on this

JsonPath pathToArray = JsonPath.compile("$.data3.extension");
documentContext.add(pathToArray, Collections.singletonMap("date", "28-08-2023"));
5
  • Data3 appears to be an array - are you adding the date to every element of the array? What JSON library are you using? Commented Aug 28, 2023 at 11:03
  • hi , data3 will not contain array , just key value pair data, Its possible using jsonpath? Commented Aug 28, 2023 at 12:09
  • Your provided JSON indicates that the key "Data3" maps to an array (indicated by the square braces[]). I'm not familiar with JsonPath. Baeldung has an intro article for it - see baeldung.com/guide-to-jayway-jsonpath Commented Aug 28, 2023 at 12:20
  • let me check, sorry my bad , data3 conatin array, yes i will add every element of the array Commented Aug 28, 2023 at 12:51
  • JSON Path is a query syntax. It retrieves nodes; it doesn't edit data. If you want to edit data, JSON Path is the wrong tool. Commented Aug 28, 2023 at 21:38

1 Answer 1

0

I have use the dependency named Fastjson, and found it easy to implementd it.
Follow is my code.

    public static void main(String[] args) {
//1.generate the json string from a object by fastjson
            String s = JSON.toJSONString(new Person());
            System.out.println("s = " + s);
//2.fastjson transfer the jsonStr to a JSONObject
            JSONObject jsonObject = JSONObject.parseObject(s);
//3. use the JSONObject to add a field named field1 and the value is 111
            JSONObject address = jsonObject.getJSONObject("address");
            address.put("field1", 111);

            System.out.println("jsonObject = " + jsonObject);

        }

The output is:

s = {"address":{"city":"2","province":"1"},"age":18,"gender":"man","name":"zhangsan"}
jsonObject = {"address":{"field1":111,"province":"1","city":"2"},"gender":"man","name":"zhangsan","age":18}

If you want to use this ,you can add this in your maven files.

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>2.0.32</version>
</dependency>
Sign up to request clarification or add additional context in comments.

3 Comments

is it possible using json path? beacues I have path , which is coming as input
I an afraid i have never seen the same function API in Json Path.
If you don't want to add the dependency. I hava two other method , one is use the String.replace() method to do it, the other one is convert the json String to a nested Map , and add the thing you want to added in the Map structure, and then convert it back to a new Json String

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.