I am writing a bot for vk.com, which will post pictures on the group page. There is such a function:

upload_url = api.photos.getWallUploadServer(group_id=Settings.owner_id) files = {'photo': open('img/img.jpg', 'rb')} r = requests.post(upload_url["upload_url"], files=files) # отправляем файл print(r.text) dict_param = json.loads(r.text) # Парсим json server = dict_param["server"] hash_photo = dict_param["hash"] photo = dict_param["photo"] save = api.photos.saveWallPhoto(group_id="143550839", server=server, hash=hash_photo, photo=photo) 

saveWallPhoto returns the following error: vk.exceptions.VkAPIError: 121. Invalid hash. Googling, I learned that such an error may occur due to decoding JSON, which is returned by the server and is stored in my variable r. How to access JSON values ​​without decoding it?

Closed due to the fact that off-topic party PashaPash 10 May '17 at 20:17 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - PashaPash
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • You can't decode a photo, and you don't decode it anyway; In this regard, your code is correct. The error arises because of something else - andreymal
  • What does print (r.text) print? - andreymal
  • @andreymal goo.gl/380ILj is like here. - Dmitry_Che
  • one
    1- use r.json() , not json.loads(r.text) 2- forget the advice that says that you should get information from a json document using something other than the json parser (or you misunderstand bad advice). - jfs
  • one
    In this repository there is an example of downloading a photo github.com/Kwentar/vkontakte-api-for-python - Andrio Skur

2 answers 2

Through regular expressions, use

 result = re.search(r'(str[\: ]+)(value)', json) 

where str is the required parameter, value is its value, json is your JSON, and the result variable will return all matches.

Regularly approximately, it will be necessary to adapt for a specific search.

  • Is this code for python ? - Pavel Durmanov
  • @Alban yes, sorry, gave an example for PHP, but for Python there is a similar function re.search (). - Dmitry Maslennikov

The problem is in

 upload_url = api.photos.getWallUploadServer(group_id=Settings.owner_id) 

group_id must be greater than zero.