Coder Social home page Coder Social logo

rugleb / jsoncompare Goto Github PK

View Code? Open in Web Editor NEW
67.0 67.0 23.0 181 KB

The Python JSON Comparison package

Home Page: https://pypi.org/project/jsoncomparison

License: MIT License

Python 95.78% Makefile 4.22%
comparison diff json json-comparison python python3

jsoncompare's People

Contributors

jacksgt avatar kahilav2 avatar miyamotoakira avatar o-water-o avatar obsidian33 avatar rugleb avatar sigmakappa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jsoncompare's Issues

Can we also compare the order of keys?

Hi there!

Say as an example below:

expected = [
{'a': "1"},
{'b': "2"},
{'c': "3"},
{'d': "4"},
]
actual = [
{'a': "1"},
{'d': "4"},
{'b': "2"},
{'c': "3"},
]

Can JsonCompare be used to compare the order of keys? Expected has the order as abcd whereas actual has adbc.

Thanks in advance.

ignore have a bug if _values is dict

    # ignore.py 62
   @classmethod
    def _ignore_values(cls, obj, black_list):
        t = type(obj)
        if t is list:
            return [x for x in obj if x not in black_list]
        if t is obj: ## should be if t is dict
            return {k: obj[k] for k in obj if k not in black_list}
        return obj

Unexpected difference

Hi,
thank you for the great package. I use it to compare HTML DOM trees, however, in some situations the package outputs wrong diffs. Attached you can find two JSON files that are smaller parts of a bigger DOM tree. At the same position, the first JSON contains the value THIS IS THE CORRECT VALUE, the second one THIS IS THE WRONG VALUE.

r1["childNodes"][0]["childNodes"][0]["childNodes"][0]["childNodes"][0]["childNodes"][0]["childNodes"][0]["nodeValue"]
THIS IS THE CORRECT VALUE

Both JSONs also contain the value THIS IS NOT DIFFERENT at the same position:

r1["childNodes"][1]["childNodes"][0]["childNodes"][0]["childNodes"][1]["childNodes"][0]["childNodes"][0]["nodeValue"]
THIS IS NOT DIFFERENT

When comparing both objects, I expect the outcome to tell me that the wrong and correct value are different. However, the result is the following, stating that the correct value is different from the "not different" value.

{'childNodes': {'_content': {0: {'childNodes': {'_content': {0: {'childNodes': {'_length': {'_message': 'Lengths not equal. Expected <1>, received: <2>',
         '_expected': 1,
         '_received': 2},
        '_content': {0: {'childNodes': {'_length': {'_message': 'Lengths not equal. Expected <1>, received: <3>',
            '_expected': 1,
            '_received': 3},
           '_content': {0: {'childNodes': {'_length': {'_message': 'Lengths not equal. Expected <1>, received: <2>',
               '_expected': 1,
               '_received': 2},
              '_content': {0: {'attributes': {'href': {'_message': 'Values not equal. Expected: </whats-new/latest-activity>, received: </search/?type=profile_post>',
                  '_expected': '/whats-new/latest-activity',
                  '_received': '/search/?type=profile_post'},
                 'rel': {'_message': 'Key does not exists. Expected: <rel>',
                  '_expected': 'rel',
                  '_received': None},
                 'data-nav-id': {'_message': 'Values not equal. Expected: <latestActivity>, received: <searchProfilePosts>',
                  '_expected': 'latestActivity',
                  '_received': 'searchProfilePosts'}},
                'childNodes': {'_content': {0: {'nodeValue': {'_message': 'Values not equal. Expected: <THIS IS THE CORRECT VALUE>, received: <THIS IS NOT DIFFERENT>',
                    '_expected': 'THIS IS THE CORRECT VALUE',
                    '_received': 'THIS IS NOT DIFFERENT'}}}}}}}}}}}}}}}}}}}}

I had not the time yet to look deeper into the issue, but it seems that some indexing in the package code is wrong. Somehow, the code compares r1["childNodes"][0] with r2["childNodes"][1]. If I remove ["childNodes"][1] from both JSONs, the outcome is correct. Furthermore, if I manually compare r1["childNodes"][0] and r2["childNodes"][1], the outcome is the same as the one printed above.

r10 = r1["childNodes"][0]
r21 = r2["childNodes"][1]
Compare().check(r10, r21)

I hope that my examples help to find the issue.
Thank you for your help!

[Feature Request] Value comparison based on regex

Let's say we have dictionary actual and expected

expected = {
"namespace": "NamespaceA",
"kind": "Pod"
}
actual = {
"namespace": "NamespaceA-832y2"
"kind": "pod"
}

In such scenario, I couldn't find utility to do the necessary comparison using regex. I found a rule "*" this matches any.

提示缺少 data/config.json

pip 安装成功
调用时,提示我缺少 data/config.json,源码追踪后发现,源码里将data/config.json加入到了忽略列表,是否考虑移除忽略,或者在说明文档里说明下,安装后需要自己配置一下

exclude entries within a list of objects of a more complex JSON structure

Hi Karpushkin.

I worked with your great library for comparing JSON files. Your Readme and your samples are very helpful.
So I could compare simple JSON files easily.

But I'm stuck in a rule for nodes within a list of objects. Would you tell me please, how I have to use the rules in the right manner?

My structure is like this:
{
"A" : {
"B" : [ {
"C" : {
"D" : "ddd",
"E" : [ {
"F" : {
"G" : "ggg",
"H" : "hhh"
}
} ],
"I" : "11",
"J" : null
}
} ]
}
}

What rules I have to use to ignore "G" and "J"?

Thank you in advance, Niko.

Min diff is not finding the true minimum

    @classmethod
    def _min_diff(cls, e, lst, method):
        t = type(e)
        d = method(e, t())
        for i, v in enumerate(lst):
            if type(v) is t:
                dd = method(e, v)
                if len(dd) <= len(d):
                    d = dd
                    break
        return d

The break statement is causing the compare to exit before it has found the minimum

Wrong expected/received values.

Hello,
Attached you can find two json eaxmples, which are only a small part of jsons I'm trying to compare.
There is a change found in
{'signals': {'LockUnlock': {'rx_nodes': {'_content': {0: which is correct, but received value is incorectly assigned to None instead of Vector__XXX.

When comparing in the other direction, expected value is incorectly assigned to None instead of HCP.

Is there any workaround for this? Im writing interactive tool where user can decide if he wants to accept or reject changes, and generate new json automatically out of that, so I would appreciate any solution.

rules are not being used to ignore fields

shouldn't this work and result NO_DIFF?

'occupationId': "*", in the rules

from jsoncomparison import Compare, NO_DIFF
expected = [{'bookings': {
    'accommodation': [{'rooms': [{
        'type': 'LUXO ',
        'occupationId': 62329,
        }]}],
    }}]

rules = [{'bookings': {
    'accommodation': [{'rooms': [{
        'type': 'LUXO ',
        'occupationId': "*",
        }]}],
    }}]

actual = [{'bookings': {
    'accommodation': [{'rooms': [{
        'type': 'LUXO ',
        'occupationId': 111,
        }]}],
    }}]

Compare(rules=rules).check(expected, actual)

i am receiving this:
{'_content': {0: {'bookings': {'accommodation': {'_content': {0: {'rooms': {'_content': {0: {'occupationId': {'_message': 'Values not equal. Expected: <62329>, received: <111>', '_expected': 62329, '_received': 111}}}}}}}}}}}

A 1.2.0 release?

Any chance that we could get a new release. That would allow me to start using the new regex functionality at work.

Question about Ignore Rules

Hi,

I really appreciate you creating jsoncompare, and I think it's going to be a great tool for testing my api. However, I am struggling using the ignore rules for a objects nested in a list. I've attached the expected.json, actual.json, ignore rules, and diff.json. If you can give many any help, I would truly appreciate it.

The gist of the problem is that, for each plotRow in my example, there well be unique kmzUrl and geoJsonUrl; so I don't want to compare those values. Though, it would be a bonus if I could still verify if there is some value or a null.

Thanks,
Joel

actual.json
diff.json
expected.json
ignore_rules.txt

Reverse Comparison not Working

if difference exist in -
Compare().check(first, second)
Then it should also work for -
Compare().check(second, first)

Does not seem to detect difference when a key is in 'actual' JSON that is not in the 'expected' JSON

It seems like the module is not checking for keys that exist in the "actual" structure that are not in the "expected" structure.

Example code:

expected = {'a': 1, 'b': 'tom'}
actual = {'a': 1, 'b': 'tom', 'c': 'surprise'}
diff = Compare().check(expected, actual)

This reports no differences, even though they are clearly not identical. I would like this condition to be included in the diff report, perhaps as an UnexpectedKey exception with a message of Unexpected key. Received: <{r}>

If this is already supported, please let me know how to enable it.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.