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

消息通知:解释 notifications 首部插入导致的多处差异

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

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

示例工作台

在完整工具中打开 ↗
JSON 结构对比修改下方数据后重新运行
运行结果
[
  {
    "op": "replace",
    "path": "/data/notifications/0/messageId",
    "oldValue": "demo-notifications-1",
    "value": "demo-inserted"
  },
  {
    "op": "replace",
    "path": "/data/notifications/0/subject",
    "oldValue": "演示站内消息",
    "value": "新增演示记录"
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/messageId",
    "oldValue": "demo-notifications-2",
    "value": "demo-notifications-1"
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/subject",
    "oldValue": "演示推送消息",
    "value": "演示站内消息"
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/retryCount",
    "oldValue": 2,
    "value": 0
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/read",
    "oldValue": false,
    "value": true
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/channel",
    "oldValue": "push",
    "value": "inbox"
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/audience/groups/0",
    "oldValue": "demo-test",
    "value": "demo-team"
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/audience/locale",
    "oldValue": "en",
    "value": "zh-CN"
  },
  {
    "op": "add",
    "path": "/data/notifications/2",
    "value": {
      "messageId": "demo-notifications-2",
      "subject": "演示推送消息",
      "retryCount": 2,
      "read": false,
      "channel": "push",
      "audience": {
        "groups": [
          "demo-test"
        ],
        "locale": "en"
      }
    }
  }
]

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

⌘ / Ctrl + Enter 运行
01

场景与操作说明

通知渠道、已读标记和重试次数表达不同状态。下面的站内消息和推送消息各有受众分组与语言设置,可练习提取消息标识、校验布尔状态,以及核对重试计数的类型。

retryCount 是示例重试次数,已读状态不由该次数推出。

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

对照样本

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

查看对照输入
{
  "ok": true,
  "data": {
    "notifications": [
      {
        "messageId": "demo-inserted",
        "subject": "新增演示记录",
        "retryCount": 0,
        "read": true,
        "channel": "inbox",
        "audience": {
          "groups": [
            "demo-team"
          ],
          "locale": "zh-CN"
        }
      },
      {
        "messageId": "demo-notifications-1",
        "subject": "演示站内消息",
        "retryCount": 0,
        "read": true,
        "channel": "inbox",
        "audience": {
          "groups": [
            "demo-team"
          ],
          "locale": "zh-CN"
        }
      },
      {
        "messageId": "demo-notifications-2",
        "subject": "演示推送消息",
        "retryCount": 2,
        "read": false,
        "channel": "push",
        "audience": {
          "groups": [
            "demo-test"
          ],
          "locale": "en"
        }
      }
    ],
    "summary": {
      "total": 2,
      "note": null
    }
  }
}
{
  "ok": true,
  "data": {
    "notifications": [
      {
        "messageId": "demo-notifications-1",
        "subject": "演示站内消息",
        "retryCount": 0,
        "read": true,
        "channel": "inbox",
        "audience": {
          "groups": [
            "demo-team"
          ],
          "locale": "zh-CN"
        }
      },
      {
        "messageId": "demo-notifications-2",
        "subject": "演示推送消息",
        "retryCount": 2,
        "read": false,
        "channel": "push",
        "audience": {
          "groups": [
            "demo-test"
          ],
          "locale": "en"
        }
      }
    ],
    "summary": {
      "total": 2,
      "note": null
    }
  }
}

对照样本的实际结果

[
  {
    "op": "replace",
    "path": "/data/notifications/0/messageId",
    "oldValue": "demo-inserted",
    "value": "demo-notifications-1"
  },
  {
    "op": "replace",
    "path": "/data/notifications/0/subject",
    "oldValue": "新增演示记录",
    "value": "演示站内消息"
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/messageId",
    "oldValue": "demo-notifications-1",
    "value": "demo-notifications-2"
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/subject",
    "oldValue": "演示站内消息",
    "value": "演示推送消息"
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/retryCount",
    "oldValue": 0,
    "value": 2
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/read",
    "oldValue": true,
    "value": false
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/channel",
    "oldValue": "inbox",
    "value": "push"
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/audience/groups/0",
    "oldValue": "demo-team",
    "value": "demo-test"
  },
  {
    "op": "replace",
    "path": "/data/notifications/1/audience/locale",
    "oldValue": "zh-CN",
    "value": "en"
  },
  {
    "op": "remove",
    "path": "/data/notifications/2",
    "oldValue": {
      "messageId": "demo-notifications-2",
      "subject": "演示推送消息",
      "retryCount": 2,
      "read": false,
      "channel": "push",
      "audience": {
        "groups": [
          "demo-test"
        ],
        "locale": "en"
      }
    }
  }
]