You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何遍历嵌套ruletree JSON提取指定键值并写入日志文件?

嵌套JSON提取字段并生成带UTC时间戳的日志

需求说明

从API返回的嵌套JSON(ruletree)中提取domainName、rule和host字段,为每条记录添加UTC时间戳后追加写入/opt/logs/domains日志文件,每条日志为独立的JSON对象。

现有代码片段

ruletree = result.json()

#code to get domainName, name & host
newjson =

#add date for every log line
now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
new_d = {"date_time": now, **newjson}

#save to log file
with open('/opt/logs/domains', 'a') as outfile:
  json.dump(new_d, outfile)
  print(file=outfile)

示例输入JSON

ruletree = {
    "contract": "1234",
    "domainName": "www.domainA.com",
    "rules": {
        "name": "default",
        "children": [
            {
                "rule": "Rule1",
                "children": [],
                "behaviors": [
                    {
                        "name": "origin",
                        "options": {
                            "originType": "CUSTOMER",
                            "host": "gateway1.com",
                        },
                    }
                ],
            },
            {
                "rule": "Rule2",
                "children": [],
                "behaviors": [
                    {
                        "name": "origin",
                        "options": {
                            "originType": "CUSTOMER",
                            "host": "gateway2.com",
                        },
                    }
                ],
            },
        ],
    },
}

预期输出日志

{"date_time": "2023-07-06 06:07:06", "domainName": "www.domainA.com", "rule": "Rule1", "host": "gateway1.com"}
{"date_time": "2023-07-06 06:07:06", "domainName": "www.domainA.com", "rule": "Rule2", "host": "gateway2.com"}
{"date_time": "2023-07-06 06:07:06", "domainName": "www.domainB.com", "rule": "Rule1", "host": "gateway1.com"}
{"date_time": "2023-07-06 06:07:06", "domainName": "www.domainB.com", "rule": "Rule2", "host": "gateway2.com"}

完整解决方案代码

import json
import datetime

# 实际场景中替换为API响应解析
# ruletree = result.json()
ruletree = {
    "contract": "1234",
    "domainName": "www.domainA.com",
    "rules": {
        "name": "default",
        "children": [
            {
                "rule": "Rule1",
                "children": [],
                "behaviors": [
                    {
                        "name": "origin",
                        "options": {
                            "originType": "CUSTOMER",
                            "host": "gateway1.com",
                        },
                    }
                ],
            },
            {
                "rule": "Rule2",
                "children": [],
                "behaviors": [
                    {
                        "name": "origin",
                        "options": {
                            "originType": "CUSTOMER",
                            "host": "gateway2.com",
                        },
                    }
                ],
            },
        ],
    },
}

# 提取顶层domainName
domain_name = ruletree.get("domainName", "")
# 获取所有规则节点
rule_list = ruletree.get("rules", {}).get("children", [])
# 生成统一UTC时间戳
utc_now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S")

# 追加写入日志文件
with open('/opt/logs/domains', 'a') as outfile:
    for rule_node in rule_list:
        current_rule = rule_node.get("rule", "")
        # 遍历行为列表,找到origin类型的行为
        for behavior in rule_node.get("behaviors", []):
            if behavior.get("name") == "origin":
                current_host = behavior.get("options", {}).get("host", "")
                # 构造单条日志对象
                log_item = {
                    "date_time": utc_now,
                    "domainName": domain_name,
                    "rule": current_rule,
                    "host": current_host
                }
                # 写入JSON并换行
                json.dump(log_item, outfile)
                print(file=outfile)

关键说明

  • 遍历逻辑:先提取顶层domainName,再遍历rules.children下的每个规则节点,最后在节点的behaviors中筛选name为origin的行为,提取对应host。
  • 容错处理:使用dict.get()替代直接键取值,避免JSON结构字段缺失时抛出KeyError。
  • 时间戳:用datetime.timezone.utc确保生成标准UTC时间,避免时区差异问题。
  • 日志格式:每条日志单独写入并换行,符合JSON Lines格式,方便后续工具解析。

内容的提问来源于stack exchange,提问作者francorocks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 06:35:34