JSON.CN让代码更轻
JSON 结构对比可运行案例

订阅套餐:解释 plans 首部插入导致的多处差异

数组按下标逐项比较。在 plans 开头插入记录后,后续元素位置移动,会产生多处修改和一个尾部新增;这不等于多条业务记录都发生了真实字段变更。需要按 ID 配对时应先在自己的业务逻辑中处理。

浏览器本地处理合成数据同场景 32 个案例 ↗

示例工作台

在完整工具中打开 ↗
JSON 结构对比修改下方数据后重新运行
运行结果
[
  {
    "op": "replace",
    "path": "/data/plans/0/planId",
    "oldValue": "demo-subscriptions-1",
    "value": "demo-inserted"
  },
  {
    "op": "replace",
    "path": "/data/plans/0/planName",
    "oldValue": "演示月度套餐",
    "value": "新增演示记录"
  },
  {
    "op": "replace",
    "path": "/data/plans/1/planId",
    "oldValue": "demo-subscriptions-2",
    "value": "demo-subscriptions-1"
  },
  {
    "op": "replace",
    "path": "/data/plans/1/planName",
    "oldValue": "演示年度套餐",
    "value": "演示月度套餐"
  },
  {
    "op": "replace",
    "path": "/data/plans/1/priceCents",
    "oldValue": 9000,
    "value": 900
  },
  {
    "op": "replace",
    "path": "/data/plans/1/available",
    "oldValue": false,
    "value": true
  },
  {
    "op": "replace",
    "path": "/data/plans/1/interval",
    "oldValue": "year",
    "value": "month"
  },
  {
    "op": "remove",
    "path": "/data/plans/1/entitlements/features/1",
    "oldValue": "team"
  },
  {
    "op": "replace",
    "path": "/data/plans/1/entitlements/seats",
    "oldValue": 5,
    "value": 1
  },
  {
    "op": "add",
    "path": "/data/plans/2",
    "value": {
      "planId": "demo-subscriptions-2",
      "planName": "演示年度套餐",
      "priceCents": 9000,
      "available": false,
      "interval": "year",
      "entitlements": {
        "features": [
          "export",
          "team"
        ],
        "seats": 5
      }
    }
  }
]

原例已核验,可修改输入后重新运行。

⌘ / Ctrl + Enter 运行
01

场景与操作说明

套餐价格、计费周期和权益应一起核对。此页面不购买、续订或取消任何服务,全部为合成套餐。

priceCents 是示例价格字段,比较不同 interval 前应统一时间口径。

  1. 核对 订阅套餐 的字段层级,保留 planId 的字符串类型。
  2. 修改一项输入并运行,观察结果中的值或路径如何变化。
  3. 载入对照样本,复现下面的变化;恢复原例会替换当前输入。
02

对照样本

对照样本交换 A 与 B。观察新增与删除的方向、修改前后值如何反转;没有差异时交换仍无差异。

查看对照输入
{
  "ok": true,
  "data": {
    "plans": [
      {
        "planId": "demo-inserted",
        "planName": "新增演示记录",
        "priceCents": 900,
        "available": true,
        "interval": "month",
        "entitlements": {
          "features": [
            "export"
          ],
          "seats": 1
        }
      },
      {
        "planId": "demo-subscriptions-1",
        "planName": "演示月度套餐",
        "priceCents": 900,
        "available": true,
        "interval": "month",
        "entitlements": {
          "features": [
            "export"
          ],
          "seats": 1
        }
      },
      {
        "planId": "demo-subscriptions-2",
        "planName": "演示年度套餐",
        "priceCents": 9000,
        "available": false,
        "interval": "year",
        "entitlements": {
          "features": [
            "export",
            "team"
          ],
          "seats": 5
        }
      }
    ],
    "summary": {
      "total": 2,
      "note": null
    }
  }
}
{
  "ok": true,
  "data": {
    "plans": [
      {
        "planId": "demo-subscriptions-1",
        "planName": "演示月度套餐",
        "priceCents": 900,
        "available": true,
        "interval": "month",
        "entitlements": {
          "features": [
            "export"
          ],
          "seats": 1
        }
      },
      {
        "planId": "demo-subscriptions-2",
        "planName": "演示年度套餐",
        "priceCents": 9000,
        "available": false,
        "interval": "year",
        "entitlements": {
          "features": [
            "export",
            "team"
          ],
          "seats": 5
        }
      }
    ],
    "summary": {
      "total": 2,
      "note": null
    }
  }
}

对照样本的实际结果

[
  {
    "op": "replace",
    "path": "/data/plans/0/planId",
    "oldValue": "demo-inserted",
    "value": "demo-subscriptions-1"
  },
  {
    "op": "replace",
    "path": "/data/plans/0/planName",
    "oldValue": "新增演示记录",
    "value": "演示月度套餐"
  },
  {
    "op": "replace",
    "path": "/data/plans/1/planId",
    "oldValue": "demo-subscriptions-1",
    "value": "demo-subscriptions-2"
  },
  {
    "op": "replace",
    "path": "/data/plans/1/planName",
    "oldValue": "演示月度套餐",
    "value": "演示年度套餐"
  },
  {
    "op": "replace",
    "path": "/data/plans/1/priceCents",
    "oldValue": 900,
    "value": 9000
  },
  {
    "op": "replace",
    "path": "/data/plans/1/available",
    "oldValue": true,
    "value": false
  },
  {
    "op": "replace",
    "path": "/data/plans/1/interval",
    "oldValue": "month",
    "value": "year"
  },
  {
    "op": "add",
    "path": "/data/plans/1/entitlements/features/1",
    "value": "team"
  },
  {
    "op": "replace",
    "path": "/data/plans/1/entitlements/seats",
    "oldValue": 1,
    "value": 5
  },
  {
    "op": "remove",
    "path": "/data/plans/2",
    "oldValue": {
      "planId": "demo-subscriptions-2",
      "planName": "演示年度套餐",
      "priceCents": 9000,
      "available": false,
      "interval": "year",
      "entitlements": {
        "features": [
          "export",
          "team"
        ],
        "seats": 5
      }
    }
  }
]