1

I try to check if object A is part of object B. As can be seen, all key-value pairs from object A are also contained in object B. Object A comes from a YAML file (yaml.safe_load()), object B is the result of an API query (requests.get().json()). Both are of type (with type()): <class 'dict'>

Example objects:

Object A
{
    "title": "Filebeat",
    "type": "org.graylog.plugins.beats.Beats2Input",
    "global": true,
    "configuration": {
        "bind_address": "0.0.0.0",
        "port": 5044,
        "tls_enable": false,
        "no_beats_prefix": false
    }
}

Object B
{
    "title": "Filebeat",
    "global": true,
    "name": "Beats",
    "created_at": "2020-07-27T12:20:57.041Z",
    "type": "org.graylog.plugins.beats.Beats2Input",
    "creator_user_id": "admin",
    "attributes": {
        "bind_address": "0.0.0.0",
        "port": 5044,
        "tls_enable": false,
        "no_beats_prefix": false
    },
    "static_fields": {},
    "node": "None",
}

I am thinking of:

if array_a.ispart(array_b):
    ...

I've already checked this post (Python: Check if one dictionary is a subset of another larger dictionary), but it doesn't work for me. Maybe because of the nested arrays.

rosaLux161
  • 33
  • 8
  • 1
    `"configuration"` and `"attributes"` are unique keys in `A` and `B`, respectively. Is this a typo? – jfaccioni Jul 27 '20 at 13:53
  • So for every key in A, you want to see if the same key in B exists and has the same value, following nested structures, but don't care if B has additional keys that A doesn't? I assume mapping "configuration" to "attributes" along the way? – kindall Jul 27 '20 at 13:56
  • To rephrase @jfaccioni, your problem has nothing to do with nesting or lists/arrays but rather the key name mismatch between them. *You, the programmer* can glance at that data structure and see that configuration and attributes are the same "JSON object" but the computer can't do that unless you explain to it *how*. – Jared Smith Jul 27 '20 at 14:09

2 Answers2

1

How about create a new dict with only the keys/values you care about, and then test the 2 dicts for equality?

Didn't test but you get the idea (you would need to implement a recursive solution if you want to compare dicts within dicts to see if they are subsets, but hard to tell if that is a requirement based on your question).

def is_subset(a, b):
    subset = {}
    for k, v in a.items():
        if k in b:
            subset[k] = b[k]
    return subset == a
user2263572
  • 5,435
  • 5
  • 35
  • 57
0

you can use recursive function to check nested object.

def is_subset(a,b):
    def recursive_check_subset(a,b):
        for x in a:
            if not isinstance(a[x],dict):
                yield (x in b ) and (a[x] == b[x]) #return a bool
            else:
                if x in b:
                    yield all(recursive_check_subset(a[x],b[x]))
                else:
                    yield False
    return all(recursive_check_subset(a,b))

this return True if all keys and values be equal.

Ali Fallah
  • 138
  • 8