|
| 1 | +"""@package docstring |
| 2 | +JSONPath implementation ported from the jsonpath MATLAB function in JSONLab |
| 3 | +
|
| 4 | +Copyright (c) 2019-2024 Qianqian Fang <q.fang at neu.edu> |
| 5 | +""" |
| 6 | + |
| 7 | +__all__ = [ |
| 8 | + "jsonpath", |
| 9 | +] |
| 10 | + |
| 11 | +##==================================================================================== |
| 12 | +## dependent libraries |
| 13 | +##==================================================================================== |
| 14 | + |
| 15 | + |
| 16 | +import re |
| 17 | +import json |
| 18 | +import copy |
| 19 | + |
| 20 | + |
| 21 | +def jsonpath(root, jpath, opt={}): |
| 22 | + |
| 23 | + obj = root |
| 24 | + jpath = re.sub(r"([^.\]])(\[[-0-9:\*]+\])", r"\1.\2", jpath) |
| 25 | + jpath = re.sub(r"\[[\'\"]*([^]\'\"]+)[\'\"]*\]", r".[\1]", jpath) |
| 26 | + jpath = re.sub(r"\\.", "_0x2E_", jpath) |
| 27 | + while re.search(r"(\[[\'\"]*[^]\'\"]+)\.(?=[^]\'\"]+[\'\"]*\])", jpath): |
| 28 | + jpath = re.sub( |
| 29 | + r"(\[[\'\"]*[^]\'\"]+)\.(?=[^]\'\"]+[\'\"]*\])", r"\1_0x2E_", jpath |
| 30 | + ) |
| 31 | + |
| 32 | + paths = re.findall(r"(\.{0,2}[^.]+)", jpath) |
| 33 | + paths = [re.sub("_0x2E_", ".", x) for x in paths] |
| 34 | + if paths and paths[0] == "$": |
| 35 | + paths.pop(0) |
| 36 | + |
| 37 | + for i, path in enumerate(paths): |
| 38 | + obj, isfound = getonelevel(obj, paths, i, opt) |
| 39 | + if not isfound: |
| 40 | + return None |
| 41 | + return obj |
| 42 | + |
| 43 | + |
| 44 | +def getonelevel(input_data, paths, pathid, opt): |
| 45 | + |
| 46 | + opt.setdefault("inplace", False) |
| 47 | + |
| 48 | + pathname = paths[pathid] |
| 49 | + if isinstance(pathname, list): |
| 50 | + pathname = pathname[0] |
| 51 | + deepscan = bool(re.search(r"^\.\.", pathname)) |
| 52 | + origpath = pathname |
| 53 | + pathname = re.sub(r"^\.+", "", pathname) |
| 54 | + obj = None |
| 55 | + isfound = False |
| 56 | + |
| 57 | + if pathname == "$": |
| 58 | + obj = input_data |
| 59 | + elif re.match(r"\$\d+", pathname): |
| 60 | + obj = input_data[int(pathname[2:]) + 1] |
| 61 | + elif re.match(r"^\[[\-0-9\*:]+\]$", pathname) or isinstance( |
| 62 | + input_data, (list, tuple, frozenset) |
| 63 | + ): |
| 64 | + arraystr = pathname[1:-1] |
| 65 | + arrayrange = {"start": None, "end": None} |
| 66 | + |
| 67 | + if ":" in arraystr: |
| 68 | + match = re.search(r"(?P<start>-*\d*):(?P<end>-*\d*)", arraystr) |
| 69 | + if match: |
| 70 | + arrayrange["start"] = ( |
| 71 | + int(match.group("start")) if match.group("start") else None |
| 72 | + ) |
| 73 | + arrayrange["end"] = ( |
| 74 | + int(match.group("end")) if match.group("end") else None |
| 75 | + ) |
| 76 | + |
| 77 | + if arrayrange["start"] is not None: |
| 78 | + if arrayrange["start"] < 0: |
| 79 | + arrayrange["start"] = len(input_data) + arrayrange["start"] |
| 80 | + else: |
| 81 | + arrayrange["start"] += 1 |
| 82 | + else: |
| 83 | + arrayrange["start"] = 1 |
| 84 | + |
| 85 | + if arrayrange["end"] is not None: |
| 86 | + if arrayrange["end"] < 0: |
| 87 | + arrayrange["end"] = len(input_data) + arrayrange["end"] |
| 88 | + else: |
| 89 | + arrayrange["end"] += 1 |
| 90 | + else: |
| 91 | + arrayrange["end"] = len(input_data) |
| 92 | + elif re.match(r"^[-0-9:]+$", arraystr): |
| 93 | + firstidx = int(arraystr) |
| 94 | + if firstidx < 0: |
| 95 | + firstidx = len(input_data) + firstidx + 1 |
| 96 | + else: |
| 97 | + firstidx += 1 |
| 98 | + arrayrange["start"] = arrayrange["end"] = firstidx |
| 99 | + elif re.match(r"^\*$", arraystr): |
| 100 | + pass |
| 101 | + |
| 102 | + if ( |
| 103 | + "arrayrange" in locals() |
| 104 | + and arrayrange["start"] is not None |
| 105 | + and arrayrange["end"] is not None |
| 106 | + ): |
| 107 | + obj = input_data[arrayrange["start"] - 1 : arrayrange["end"]] |
| 108 | + else: |
| 109 | + arrayrange = {"start": 1, "end": len(input_data)} |
| 110 | + |
| 111 | + if not obj and isinstance(input_data, list): |
| 112 | + input_data = input_data[arrayrange["start"] - 1 : arrayrange["end"]] |
| 113 | + searchkey = ".." + pathname if deepscan else origpath |
| 114 | + newobj = [] |
| 115 | + for idx, item in enumerate(input_data): |
| 116 | + val, isfound = getonelevel( |
| 117 | + item, paths[:pathid] + [searchkey], pathid, opt |
| 118 | + ) |
| 119 | + if isfound: |
| 120 | + newobj.extend(val) |
| 121 | + if newobj: |
| 122 | + obj = newobj |
| 123 | + if isinstance(obj, list) and len(obj) == 1: |
| 124 | + obj = obj[0] |
| 125 | + |
| 126 | + elif isinstance(input_data, dict): |
| 127 | + pathname = re.sub(r"^\[(.*)\]$", r"\1", pathname) |
| 128 | + stpath = pathname |
| 129 | + |
| 130 | + if stpath in input_data: |
| 131 | + obj = [input_data[stpath]] |
| 132 | + |
| 133 | + deepscan = False |
| 134 | + if obj is None or deepscan: |
| 135 | + items = input_data.keys() |
| 136 | + |
| 137 | + for idx in items: |
| 138 | + val, isfound = getonelevel( |
| 139 | + input_data[idx], paths[:pathid] + [[".." + pathname]], pathid, opt |
| 140 | + ) |
| 141 | + if isfound: |
| 142 | + obj = obj or [] |
| 143 | + if isinstance(val, list): |
| 144 | + obj.extend(val) |
| 145 | + else: |
| 146 | + obj.append(val) |
| 147 | + |
| 148 | + if obj and len(obj) == 1: |
| 149 | + obj = obj[0] |
| 150 | + |
| 151 | + if isinstance(obj, list) and len(obj) == 1: |
| 152 | + obj = obj[0] |
| 153 | + |
| 154 | + elif not deepscan: |
| 155 | + raise ValueError( |
| 156 | + f'json path segment "{pathname}" can not be found in the input_data object' |
| 157 | + ) |
| 158 | + |
| 159 | + if obj is None: |
| 160 | + isfound = False |
| 161 | + obj = [] |
| 162 | + else: |
| 163 | + isfound = True |
| 164 | + |
| 165 | + return (copy.deepcopy(obj), isfound) if opt["inplace"] else (obj, isfound) |
0 commit comments