我从Loggly服务以点表示法接收数据,但要放回数据,它必须为JSON。
因此,我需要转换:
{'json.message.status.time':50, 'json.message.code.response':80, 'json.time':100}
Run Code Online (Sandbox Code Playgroud)
进入:
{'message': {'code': {'response': 80}, 'status': {'time': 50}}, 'time': 100}
Run Code Online (Sandbox Code Playgroud)
我已经组合了一个函数来执行此操作,但是我想知道是否有更直接,更简单的方法来实现相同的结果。
def dot_to_json(a):
# Create root for JSON tree structure
resp = {}
for k,v in a.items():
# eliminate json. (if metric comes from another type, it will keep its root)
k = re.sub(r'\bjson.\b','',k)
if '.' in k:
# Field has a dot
r = resp
s = ''
k2 = k.split('.')
l = len(k2)
count = 0
t = {}
for f in k2: …Run Code Online (Sandbox Code Playgroud)