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

账单明细:解释 invoices 首部插入导致的多处差异

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

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

示例工作台

在完整工具中打开 ↗
JSON 结构对比修改下方数据后重新运行
运行结果
[
  {
    "op": "replace",
    "path": "/data/invoices/0/invoiceId",
    "oldValue": "demo-invoices-1",
    "value": "demo-inserted"
  },
  {
    "op": "replace",
    "path": "/data/invoices/0/title",
    "oldValue": "演示服务账单",
    "value": "新增演示记录"
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/invoiceId",
    "oldValue": "demo-invoices-2",
    "value": "demo-invoices-1"
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/title",
    "oldValue": "演示配额账单",
    "value": "演示服务账单"
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/amountCents",
    "oldValue": 3500,
    "value": 12000
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/settled",
    "oldValue": false,
    "value": true
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/currency",
    "oldValue": "USD",
    "value": "CNY"
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/lines/items/0/name",
    "oldValue": "演示配额",
    "value": "演示服务"
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/lines/items/0/cents",
    "oldValue": 3500,
    "value": 12000
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/lines/taxIncluded",
    "oldValue": false,
    "value": true
  },
  {
    "op": "add",
    "path": "/data/invoices/2",
    "value": {
      "invoiceId": "demo-invoices-2",
      "title": "演示配额账单",
      "amountCents": 3500,
      "settled": false,
      "currency": "USD",
      "lines": {
        "items": [
          {
            "name": "演示配额",
            "cents": 3500
          }
        ],
        "taxIncluded": false
      }
    }
  }
]

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

⌘ / Ctrl + Enter 运行
01

场景与操作说明

账单样本用整数分表示金额,并明确币种。下面的两条账单各有明细数组,适合练习提取账单号、区分金额数字与字符串,以及比较币种或明细变化。

amountCents 是最小货币单位的示例整数;不同币种的最小单位需另行核对。

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

对照样本

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

查看对照输入
{
  "ok": true,
  "data": {
    "invoices": [
      {
        "invoiceId": "demo-inserted",
        "title": "新增演示记录",
        "amountCents": 12000,
        "settled": true,
        "currency": "CNY",
        "lines": {
          "items": [
            {
              "name": "演示服务",
              "cents": 12000
            }
          ],
          "taxIncluded": true
        }
      },
      {
        "invoiceId": "demo-invoices-1",
        "title": "演示服务账单",
        "amountCents": 12000,
        "settled": true,
        "currency": "CNY",
        "lines": {
          "items": [
            {
              "name": "演示服务",
              "cents": 12000
            }
          ],
          "taxIncluded": true
        }
      },
      {
        "invoiceId": "demo-invoices-2",
        "title": "演示配额账单",
        "amountCents": 3500,
        "settled": false,
        "currency": "USD",
        "lines": {
          "items": [
            {
              "name": "演示配额",
              "cents": 3500
            }
          ],
          "taxIncluded": false
        }
      }
    ],
    "summary": {
      "total": 2,
      "note": null
    }
  }
}
{
  "ok": true,
  "data": {
    "invoices": [
      {
        "invoiceId": "demo-invoices-1",
        "title": "演示服务账单",
        "amountCents": 12000,
        "settled": true,
        "currency": "CNY",
        "lines": {
          "items": [
            {
              "name": "演示服务",
              "cents": 12000
            }
          ],
          "taxIncluded": true
        }
      },
      {
        "invoiceId": "demo-invoices-2",
        "title": "演示配额账单",
        "amountCents": 3500,
        "settled": false,
        "currency": "USD",
        "lines": {
          "items": [
            {
              "name": "演示配额",
              "cents": 3500
            }
          ],
          "taxIncluded": false
        }
      }
    ],
    "summary": {
      "total": 2,
      "note": null
    }
  }
}

对照样本的实际结果

[
  {
    "op": "replace",
    "path": "/data/invoices/0/invoiceId",
    "oldValue": "demo-inserted",
    "value": "demo-invoices-1"
  },
  {
    "op": "replace",
    "path": "/data/invoices/0/title",
    "oldValue": "新增演示记录",
    "value": "演示服务账单"
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/invoiceId",
    "oldValue": "demo-invoices-1",
    "value": "demo-invoices-2"
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/title",
    "oldValue": "演示服务账单",
    "value": "演示配额账单"
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/amountCents",
    "oldValue": 12000,
    "value": 3500
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/settled",
    "oldValue": true,
    "value": false
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/currency",
    "oldValue": "CNY",
    "value": "USD"
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/lines/items/0/name",
    "oldValue": "演示服务",
    "value": "演示配额"
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/lines/items/0/cents",
    "oldValue": 12000,
    "value": 3500
  },
  {
    "op": "replace",
    "path": "/data/invoices/1/lines/taxIncluded",
    "oldValue": true,
    "value": false
  },
  {
    "op": "remove",
    "path": "/data/invoices/2",
    "oldValue": {
      "invoiceId": "demo-invoices-2",
      "title": "演示配额账单",
      "amountCents": 3500,
      "settled": false,
      "currency": "USD",
      "lines": {
        "items": [
          {
            "name": "演示配额",
            "cents": 3500
          }
        ],
        "taxIncluded": false
      }
    }
  }
]