Ansible Automation Platform API Call Delivers Bad JSON Format

Hello Community, :waving_hand:
today I tried to call Ansible Automation Platform (AAP) release 2.5 API, e.g.

/api/controller/v2/job_templates/?page_size=100

However, calling json.loads with the response fails. The returned JSON has the following deviations from the standard:

  • All strings are enclosed in single quotes instead of double quotes.
  • All boolean values (True and False) are not enclosed in double quotes.
  • All None values are also not enclosed in double quotes.

This causes json.loads to abort with an exception.

A month ago, the same Python source code worked without any problems.

In the header of the API call the content type is set to:

request.add_header("Content-Type", "application/json")

Is it possible to configure the JSON return format of API calls in the AAP?
Or is there another way to get a valid JSON response?

Thanks for hints and tips.
Best regards
Stefan

Hello Community,

I solved it on this way:

def jsonStringToDict(
    jsonString: str
) -> dict:

    jsonDict: dict = {}

    try:
        jsonDict = json.loads(jsonString)
    except:
        try:
            jsonDict = ast.literal_eval(jsonString)
        except Exception as err:
            raise Exception("An error occurred at JSON string to dictionary") from err

    return jsonDict

The first step is to try json.loads and, if it do not work, the second step is to try ast.literal_eval. It works now as expected.

Best regards
Stefan

1 Like