0

I wrote little script on python 3.7 to receive actual browser version

Here is it:

import json


def json_open():
    file_json = open('/Users/user/PycharmProjects/Test/configuration.json')
    return json.load(file_json)


def get_last_version(browser_name):
    f = json_open()
    res = (f['global']['link_to_latest_browser_version'])
    last_version = repr(res.json()['latest']['client'][browser_name]['version'])
    #print(last_version[1:-1])
    return last_version[1:-1]

Also, json file exists, but it does not matter now.

Received:

AttributeError: 'str' object has no attribute 'json'.

In row

last_version = repr(res.json()['latest']['client'][browser_name]['version'])

Please, tell me what is my mistake?

1
  • 1
    Can you show the JSON file or at least the structure? Commented Jun 2, 2020 at 14:09

4 Answers 4

4

If you are trying to convert res as a json object try json.loads(res) instead of res.json()

Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

import json

FILEJSON = '/Users/user/PycharmProjects/Test/configuration.json'

def get_last_version(browser_name):
    with open(FILEJSON, 'r') as fson:  
        res = json.load(fson)
    last_version = res['global']['link_to_latest_browser_version']\
                    ['latest']['client'][browser_name]['version'][1:-1]
    return last_version

I think that the json_open function is unnecessary. Also take into account that the behavior of the json.load() method depends on the type of file you are reading.

1 Comment

I have corrected the answer, there was a typo. I meant json.load
1

Ok, the problem is here:

    last_version = repr(res.json()['latest']['client'][browser_name]['version'])

A JSON object is basically a dictionary. So when you do json['key'] it returns the content, not a json object.

Here res is a string, not a json object and thus does not have the .json() attribute.

Edit:
If you want a string to be return in your situation:

res = json.loads(f['global']['link_to_latest_browser_version'])
last_version = res['latest']['client'][browser_name]['version']

return last_version

2 Comments

It is idea, I want to receive output exactly in string? Is it possible?
Just return res, it is already a string. Try to print(res) or print(type(res)), you'll see !
0

Your "res" variable is of type string. Strings do not have an attribute called json. So res.json() is invalid.

4 Comments

I want to receive return exactly in str. Any ideas how perform it?
Could you be more clear on what exactly it is you want to achieve with res.json()?
The output should be '83.0.4103.61'. Type str
What is the value of res ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.