我有一个包含许多节点的 Json,其中一些节点包含大小大于 1 的数组。我正在尝试编写一些代码将数组大小修剪为 1,并确保包含所有数组元素的所有属性在那个数组元素中。然后我将使用“清理”的 json 来生成一个 avro 模式,用于 kafka 流 API。实际的 json 非常大,因此为了使其易于管理,我正在尝试这样做。
以下代码已尝试使用 gson,但我对 gson 或 jackson 没有偏好。
这是我的尝试
private static void traverseJsonObject(JsonObject source,JsonObject compactedJson,String[] currentJsonPath) {
String parentKey = currentJsonPath[0];
source.keySet().forEach(key ->
{
Object keyValue = source.get(key);
if (keyValue instanceof JsonObject) {
currentJsonPath[0] += "."+key;
traverseJsonObject((JsonObject) keyValue,compactedJson,currentJsonPath);
}
if (keyValue instanceof JsonArray) {
JsonArray srcArray = (JsonArray) keyValue;
// get the size of the array
// if the array size is 0 or 1
// continue
// if array size is > 1
// call compactArray method,passing the value ( jsonarray )
// this method should return an array of size 1
// remove the node from compactedJson
// add the returned array to compactedJson
if(srcArray.size() > 1) {
JsonObject firstArrayElement = (JsonObject) srcArray.get(0);
JsonArray merged = compactArray(srcArray,firstArrayElement);
//*********** **This is where my error occurs**
// I would ideally like to build a jsonpath for the key at this point,// but I am NOT able to build a correct jsonpath here
JsonElement elementAtKey = compactedJson.get(key);
JsonArray compactedArrayAtKey = (JsonArray) compactedJson.get(key);
int srcArraySize = srcArray.size();
for (int i = 0; i < srcArraySize; i++) {
compactedArrayAtKey.remove(i);
}
compactedArrayAtKey.addAll(merged);
compactedJson.add(key,compactedArrayAtKey);
} //remove the key node
// add jsonarray at key
}
// iterate over keys of input json
// If the value for the key is of type jsonarray
// get the size of the array
// if the array size is 0 or 1
// continue
// if array size is > 1
// call compactArray method,passing the value ( jsonarray )
// this method should return an array of size 1
// remove the node from compactedJson
// add the returned array to compactedJson
});
}
private static JsonArray compactArray(JsonArray multiDimArray,JsonObject firstArrayElement) {
IntStream.range(0,multiDimArray.size()).forEach(index -> {
JsonElement srcElem = multiDimArray.get(index);
JsonObject srcAtIndex = (JsonObject) srcElem;
Set<String> sourceKeySet = srcAtIndex.keySet();
for (String key : srcAtIndex.keySet()) {
if(!firstArrayElement.has(key)) {
firstArrayElement.addProperty(key,String.valueOf(srcAtIndex.get(key)));
}
}
});
JsonArray oneElementArray = new JsonArray();
oneElementArray.add(firstArrayElement);
return oneElementArray;
}
这是我的错误发生的地方
JsonElement elementAtKey = compactedJson.get(key);
示例 JSON
{
"TopmostNode" {
"Level1-1"{
"level2-1" {
"attr2-1-1": "value2-2-1",},"level2-2" {
"attr2-2-1": "value2-2-1","level3-1": [
{
"attr3-1-1-1": "value3-1-1-1"
"attr3-1-1-2": "value3-1-1-2"
},{
"attr3-1-1-1": "value3-1-1-1"
"attr3-1-2-1": "value3-1-2-1"
}
]
}
}
}
}
预期产出
{
"TopmostNode" {
"Level1-1"{
"level2-1" {
"attr2-1-1": "value2-2-1","level3-1": [
{
"attr3-1-1-1": "value3-1-1-1"
"attr3-1-1-2": "value3-1-1-2"
"attr3-1-2-1": "value3-1-2-1"
}
]
}
}
}
}
理想情况下,此时我想为密钥构建一个 jsonpath,但我无法在这里构建正确的 jsonpath
如何在遍历 json 对象时构建 jsonpath 或
是否有另一种方法可以将所有数组修剪到大小为 1?
谢谢