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

如何在Paw中遍历含动态键的对象并获取指定值?

How to get the "bar" value when the top-level object key is dynamic

Since the top-level key (like abcd1234) is variable and you need to target the first key in the parsed response object, here are solutions in common programming languages:

JavaScript

As you already hinted at with Object.keys(res)[0], this is the straightforward way to get the dynamic key and access the nested foo property:

// parsedResponse is your already parsed JSON object
const dynamicKey = Object.keys(parsedResponse)[0];
const barValue = parsedResponse[dynamicKey].foo;
console.log(barValue); // Logs "bar"

If you're using modern JavaScript (ES2017+), you can skip getting the key entirely and grab the first value directly with Object.values:

const barValue = Object.values(parsedResponse)[0].foo;

Or use destructuring for a cleaner look:

const [firstNestedObj] = Object.values(parsedResponse);
const { foo: barValue } = firstNestedObj;

Python

In Python, after parsing the JSON into a dictionary, you can get the first key using next(iter()) (since dictionaries in Python 3.7+ preserve insertion order):

import json

# Example parsed response (after json.loads())
parsed_response = {"abcd1234": {"foo": "bar"}}

# Option 1: Get the key first
dynamic_key = next(iter(parsed_response.keys()))
bar_value = parsed_response[dynamic_key]["foo"]

# Option 2: Directly get the first value
bar_value = next(iter(parsed_response.values()))["foo"]

print(bar_value) # Prints "bar"

Java (using Jackson)

If you're working with Java and using Jackson for JSON parsing, you can read the response into a Map and then retrieve the first entry:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;

public class DynamicKeyExample {
    public static void main(String[] args) throws Exception {
        String jsonResponse = "{ \"abcd1234\": { \"foo\": \"bar\" } }";
        ObjectMapper mapper = new ObjectMapper();
        
        // Parse into a map of maps
        Map<String, Map<String, String>> parsedMap = mapper.readValue(jsonResponse, Map.class);
        
        // Get the first entry's nested object and extract "foo"
        Map<String, String> nestedObject = parsedMap.entrySet().iterator().next().getValue();
        String barValue = nestedObject.get("foo");
        
        System.out.println(barValue); // Outputs "bar"
    }
}

Important Note

Keep in mind that older versions of some languages (like pre-ES2015 JavaScript or pre-3.7 Python) don't preserve object/dictionary key order. If you need to support these environments, make sure the API you're calling guarantees that the key you care about is the first one in the response, or adjust your logic to handle unordered keys appropriately.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:04:16