英文:
Rewrite json via json path library in different levels with the same "key"
问题
I understand that you want assistance with a JSON manipulation task in Java using the json-path library. Here's the relevant code part that you'd like to modify:
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);
// Here you want to set the nextUrl for each specific item, but it's not working.
// jsonContext.set("$..next[" + i + "]", nextUrl);
}
}
You mentioned that you want to modify different "next" values in the JSON array with different URLs, but the code you provided is not working as expected. You also considered using a predicate, but it returned the entire JSON block instead of just the "next" value.
To achieve your goal, you can modify the "next" values in the JSON array individually by using a different JSON path for each one. You can construct the JSON path dynamically based on the index i
. Here's how you can do it:
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);
// Construct the JSON path dynamically for each "next" value
String jsonPath = "$.." + nextJsonPath;
jsonContext.set(jsonPath, nextUrl);
}
}
This code will set each "next" value in the JSON array to the corresponding nextUrl
using a dynamically constructed JSON path based on the index i
.
英文:
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.
Any idea what to do?
答案1
得分: 1
你正在寻找DocumentContext
类的map
函数。
示例:
jsonContext.map("$..next", (Object object, Configuration configuration) -> {
return doCustomTransformation(object.toString());
});
英文:
You are looking for the map
function of the DocumentContext
class.
example:
jsonContext.map("$..next", (Object object, Configuration configuration) -> {
return doCustomTransformation(object.toString());
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论