lombocska
lombocska

Reputation: 222

Rewrite json via json path library in different levels with the same "key"

Let's assume that we have a json like this one

{
  "data": [
    {
      "id": "...",
      "name": "...",
      "participants": {
        "data": [
          {
            "username": "...",
            "id": "...",
            "user_id": "..."
          },
          {
            "username": "...",
            "id": "...",
            "user_id": "..."
          }
        ]
      },
      "messages": {
        "data": [
          {
            "id": "...",
            "created_time": ...",
            "message": "...",
            "from": {
              "username": "...",
              "id": "...",
              "user_id": "..."
            },
            "to": {
              "data": [
                {
                  "username": "...",
                  "id": "...",
                  "user_id": "..."
                }
              ]
            }
          },
          {
            "id": "...",
            "created_time": "...",
            "message": "...",
            "from": {
              "username": "...",
              "id": "...",
              "user_id": "..."
            },
            "to": {
              "data": [
                {
                  "username": "...",
                  "id": "...",
                  "user_id": "..."
                }
              ]
            }
          },
          {
            "id": "...",
            "created_time": "...",
            "message": "...",
            "from": {
              "username": "...",
              "id": "...",
              "user_id": "..."
            },
            "to": {
              "data": [
                {
                  "username": "...",
                  "id": "...",
                  "user_id": "..."
                }
              ]
            }
          },
          {
            "id": "...",
            "created_time": "...",
            "message": "...",
            "from": {
              "username": "...",
              "id": "...",
              "user_id": "..."
            },
            "to": {
              "data": [
                {
                  "username": "...",
                  "id": "...",
                  "user_id": "..."
                }
              ]
            }
          },
          {
            "id": "...",
            "created_time": "...",
            "message": "...",
            "from": {
              "username": "...",
              "id": "...",
              "user_id": "..."
            },
            "to": {
              "data": [
                {
                  "username": "...",
                  "id": "...",
                  "user_id": "..."
                }
              ]
            }
          }],
        "paging": {
          "cursors": {
            "after": "..."
          },
          "next": "https://host/v7.0/id/messages?access_token=...fields=id%2Ccreated_time%2Cmessage%2Cfrom%2Cto%2Cis_unsupported%2Cstory%2Cattachments%2Creactions%2Cshares%7Blink%7D&limit=25&after=ZAXlKamRYSnpiM0lpT2lJeU9UTTVNell5T0RnM05qQTFOakkyTmpReU1qRTBPVFV5TWpZAME1EWXdNVEE0T0NKOQZDZD"
        }
      }
    }
  ],
  "paging": {
    "cursors": {
      "after": "..."
    },
    "next": "https://host/v7.0/id/conversations?access_token=...&fields=id%2Cname%2Cparticipants%2Cmessages%7Bid%2Ccreated_time%2Cmessage%2Cfrom%2Cto%2Cis_unsupported%2Cstory%2Cattachments%2Creactions%2Cshares%7Blink%7D%7D%2Cupdated_time&platform=instagram&limit=1&after=ZAXlKMGFXMWxjM1JoYlhBaU9qRTFPVFkwTlRjME1qVXNJblJvY21WaFpBRjlwWkFDSTZAJak0wTURJNE1qTTJOamcwTVRjeE1ETXdNRGswT1RFeU9ERXlORGt4T0RRNE16SXdPVGd3TUNKOQZDZD"
  }
}

The used java json path library

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>2.4.0</version>
        </dependency>

During a sanitization process, this json string should be modified.

        ...
        JSONArray jsonPathNextUrl = jsonContext.read(NEXT_URL_JSON_PATH_REGEX);
        ...
        if (isNotEmpty(jsonPathNextUrl)) {
            for (int i = 0; i < jsonPathNextUrl.size(); i ++) {
                 String nextJsonPath = jsonPathNextUrl.get(i).toString();
                final String nextUrl = rewritePaging(nextJsonPath, replacementUrl);
//                jsonContext.set("$..next" + "[" + i + "]", nextUrl);
            }
        }

My problem is that I don't want to modify all "next" value with the same value in this JsonArray, but different ones.

But the jsonContext.set(...[i]) with the specific item does not work.

I had an idea to use the predicate functionality of json path like this one

jsonContext.read("$..[?(@.next)]")

but it gives back the json block instead of just the exact next value, thus I cannot compare it to the current searched value that I have from the read process. enter image description here

Any idea what to do? :)

Upvotes: 1

Views: 324

Answers (1)

csenga
csenga

Reputation: 4114

You are looking for the map function of the DocumentContext class.

example:

jsonContext.map("$..next", (Object object, Configuration configuration) -> {
    return doCustomTransformation(object.toString());
});

Upvotes: 1

Related Questions