我想做的是 for each 订单取fulfillmentId,在fulfillments数组中获取相应的对象,并将其添加到订单对象中以代替fulfillmentId.

输入:

{
  "orders": [
    {
      "orderNo": "12345",
      "orderType": "online",
      "fulfillmentId": "123"
    },
    {
      "orderNo": "54321",
      "orderType": "in store",
      "fulfillmentId": "987"
    }
  ],
  "fulfillments": [
    {
      "fulfillmentId": "123",
      "address": "123 fake street",
      "type": "delivery"
    },
    {
      "fulfillmentId": "987",
      "address": "321 main street",
      "type": "pickup"
    }
  ]
}

我正在寻找的输出

{
  "orders": [
    {
      "orderNo": "12345",
      "orderType": "online",
      "fulfillment": {
        "fulfillmentId": "123",
        "address": "123 fake street",
        "type": "delivery"
      }
    },
    {
      "orderNo": "54321",
      "orderType": "in store",
      "fulfillment": {
        "fulfillmentId": "987",
        "address": "321 main street",
        "type": "pickup"
      }
    }
  ]
}

我还没有能够工作玩周围的震动变形演示.我不是100%确定这与内置的震动功能的可行性,但认为这应该是可能的.

以下是我到目前为止所得到的,没有触及fulfillment

[
  {
    "operation": "shift",
    "spec": {
      "orders": {
        "*": {
          "orderNo": "orders[&1].orderNo",
          "orderType": "orders[&1].orderType"
        }
      }
    }
  }
]

任何帮助将不胜感激.

推荐答案

您可以使用以下转换:

[
  { // generate two new objects which replicates of the objects
    // of "fulfillments" array with "fulfillmentId" valued keys 
    "operation": "shift",
    "spec": {
      "*s": { // represents the array "fulfillments" which ends with "-s"
        "@": "&", // replicate all elements within the input JSON
        "*": {
          "*": "@1,&(2,1)Id.&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "orders": {
        "@": "&", // replicate "orders" array
        "*": {
          "*Id": { // represents "fulfillmentId" which ends with "-Id"
            "*": {
              "@4,&": { // go 4 levels up the tree to reach the level of
                // "orders" and bring previously generated objects inside 
                "*": "&5[&4].&(3,1).&" // &5 stands for the literal "order"
                  // [&4] for indexes of the "order" array to produce arraywise results
                  // &(3,1) represents going 3 levels up the tree and pick 
                  //        1st asterisk replacement there
                  // & is the leaf node which replicates values of the attributes respectively
              }
            }
          }
        }
      }
    }
  },
  { // get rid of the extra "fulfillmentId" attribute(outer ones)
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "fulfillmentId": ""
        }
      }
    }
  }
]

站点https://jolt-demo.appspot.com/上的demo是:

enter image description here

Json相关问答推荐

从Razor Pages的AJAX Json呈现DataTables问题.Net GET

使用Jolt将字符串数组转换为JSON对象数组

kotlinx-serialization:如何将具有不同类型对象的JsonArray转换为同一个Class

jq:将一个数组与多个数组连接起来

如何强制仅有一个元素的数组在JSON中生成方括号

将请求中的数据推送到数组中

Kotlin Android Room 处理 Moshi TypeConverter 中的空对象列表

如何将 XML 转换为 PsCustomObject 以允许最终导出为 JSON?

单元测试球衣 Restful Services

从 Postgres 中的 json 对象中提取键、值

Android JSON 库的性能和可用性比较

使用 JSONObject 在 Java 中为以下 struct 创建嵌套 JSON 对象?

如何向(JSON)对象的原型添加方法?

使用 Node.js 对 JSON 中的字符串大小有限制吗?

POST:在 url 本身中发送 post 请求

在 JSON.NET 中序列化派生类时的字段顺序

严重:找不到媒体类型 = 应用程序/json、类型 = 类 com.jersey.jaxb.Todo、通用类型 = 类 com.jersey.jaxb.Todo 的 MessageBodyWriter

获取一个数字的 PHP 对象属性

强制 JSON.NET 在序列化 DateTime 时包含毫秒(即使 ms 组件为零)

从调试器获取 IntelliJ Idea 中的 JSON 对象