使用不同级别的JSON路径库重写JSON,使用相同的“key”

huangapple go评论88阅读模式
英文:

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

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

The used java json path library

        &lt;dependency&gt;
            &lt;groupId&gt;com.jayway.jsonpath&lt;/groupId&gt;
            &lt;artifactId&gt;json-path&lt;/artifactId&gt;
            &lt;version&gt;2.4.0&lt;/version&gt;
        &lt;/dependency&gt;

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 &lt; jsonPathNextUrl.size(); i ++) {
                 String nextJsonPath = jsonPathNextUrl.get(i).toString();
                final String nextUrl = rewritePaging(nextJsonPath, replacementUrl);
//                jsonContext.set(&quot;$..next&quot; + &quot;[&quot; + i + &quot;]&quot;, 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(&quot;$..[?(@.next)]&quot;)

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.
使用不同级别的JSON路径库重写JSON,使用相同的“key”

Any idea what to do? 使用不同级别的JSON路径库重写JSON,使用相同的“key”

答案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(&quot;$..next&quot;, (Object object, Configuration configuration) -&gt; {
    return doCustomTransformation(object.toString());
});

huangapple
  • 本文由 发表于 2020年8月6日 18:45:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63281923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定