def _json_get(inp):
""" Get a Python object (list or dict) regardless of whether data is passed
as a JSON string, a file path, or is already a python object.
Returns the parsed data, as well as "native" if the data was already a
Python object, "str" if the data was passed as a JSON string, or the path
if the data passed was a file path """
if not (isinstance(inp, dict) or isinstance(inp, list)): # Python object
try: # JSON string
data = json.loads(inp)
dataformat = "str"
except json.JSONDecodeError: # JSON filepath
# Store the filename in the dataformat variable if dataformat is a
# file, because it's just one fewer variable to keep track of
dataformat = inp
with open(inp, encoding="utf-8") as f:
data = json.load(f)
else:
dataformat = "native"
return data, dataformat
评论列表
文章目录