json 我需要对给定的输入进行震动变换,照片数组本质上是动态的

pgpifvop  于 2022-12-15  发布在  其他
关注(0)|答案(3)|浏览(150)

如果id值是0,那么我不希望在输出。照片数组是动态的性质。这里它是3,它可以是3或4或5任何。您的帮助,这是非常感谢。谢谢。
输入JSON

{
  "Entity": {
    "card": {
      "cardNo": "123456789",
      "cardStatus": "10",
      "cardAddress": "UK",
      "cardAddress1": "US",
      "cardCity": "mk",
      "name": "RAM",
      "lastName": "ABU",
      "name1": "RAM1",
      "lastName1": "ABU1",
      "Id1": "0",
      "Id2": 0,
      "Id3": 0
    },
    "Photos": [
      {
        "Id": 327703,
        "Caption": "TEST>> photo 1",
        "Url": "http://bob.com/0001/327703/photo.jpg"
      },
      {
        "Id": 327704,
        "Caption": "TEST>> photo 2",
        "Url": "http://bob.com/0001/327704/photo.jpg"
      },
      {
        "Id": 327704,
        "Caption": "TEST>> photo 2",
        "Url": "http://bob.com/0001/327704/photo.jpg"
      }
    ]
  }
}

输出JSON

{
  "tab": {
    "text": "123456789"
  },
  "address": [
    {
      "add": "UK",
      "add2": "US",
      "mk": "mk"
    }
  ],
  "Photos": [
    {
      "caption2": "http.1.com",
      "no": "0"
    },
    {
      "caption2": "http.2.com",
      "no": "0"
    },
    {
      "caption2": "http.3com",
      "no": "0"
    },
    {
      "caption2": "TEST>> photo 1",
      "no": "327703"
    },
    {
      "caption2": "TEST>> photo 2",
      "no": "327704"
    },
    {
      "caption2": "TEST>> photo 2",
      "no": "327704"
    }
  ]
}

应为JSON

{
  "tab": {
    "text": "123456789"
  },
  "address": [
    {
      "add": "UK",
      "add2": "US",
      "mk": "mk"
    }
  ],
  "Photos": [
    {
      "caption2": "TEST>> photo 1",
      "no": "327703"
    },
    {
      "caption2": "TEST>> photo 2",
      "no": "327704"
    },
    {
      "caption2": "TEST>> photo 2",
      "no": "327704"
    }
  ]
}

震动规格使用

[
  {
    "operation": "shift",
    "spec": {
      "Entity": {
        "card": {
          "cardNo": "tab.text",
          "cardAddress": "address[0].add",
          "cardAddress1": "address[0].add2",
          "cardC*": "address[0].mk",
          "Id1": "Photos.no",
          "#http.1.com": "Photos.caption2",
          "Id2": "Photos.no",
          "#http.2.com": "Photos.caption2",
          "Id3": "Photos.no",
          "#http.3com": "Photos.caption2"
        },
        "Photos": {
          "*": {
            "Id": "Photos.no",
            "Caption": "Photos.caption2"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "tab": "&",
      "address": "&",
      "Photos": {
        "*": {
          "*": {
            "@": "&3[&1].&2"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Photos": {
        "*": {
          "no": "=toString"
        }
      }
    }
  }
]

我在想一个如果需要逻辑,如果值是0,那么就不传递,否则就像这样传递。

bkhjykvo

bkhjykvo1#

您可以首先转换在公共节点下动态收集的独立属性,然后使用条件逻辑,例如

[
  {
    "operation": "shift",
    "spec": {
      "Entity": {
        "card": {
          "cardNo": "tab.text",
          "*Addres*|*City": {
            "*": {
              "@1": "address[#1].&2"
            }
          },
          "Id*": {// enumerate the members of the object in a dynamical manner
            "*": {
              "#caption2": "Photos&2[#1].http\\.&(2,1)\\.com", // &(2,1) could be used RHS
              "@1": "Photos&2[#1].no"
            }
          }
        },
        "*": {
          "*": {
            "Id": "&2[&1].no", // rename "Id" to "no"
            "*": "&2[&1].&"
          }
        }
      }
    }
  },
  { // combine all objects related to Photoes under common array
    "operation": "shift",
    "spec": {
      "*": "&",
      "Photo*": {
        "*": {
          "@": "Photoes"
        }
      }
    }
  },
  { // label objects of photoes by their respective "no" values
    "operation": "shift",
    "spec": {
      "*": "&",
      "Photoes": {
        "*": {
          "http*": {
            "$": "&3[&2].@(2,no).@(0)" // exchange key-value pairs
          },
          "*": "&2[&1].@(1,no).&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "Photoes": {
        "*": {
          "0": "rem&2", // remPhotoes tagged stuff will be removed
          "*": {
            "@": "&3[]"
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "remPhotoes": ""
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Photoes": {
        "*": {
          "no": "=toString"
        }
      }
    }
  }
]
cyvaqqii

cyvaqqii2#

嘿Saurabh我不能正确理解您的请求,但这可能会帮助您解决您的查询。我已经为您的预期输出创建了一个规范,我已经在Photos[]上保留了一个if else条件。否,如果它是0,它不会填充字段,您可以检查下面的规范和输入:
输入:

{
  "Entity": {
    "card": {
      "cardNo": "123456789",
      "cardStatus": "10",
      "cardAddress": "UK",
      "cardAddress1": "US",
      "cardCity": "mk",
      "name": "RAM",
      "lastName": "ABU",
      "name1": "RAM1",
      "lastName1": "ABU1",
      "Id1": "0",
      "Id2": 0,
      "Id3": 0
    },
    "Photos": [
      {
        "Id": 0,
        "Caption": "TEST>> photo 1",
        "Url": "http://bob.com/0001/327703/photo.jpg"
      },
      {
        "Id": 327704,
        "Caption": "TEST>> photo 2",
        "Url": "http://bob.com/0001/327704/photo.jpg"
      },
      {
        "Id": 327704,
        "Caption": "TEST>> photo 2",
        "Url": "http://bob.com/0001/327704/photo.jpg"
      }
    ]
  }
}

质量标准:

[
  {
    "operation": "shift",
    "spec": {
      "Entity": {
        "card": {
          "cardNo": "tab.text",
          "cardAddress": "address.[#2].add",
          "cardAddress1": "address.[#2].add2",
          "cardCity": "address.[#2].mk"
        },
        "Photos": {
          "*": {
            "Id": "Photos.[&1].no",
            "Caption": "Photos.[&1].caption2"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Photos": {
        "*": {
          "no": "=toString"
        }
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "*": "&",
      "Photos": {
        "*": {
          "no": {
            "0": {
              "@(2,no)": "Trash"
            },
            "*": {
              "@(2,no)": "p.Photos.[#4].no",
              "@(2,caption2)": "p.Photos.[#4].caption2"
            }
          }
        }
      }
    }
   }, {
    "operation": "remove",
    "spec": {
      "Trash": ""
    }
   },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
},
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "p": {
        "Photos": "Photos"
      }
    }
   }
]

eqoofvh9

eqoofvh93#

您可以考虑使用另一个库 Josson 通过更短的转换语句来完成相同的工作。
https://github.com/octomix/josson

荒漠化

Josson josson = Josson.fromJsonString(
    "{" +
    "  \"Entity\": {" +
    "    \"card\": {" +
    "      \"cardNo\": \"123456789\"," +
    "      \"cardStatus\": \"10\"," +
    "      \"cardAddress\": \"UK\"," +
    "      \"cardAddress1\": \"US\"," +
    "      \"cardCity\": \"mk\"," +
    "      \"name\": \"RAM\"," +
    "      \"lastName\": \"ABU\"," +
    "      \"name1\": \"RAM1\"," +
    "      \"lastName1\": \"ABU1\"," +
    "      \"Id1\": \"0\"," +
    "      \"Id2\": 0," +
    "      \"Id3\": 0" +
    "    }," +
    "    \"Photos\": [" +
    "      {" +
    "        \"Id\": 0," +
    "        \"Caption\": \"TEST>> photo 1\"," +
    "        \"Url\": \"http://bob.com/0001/327703/photo.jpg\"" +
    "      }," +
    "      {" +
    "        \"Id\": 327704," +
    "        \"Caption\": \"TEST>> photo 2\"," +
    "        \"Url\": \"http://bob.com/0001/327704/photo.jpg\"" +
    "      }," +
    "      {" +
    "        \"Id\": 327704," +
    "        \"Caption\": \"TEST>> photo 2\"," +
    "        \"Url\": \"http://bob.com/0001/327704/photo.jpg\"" +
    "      }" +
    "    ]" +
    "  }" +
    "}");

转型

JsonNode node = josson.getNode(
    "Entity.map(" +
    "  tab: map(text: card.cardNo)," +
    "  address: wrap(card.map(" +
    "    add: cardAddress," +
    "    add2: cardAddress1," +
    "    cardCity:: cardCity))," +
    "  Photos[Id!=0]*.map(" +
    "    caption2: Caption," +
    "    no: toText(Id))" +
    ")");
System.out.print(node.toPrettyString());

产出

{
  "tab" : {
    "text" : "123456789"
  },
  "address" : [ {
    "add" : "UK",
    "add2" : "US",
    "mk" : "mk"
  } ],
  "Photos" : [ {
    "caption2" : "TEST>> photo 2",
    "no" : "327704"
  }, {
    "caption2" : "TEST>> photo 2",
    "no" : "327704"
  } ]
}

相关问题