Skip to main content

A simple json struct matcher

Project description

JsonMatcher

A simple tool to judge if a json matches a specific struct, and give a way to get specific value Github

Usage

Basic match

from json_matcher.matcher import JsonMatcher

def test_basic():
    tpl = {
        "a": 1,
        "b": {
            "bb": 2
        },
        "c": list
    }
    matcher = JsonMatcher(tpl)

    # =========case1=========
    data1 = {
        "a": 1,
        "b": {
            "bb": 2
        },
        "c": [1, 2, 3]
    }
    ok, msg = matcher.is_match(data1)
    print(ok, msg)
    # True, ""

    # =========case2=========
    data2 = {
        "a": 2,
        "b": {
            "bb": 2
        },
        "c": 123
    }
    ok, msg = matcher.is_match(data2)
    print(ok, msg)
    # False, "Value error a, expect 1, but get 2"

    # =========case3=========
    data3 = {
        "a": 1,
        "b": {
            "bb": 2
        },
        "c": 123
    }
    ok, msg = matcher.is_match(data3)
    print(ok, msg)
    # False, "Type error, expect type: <class 'list'>, but get <class 'int'>"

Logic match

from json_matcher import or_
from json_matcher.matcher import JsonMatcher

def test_logic():
    tpl = {
        "a": [
            1, 2
        ],
        "b": {
            "bb": 1
        }
    }
    matcher = JsonMatcher(tpl)

    # =========case1=========
    data1 = {
        "a": [
            1, 2, 3, 4
        ],
        "b": {
            "bb": 1
        },
    }
    ok, msg = matcher.is_match(data1)
    print(ok, msg)
    # True, ""
    # Note:for each rule in list template, if any element in data matches the rule, will return true

    # =========case2=========
    tpl = {
        "a": [
            1, 2
        ],
        "b": {
            "bb": 1
        },
        "c": or_(1, 2)
    }
    matcher = JsonMatcher(tpl)
    data2 = {
        "a": [
            1, 2, 3, 4
        ],
        "b": {
            "bb": 1
        },
        "c": 1
    }
    ok, msg = matcher.is_match(data2)
    print(ok, msg)
    # True, ""

    data3 = {
        "a": [
            1, 2, 3, 4
        ],
        "b": {
            "bb": 1
        },
        "c": 3
    }
    ok, msg = matcher.is_match(data3)
    print(ok, msg)
    # False Value error logic_op_or, expect 1, but get 3
    # Value error logic_op_or, expect 2, but get 3

    # =========case3=========
    tpl = {
        "a": or_(
            {
                "b": 1
            },
            {
                "c": 1
            }
        )
    }
    matcher = JsonMatcher(tpl)
    data4 = {
        "a": {
            "b": 1,
            "bb": 2,
            "bbb": 3,
            "bbbb": {
                "c": 1,
                "cc": 2
            }
        },
        "b": []
    }
    ok, msg = matcher.is_match(data4)
    print(ok, msg)
    # True, ""

Fetch data

This tool apply a way to get specific data from json data. but only support one get_ in single template temporarily

from json_matcher import get_
from json_matcher.matcher import JsonMatcher
def test_fetch():
    tpl = {
        "a": {
            "b": 1,
            "c": get_()
        }
    }

    matcher = JsonMatcher(tpl)
    data = {
        "abc": [1, 2, 3],
        "a": {
            "b": 1,
            "c": {
                "c1": 1,
                "c2": [1, 2, 3]
            },
            "d": "xxx"
        },
        "bac": {1, 2, 3}
    }

    ok, msg = matcher.is_match(data)
    print(ok, msg)
    # True, ""
    fetch_data = matcher.get_data()
    print(fetch_data)
    # {'c': {'c1': 1, 'c2': [1, 2, 3]}}

Find and fetch

check is there any part of a json matches a template, and support fetch data

from json_matcher.matcher import JsonMatcher
from json_matcher import get_
def test_parse():
    tpl = {
        "a": {
            "b": 1,
            "c": get_()
        }
    }

    data = {
        "key1": "val1",
        "key2": {
            "a": {
                "b": 1,
                "c": "data to fetch"
            }
        },
        "key3": []
    }
    matcher = JsonMatcher(tpl)
    matched_data = matcher.find_from_json(data)
    print(matched_data)
    # {'a': {'b': 1, 'c': 'data to fetch'}}
    fetched_data = matcher.get_data()
    print(fetched_data)
    # {'c': 'data to fetch'}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

JsonMatcher-0.0.2.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

JsonMatcher-0.0.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file JsonMatcher-0.0.2.tar.gz.

File metadata

  • Download URL: JsonMatcher-0.0.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.4

File hashes

Hashes for JsonMatcher-0.0.2.tar.gz
Algorithm Hash digest
SHA256 37bf344b2a2c75c0392d9fb514b9bde05fc08464da216be6e2e945ac22494220
MD5 f261ad8c4d8bf7aae4d3e2039e6577f9
BLAKE2b-256 fabdd2e49148cda930d025449276a1f6795d9f425bd3dd6bd8cb3ac69775e24e

See more details on using hashes here.

File details

Details for the file JsonMatcher-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: JsonMatcher-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.4

File hashes

Hashes for JsonMatcher-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d1631a58ac91fe2cf8f18f90898bdbf68c2fbdedb797515afb2372bf1351dad1
MD5 9af7173a6c5079d08714634267740484
BLAKE2b-256 74ed535f65fafe7362977a8a518220ac0466e55225af2a23385cc1751b12c070

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page