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

如何使用Python提取JSON字符串中display_value字段的值

Python Methods to Extract display_value

Since your response is valid JSON, using Python's built-in json module is the most reliable way to extract the value (avoid regex, which can break if the JSON structure changes).

Method 1: Extract directly from response.text (API response)

If you already have the API response text, parse it into a dictionary and navigate to the desired value:

import json

# Your API response text
response_text = '{"import_set":"ISET1677878","staging_table":"u_incident_create_import","result":[{"transform_map":"Incident Create Import","table":"incident","display_name":"number","display_value":"INC1301724","record_link":"https://example.com/api/now/table/incident/04331234rfgthyjjkkkkllljhfdd","status":"inserted","sys_id":"01234567890"}]}'

# Parse JSON string to Python dictionary
data = json.loads(response_text)

# Extract the display_value from the first item in the result list
display_value = data['result'][0]['display_value']
print(display_value)  # Output: INC1301724

Method 2: Extract from Response.txt file

If you need to read from the local text file instead:

import json

# Read content from the file
with open('Response.txt', 'r') as file:
    file_content = file.read()

# Parse and extract the value
data = json.loads(file_content)
display_value = data['result'][0]['display_value']
print(display_value)  # Output: INC1301724

Robust Version with Error Handling

To handle cases where keys might be missing or the JSON is invalid:

import json

def get_display_value(json_content):
    try:
        data = json.loads(json_content)
        # Use .get() to avoid KeyError if keys are missing
        return data.get('result', [{}])[0].get('display_value', 'Value not found')
    except json.JSONDecodeError:
        return 'Invalid JSON format'

# For response.text usage
print(get_display_value(response_text))

# For file usage
with open('Response.txt', 'r') as file:
    print(get_display_value(file.read()))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 02:45:22