【问题标题】:Create JSON Object from various JSON Path从各种 JSON 路径创建 JSON 对象
【发布时间】:2021-03-10 08:17:53
【问题描述】:

我们需要从提供的各种 JSONPaths 创建 JSON 对象。例如,下面是两条路径,以及该路径的值存在于要创建的新 JSON 对象中。

$.student.firstName = "Abc"
$.student.subject['physics'].mark=100

是否有任何 java 开源库可以帮助仅从该路径创建结果对象?

{
 "student":{
 "firstName":"Abc",
 "physics":{
   "mark":100
  }
 }
}

我们探索了某些库,例如 JSONPath。它具有解析 JSON 文件的选项,但没有选项从路径递归创建 JSON 文件。

【问题讨论】:

  • 你试过了吗?也许这篇文章对你来说很有趣:stackoverflow.com/q/51971642/1140748
  • JSON 路径本身就是一种查询语法。它不适用于转换或构建新对象。我不相信 JSON Path 是您正在寻找的技术。您是从现有的 JSON 对象开始的吗?
  • 我理解 JSONPath 只是在 JSON 中给出了路径。我想从各种路径创建一个新的 JSON.. 这样在我完成路径评估和基于此的对象创建之后,我就有了一个完整的 JSON 对象和数据
  • @alain.janinm,看过这个例子。它正在使用 JsonPointer。与 JsonPath 相比,它的选项有限。

标签: java json jsonpath


【解决方案1】:

基于 this fork from JsonPath 添加从 json 路径创建新 json 节点的功能,您可以执行以下操作:

//    columnsMap = key: jsonPath node, value: value to be added to node

String json = "{ }";
for (Entry<String, String> entry : columnsMap.entrySet()) {
  JsonPath compiledPath = JsonPath.compile(entry.getKey());
  Object parsedJsonPath =
      compiledPath.set(configuration.jsonProvider().parse(json), entry.getValue(), configuration);
  json = JsonPath.parse(parsedJsonPath).jsonString();
}
return json;

输入(columnsMap):

{vendor_account_ids=1234567,4567123,789785,
 charges[0].vendor_charge_id=CHARGE-01,
 charges[1].vendor_charge_id=CHARGE-02,
 charges[0].type=LOAN_DEDUCTION,
 charges[0].conditions.alternative_delivery_date=false,
 charges[0].conditions.depends_on_vendor_charge_ids[0]=ID01,
 charges[0].conditions.depends_on_vendor_charge_ids[1]=ID02,
 charges[0].conditions.order_total.maximum_value=123.45,
 charges[0].conditions.payment_method=BANK_SLIP,
 charges[0].conditions.scaled_from=ORDER_TOTAL,
 charges[0].conditions.product_attributes.is_alcoholic=false,
 charges[0].conditions.payment_terms[0]=1,
 charges[0].conditions.payment_terms[1]=30,
 charges[0].output.scope=LINE_ITEM,
 charges[0].output.apply_to=DISCOUNT_AMOUNT,
 charges[0].output.type=PERCENT,
 charges[0].output.ratio=FIXED,
 charges[0].output.ranges[0].from=1,
 charges[0].output.ranges[0].value=10,
 charges[0].output.vendor_item_ids[0]=SKU001,
 charges[0].output.vendor_item_ids[1]=SKU002
}

输出(返回的json):

{
    "vendor_account_ids": "1234567,4567123,789785",
    "charges": [
        {
            "vendor_charge_id": "CHARGE-01",
            "type": "LOAN_DEDUCTION",
            "conditions": {
                "alternative_delivery_date": "false",
                "depends_on_vendor_charge_ids": [
                    "ID01",
                    "ID02"
                ],
                "order_total": {
                    "maximum_value": "123.45"
                },
                "payment_method": "BANK_SLIP",
                "scaled_from": "ORDER_TOTAL",
                "product_attributes": {
                    "is_alcoholic": "false"
                },
                "payment_terms": [
                    "1",
                    "30"
                ]
            },
            "output": {
                "scope": "LINE_ITEM",
                "apply_to": "DISCOUNT_AMOUNT",
                "type": "PERCENT",
                "ratio": "FIXED",
                "ranges": [
                    {
                        "from": "1",
                        "value": "10"
                    }
                ],
                "vendor_item_ids": [
                    "SKU001",
                    "SKU002"
                ]
            }
        },
        {
            "vendor_charge_id": "CHARGE-02"
        }
    ]
}

【讨论】:

    【解决方案2】:

    使用 lodash/set 提供了一个简单的解决方案,例如:

    const set = require('lodash/set');
    const object = {};
    paths.forEach(cmsPath => set(cmsObject, cmsPath, null));
    

    【讨论】:

      【解决方案3】:

      Jayway Jsonpath,它有JSON生成功能。

      你应该使用“feature/transformation-api”分支来使用transfomation api。

      遵循规则生成与您发布的完全相同的 json

      {
      "pathMappings": [{
              "target": "$.student.firstName",
              "additionalTransform": {
                  "constantSourceValue": "Abc"
              }
          },
          {
              "target": "$.student.physics.mark",
              "additionalTransform": {
                  "constantSourceValue": 100
              }
          }
      ]
      

      }

      【讨论】:

        猜你喜欢
        • 2011-06-17
        • 2021-03-21
        • 1970-01-01
        • 1970-01-01
        • 2022-01-11
        • 2021-12-30
        • 1970-01-01
        • 2019-06-02
        • 2014-07-12
        相关资源
        最近更新 更多