1

I'm trying to use this json patch implementation to create patch https://github.com/stefankoegl/python-json-patch JsonPatch RFC

But I found the result in some cases not satisfactory. For example json 1

[
    {
      "asset": "USDT",
      "marginAvailable": true,
      "autoAssetExchange": "-10000"
    },
    {
      "asset": "BTC",
      "marginAvailable": true,
      "autoAssetExchange": "-0.00100000"
    },
    {
      "asset": "BNB",
      "marginAvailable": true,
      "autoAssetExchange": "-10"
    },
    {
      "asset": "ETH",
      "marginAvailable": true,
      "autoAssetExchange": "-5"
    }
]

json 2

[
    {
      "asset": "BTC",
      "marginAvailable": true,
      "autoAssetExchange": "-0.00100000"
    },
    {
      "asset": "BNB",
      "marginAvailable": true,
      "autoAssetExchange": "-10"
    },
    {
      "asset": "ETH",
      "marginAvailable": true,
      "autoAssetExchange": "-5"
    }
]

the make_diff result is

[
  {
    "op": "replace",
    "path": "/0/asset",
    "value": "BTC"
  },
  {
    "op": "replace",
    "path": "/0/autoAssetExchange",
    "value": "-0.00100000"
  },
  {
    "op": "replace",
    "path": "/1/asset",
    "value": "BNB"
  },
  {
    "op": "replace",
    "path": "/1/autoAssetExchange",
    "value": "-10"
  },
  {
    "op": "replace",
    "path": "/2/asset",
    "value": "ETH"
  },
  {
    "op": "replace",
    "path": "/2/autoAssetExchange",
    "value": "-5"
  },
  {
    "op": "remove",
    "path": "/3"
  }
]

But the shortest patch should be

{"op": "remove", "path": "/0"}

I'm wondering whether there is an algorithm that can give optimal json diff result, or is there other implementation that gives better results

Xiaoyong Guo
  • 361
  • 1
  • 7
  • There is. Brute force: Try all possible combinations and pick the best one. It can take a while though. – Ouroborus Feb 28 '23 at 05:03
  • Then how to list all possible transformations from json A to json B so that required number of ops is less than a given number. – Xiaoyong Guo Feb 28 '23 at 05:08
  • That's too broad of a question. When you've done further research, have code to show, and have a specific question about some specific issue in that code, come back and ask a new question. Meanwhile, read [how to ask](https://stackoverflow.com/help/how-to-ask). – Ouroborus Feb 28 '23 at 07:00

0 Answers0