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

TCL新手求助:如何自动提取更新后嵌套JSON的所有键值对

Generic Solution to Traverse Nested JSON (Dict) and Save All Key-Value Pairs to TXT in TCL

Hey there! I get where you're coming from—needing a flexible solution that doesn't break when new keys get added to your JSON later is super important. Instead of relying on hardcoding specific keys (like with the tool you mentioned), we can build a recursive traversal function that digs through every level of your parsed dict, capturing all keys and their values automatically.

Here's how to do it step by step:

1. Build a Recursive Traversal Function

This function will walk through every key in your nested dict. If it hits another dict (nested JSON object), it will dive deeper. If it finds a regular value (or even a list/JSON array), it will record the full path of the key along with its value.

proc traverseNestedDict {dictObj currentKeyPath outputListVar} {
    upvar $outputListVar output
    foreach key [dict keys $dictObj] {
        # Build the full path for nested keys (e.g., "user.profile.email")
        set fullKeyPath [expr {$currentKeyPath eq "" ? $key : "$currentKeyPath.$key"}]
        set value [dict get $dictObj $key]

        # Check if the value is another nested dict
        if {[dict exists $value]} {
            traverseNestedDict $value $fullKeyPath output
        } 
        # Handle lists (JSON arrays) if your data has them
        elseif {[llength $value] > 1 && ![string is scalar -strict $value]} {
            set index 0
            foreach listItem $value {
                if {[dict exists $listItem]} {
                    traverseNestedDict $listItem "$fullKeyPath\[$index\]" output
                } else {
                    lappend output "$fullKeyPath\[$index\] = $listItem"
                }
                incr index
            }
        }
        # Regular scalar value—add to output
        else {
            lappend output "$fullKeyPath = $value"
        }
    }
}

2. Use the Function to Process Your Dict and Save to TXT

Once you've parsed your JSON into a TCL dict (let's call it parsedJsonDict), you can use the function to collect all key-value pairs, then write them to a text file.

# Assume you've already converted your JSON to a dict named 'parsedJsonDict'
set allKeyValuePairs [list]
traverseNestedDict $parsedJsonDict "" allKeyValuePairs

# Write the results to a TXT file
set outputFile [open "key_value_output.txt" w]
puts $outputFile [join $allKeyValuePairs "\n"]
close $outputFile

Why this works for future updates:

  • No hardcoded keys: The function automatically discovers every key in the dict, no matter how deeply nested or how many new ones get added later.
  • Handles nested objects and arrays: It will correctly map paths like parent.child[0].newKey so you can tell exactly where each value comes from in the original JSON.
  • Clean, readable output: The TXT file will have each key-value pair on its own line, with full path context for nested entries.

Example Output:

If your JSON has nested data like:

{
  "name": "TCL User",
  "settings": {
    "theme": "dark",
    "notifications": ["email", "push"]
  }
}

Your TXT file will look like this:

name = TCL User
settings.theme = dark
settings.notifications[0] = email
settings.notifications[1] = push

This setup will adapt seamlessly to any new keys added to your JSON down the line—no need to modify your code!

内容的提问来源于stack exchange,提问作者Ab19.newbie

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:04:22