[Python] Ansible to Python: Error decoding JSON: Expecting property name enclosed in double...

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    In my Ansible config I have the following:

    new_attributes:
    tenantID: "4234iugob8y8l3-8809yo3x"
    new_attribute2: "value2"


    and the following in my tasks:

    "{{ new_attributes | to_json }}"


    and this in my Python script to access it:

    try:
    new_attributes = json.loads(new_attributes)
    except json.JSONDecodeError as e:
    print(f"Error decoding JSON: {e}")
    sys.exit(1)



    but I get the following error:

    "stderr_lines": [],
    "stdout": "Error decoding JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)",
    "stdout_lines": ["Error decoding JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"]}


    I am wondering if anyone can please help fix this.

    update:

    error output:

    Error decoding JSON: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"


    and output from the to_json on the ansible side:

    msg": "{\"tenantID\": \"4234iugob8y8l3-8809yo3x\", \"new_attribute2\": \"value2\"}


    This is how i am parsing to python:

    - name: Debug new_attributes as JSON
    debug:
    msg: "{{ new_attributes | to_json }}"

    - name: Insert data into DynamoDB for client "{{ client_name }}"
    command: >
    python3 dynamodb_insert_or_update.py
    "{{ dynamodb_table_name }}"
    "{{ client_name }}"
    "{{ applicationID }}"
    "{{ new_attributes | to_json }}"
    "{{ aws_region }}"
    "{{ aws_profile }}"
    args:
    chdir: "/mypath/scripts/"
    register: result
    loop: "{{ clients }}"
    loop_control:
    loop_var: item



    and my python script in full:

    import sys
    import boto3
    import json

    def insert_data(dynamodb_table_name, client_name, item_id, new_attributes, aws_region, aws_profile):
    session = boto3.Session(profile_name=aws_profile, region_name=aws_region)
    dynamodb = session.resource('dynamodb')
    table = dynamodb.Table(dynamodb_table_name)

    item = {
    'client_name': client_name,
    'applicationID': item_id
    }

    try:
    new_attributes = json.loads(new_attributes)
    except json.JSONDecodeError as e:
    print(f"Error decoding JSON: {e}")
    sys.exit(1)

    item.update(new_attributes)

    try:
    response = table.put_item(Item=item)
    print(f"Insert successful: {response}")
    except Exception as e:
    print(f"Failed to insert item: {e}")
    sys.exit(1)

    # Command-line arguments
    dynamodb_table_name = sys.argv[1]
    client_name = sys.argv[2]
    item_id = sys.argv[3]
    new_attributes = sys.argv[4]
    aws_region = sys.argv[5]
    aws_profile = sys.argv[6]

    # Call the insert function
    insert_data(dynamodb_table_name, client_name, item_id, new_attributes, aws_region, aws_profile)

    Continue reading...

Compartilhe esta Página