Skip to content
TheChatArchiveThe Chat Archive
o3-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

How to extract json data from a string in Python? Let's say the input is "{'key':…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-opus-4-20250514’s answer.
  1. User

    How to extract json data from a string in Python? Let's say the input is "{'key': 'value'}".

  2. Assistant

    Below are a few common scenarios and the corresponding “recipes” for turning the string you have into an actual Python object.


    1. The string really is valid JSON

    (e.g. it uses double quotes like {"key": "value"}).

    import json
    
    raw = '{"key": "value"}'
    data = json.loads(raw)
    print(data)          # {'key': 'value'}
    print(data['key'])   # value
    

    2. The string is Python-style (single quotes)

    (e.g. "{'key': 'value'}").
    This isn’t valid JSON, so json.loads() will choke. Two quick fixes:

    2-a. Safest: use ast.literal_eval

    import ast
    
    raw = "{'key': 'value'}"
    data = ast.literal_eval(raw)    # safely evaluates basic Python literals
    print(data)                     # {'key': 'value'}
    

    ast.literal_eval() only understands literals (dicts, lists, numbers, strings, booleans, None) so it can’t execute arbitrary code—making it safer than eval().

    2-b. Quick-n-dirty: swap quotes then load

    import json
    
    raw = "{'key': 'value'}"
    json_ready = raw.replace("'", '"')
    data = json.loads(json_ready)
    

    Only use this if you’re sure your values don’t contain legitimate single quotes themselves.


    3. The JSON is embedded inside other text

    Imagine:

    raw = 'Error-123: response={"key": "value", "num": 42} END'
    

    Minimalistic extraction (works for simple, flat JSON blocks):

    import re, json
    
    m = re.search(r'\{.*?\}', raw)  # first {...} non-greedy
    if not m:
        raise ValueError("No JSON found")
    data = json.loads(m.group(0))
    

    For nested/bracketed JSON or multiple objects, consider a streaming parser such as ijson, or a third-party package like jsonfinder.


    4. Non-standard JSON (trailing commas, comments, etc.)

    Use a lenient parser instead of the stdlib one, e.g.:

    pip install json5
    
    import json5
    
    raw = "{'key': 'value',}"    # trailing comma
    data = json5.loads(raw)
    

    TL;DR

    • Valid JSON → json.loads()
    • Python-literal dict → ast.literal_eval()
    • Dirty quick fix → replace quotes or use a tolerant parser (e.g. json5)
    • JSON buried in text → regex / streaming parser to isolate, then json.loads()

    Pick the approach that matches the exact shape of your input. 👍

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.