[Python] json.loads getting Expecting value: line 1 column 2 (char 1) even if the value seems...

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm parsing a complete JSON file using json.loads and working on it. One of the attributes of the elements I'm working on contains a string formatted as a list, like this:

    { "value": "[\"13700\",\"10004\"]" }


    Since I need to work on each list value separately, I convert this string into a list using json.loads. Usually this works, however sometimes it doesn't, I get a Expecting value: line 1 column 2 (char 1) error and I don't know why. Here's a part of the JSON file with two "component" examples, each one with a "value" element containing such a list string. My code manages to parse the first one but not the second one:

    {
    "rules": [
    {
    "components":[
    {
    "id": "259177",
    "component": "CONDITION",
    "schemaVersion": 3,
    "type": "jira.issue.condition",
    "value": {
    "selectedField": {
    "type": "ID",
    "value": "issuetype"
    },
    "selectedFieldType": "issuetype",
    "comparison": "ONE_OF",
    "compareValue": {
    "type": "ID",
    "value": "[\"13700\",\"10004\"]",
    "multiValue": true
    }
    },
    "children": [],
    "conditions": [],
    "optimisedIds": [],
    "newComponent": false
    },
    {
    "id": "272355",
    "component": "CONDITION",
    "schemaVersion": 3,
    "type": "jira.issue.condition",
    "value": {
    "selectedField": {
    "type": "ID",
    "value": "status"
    },
    "comparison": "ONE_OF",
    "compareValue": {
    "type": "ID",
    "value": "[\"10300\",\"14205\",\"15613\",\"3\",\"14200\",\"14204\",\"14219\",\"15608\",\"15609\",\"15610\",\"15611\",\"15612\"]",
    "multiValue": true
    }
    },
    "children": [],
    "conditions": [],
    "optimisedIds": [],
    "newComponent": false
    }
    ]
    }
    ]
    }


    Here's a version of the code that I think encapsulates the major elements of what I'm doing and that could cause the issue:

    import json

    def jiraissuecondition(component):
    field = component['value']['selectedField']
    if 'compareValue' in component['value']:
    if component['value']['compareValue']['type'] == 'ID':
    if component['value']['compareValue']['value']:
    try:
    print(str(component['value']['compareValue']))
    value = json.loads(str(component['value']['compareValue']['value']))
    except ValueError as e:
    print(str(e))

    functions = {'jira.issue.condition':jiraissuecondition}

    def convert(components):
    for component in components:
    if component['type'] == 'jira.issue.condition':
    functions[component['type']](component)

    with open(r'C:\Users\jsonfile.json',encoding='utf-8') as f:
    file = json.load(f)
    rules = file['rules']
    for rule in rules:
    convert(rule['components'])


    I'm really unable to understand why one works and the other doesn't. I'm testing the component['value']['compareValue']['value'] element is not empty before trying to parse it... I don't know.

    Does anyone have any idea?

    Continue reading...

Compartilhe esta Página