seperman / deepdiff

DeepDiff: Deep Difference and search of any Python object/data. DeepHash: Hash of any object based on its contents. Delta: Use deltas to reconstruct objects by adding deltas together.

Home Page:http://zepworks.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Documentation on how to consume keys

brettforbes opened this issue · comments

Hi,

Your package looks amazing, and so i am sorry if my question is dumb, but it would be really useful to understand how to consume keys, and perhaps you could enhance your docs with a demo?

My scenario is that:

  1. i have Stix 2.1 json objects converted and stored in a knowledge graph
  2. I query these objects into my user interface, and convert them back to json (lists of dicts) on the way in.
  3. I operate on these lists of dicts in my user interface to add, update and delete objects and properties
  4. I want to use DeepDiff to evaluate the difference between what is in my database, and the new changes, so i can issue add, update and delete commands to the knowledge graph.

Question: How can I consume the keys easily so i can use them to construct update statements?
I understand how to use DeepDiff to generate the type of difference (e.g. item added to dictionary), and to obtain the key and value to this change. But what is the easiest way to consume the key (string), and convert it to a dict pathway.

As an example, consider an object like the following

{
      "type": "incident",
      "spec_version": "2.1",
      "id": "incident--415c986d-f2b3-4c88-abb4-58531b3c3a93",
      "created": "2023-12-31T08:03:04.46545Z",
      "modified": "2023-12-31T08:03:04.46545Z",
      "name": "potential phishing",
      "extensions": {
        "extension-definition--ef765651-680c-498d-9894-99799f2fa126": {
          "extension_type": "property-extension",
          "investigation_status": "new",
          "incident_types": [
            "dissemination-phishing-emails"
          ],
          "sequence_start_refs": [
            "sequence--fc108d99-8262-4082-81dd-02411203d291",
            "sequence--ac6f3beb-e28a-49ef-b0e4-2ddcd54fa2a2"
          ],
          "sequence_refs": [
            "sequence--b75a90bb-5a55-48c3-bdae-23c9e693a362",
            "sequence--b75a90bb-5a55-48c3-bdae-23c9e693a362"
          ],
          "task_refs": [
            "task--8a676b0b-a835-4e13-8912-b912c3d9be72"
          ],
          "event_refs": [
            "event--59026c8c-b8f7-4cbf-9740-a3b2b22bad7f"
          ],
          "other_object_refs": [
            "email-addr--eb38d07e-6ba8-56c1-b107-d4db4aacf212",
            "user-account--597ad4d4-35ba-585d-8f6d-134a75032f9b",
            "email-addr--4722424c-7012-56b0-84d5-01d076fc547b",
            "identity--e5a6e5c3-7138-4288-8146-87d972eb2098",
            "identity--2e02a834-c89b-483b-869d-fcb713721246",
            "url--3279c7de-8f91-5c1a-99d9-d6546c6c41f7",
            "email-message--6090e3d4-1fa8-5b36-9d2d-4a66d824995d",
            "relationship--2a4ac790-b905-43c7-aada-87ddafd849fd",
            "observed-data--edfce46d-423e-45cc-9c9b-f6b39af482b9",
            "indicator--c39ce0f6-4d12-4151-9256-596166aa3a5f",
            "sighting--3f365a55-bd23-4e09-9a69-8a5a46caf328"
          ]
        }
      }
    }

Now lets say for the

  • category = "dictionary_item_added"
  • key = "root['extensions']['extension-definition--ef765651-680c-498d-9894-99799f2fa126']['impact_refs']"
  • value = ['impact--dfb745f7-a931-4f3a-a6ef-6e9f8ae4a018']

or

  • category = "dictionary_item_removed"
  • key = "root['extensions']['extension-definition--ef765651-680c-498d-9894-99799f2fa126']['investigation_status']"
  • value = new

What is the best way to consume the key to find the dict pathway, am i to parse it, looking for single quote marks and array brackets?? Is the only way to consume this string to manually parse it looking for characters, or is there an easy way to consume these keys???

I love the package, I am just not sure how to best consume the keys, other than searching the string for square brackets and single quotes??? Can you explain a better approach please?

Sorry if the question is a bit dumb, thanks

aaah, fter much searching, I cam across your response here

And that then led me to your FAQ, where you briefly document the parse paths method. Brilliant!!!

Who knew??? This should be mentioned up high in your deepdiff docs. Consider modifying the docs to make it screamingly obvious how to parse paths

@brettforbes Thanks for the recommendation. Happy to hear you like DeepDiff!