def test_recur_dict(): test_1 = {"one": 1} assert util.recur_dict(set(), test_1) == set(["$['one']"]) test_2 = {"one": 1, "two": {"three": 1}} res_2 = set(["$['one']", "$['two']['three']"]) assert util.recur_dict(set(), test_2) == res_2 test_3 = {"one": 1, "two": {"three": {"four": 4}, "five": [1, 2, 3]}} res_3 = set(["$['one']", "$['two']['three']['four']", "$['two']['five'][0]"]) assert util.recur_dict(set(), test_3, list_idx=0) == res_3 test_4 = {"one": [1, 2]} assert util.recur_dict(set(), test_4, list_idx=1) == set(["$['one'][1]"])
def gen_jsonpaths(json_doc, list_idx=None): """ Generate Redshift jsonpath file for given JSON document or dict. If an array is present, you can specify an index to use for that field in the jsonpaths result. Right now only a single index is supported. Results will be ordered alphabetically by default. Parameters ---------- json_doc : str or dict Dictionary or JSON-able string list_idx : int Index for array position Returns ------- Dict """ if isinstance(json_doc, str): parsed = json.loads(json_doc) else: parsed = json_doc paths_set = util.recur_dict(set(), parsed, list_idx=list_idx) paths_list = list(paths_set) paths_list.sort() return {"jsonpaths": paths_list}