import vk_api import pickle import json vk_session = vk_api.VkApi('login', 'Pass') # Π»ΠΎΠ³ΠΈΠ½ ΠΈ ΠΏΠ°Ρ€ΠΎΠ»ΡŒ vk_session.auth() vk = vk_session.get_api() def main(): y = vk.groups.getMembers(group_id='your_group_id', fields='contacts') # Id Π³Ρ€ΡƒΠΏΠΏΡ‹ ΠΈ Π½ΠΎΠΌΠ΅Ρ€Π° Ρ‚Π΅Π»Π΅Ρ„ΠΎΠ½ΠΎΠ² ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ этой Π³Ρ€ΡƒΠΏΠΏΡ‹ data = y json_str = json.dumps(data) resp = json.loads(json_str) print(resp) print(resp['items']) data_items = str(resp['items']) with open("Ρ„Π°ΠΉΠ».txt", "w") as file: print(data_items, file=file) if __name__ == '__main__': main() 

So I get a JSON of the form:

 {'items': [{'first_name': 'Оля', 'id': 111111, 'last_name': 'Π‘ΡƒΡ‰Π΅Π½ΠΊΠΎ'}, {'first_name': 'Π“Π΅ΠΎΡ€Π³ΠΈΠΉ', 'id': 222222, 'last_name': 'Голосов'}, {'first_name': 'Максим', 'id': 333333, 'home_phone':+79909999999 '', 'last_name': 'Π’ΡƒΠΏΠΈΡ‡Π΅Π½ΠΊΠΎΠ²'} 

which in turn is a dict - is the dictionary, I understand correctly? Tell me please.

And how can I export this JSON to csv? All my attempts ended only in that I put coutn in one line, and on the second, the whole 'answer' of JSON (in one line) with dictionary keys and LINKS TO DATA (how to draw this data) ?. Here, the difficulty is that the data obtained is not monotonous, someone has contact details and someone does not, and the home_number / mobile_number fields may be missing and I already anticipate what this will result in when exporting, at least there will be problems when creating columns in the table, tell me how to solve this problem correctly?

In desperation, I decided to unload all the data into a txt file, on output I got a txt file of type:

 {'first_name': 'Оля', 'id': 111111, 'last_name': 'Π‘ΡƒΡ‰Π΅Π½ΠΊΠΎ'}, {'first_name': 'Π“Π΅ΠΎΡ€Π³ΠΈΠΉ', 'id': 222222, 'last_name': 'Голосов'}, {'first_name': 'Максим', 'id': 333333, 'home_phone':+79909999999 '', 'last_name': 'Π’ΡƒΠΏΠΈΡ‡Π΅Π½ΠΊΠΎΠ²'} 

can it be possible to sort / fraud it somehow with a script to get the desired csv?

    1 answer 1

    To save a similar JSON in CSV, it must be turned into a flat (without attachments) structure.

    Pandas module allows you to do this in one command.

    having the following dictionary :

     In [80]: d Out[80]: {'items': [{'first_name': 'Оля', 'id': 111111, 'last_name': 'Π‘ΡƒΡ‰Π΅Π½ΠΊΠΎ'}, {'first_name': 'Π“Π΅ΠΎΡ€Π³ΠΈΠΉ', 'id': 222222, 'last_name': 'Голосов'}, {'first_name': 'Максим', 'id': 333333, 'home_phone': '+79909999999', 'last_name': 'Π’ΡƒΠΏΠΈΡ‡Π΅Π½ΠΊΠΎΠ²'}]} 

    You can easily turn it into a flat dataframe and save it as CSV:

     In [81]: import pandas as pd In [82]: df = pd.io.json.json_normalize(d['items']) In [83]: df Out[83]: first_name home_phone id last_name 0 Оля NaN 111111 Π‘ΡƒΡ‰Π΅Π½ΠΊΠΎ 1 Π“Π΅ΠΎΡ€Π³ΠΈΠΉ NaN 222222 Голосов 2 Максим +79909999999 333333 Π’ΡƒΠΏΠΈΡ‡Π΅Π½ΠΊΠΎΠ² In [84]: df.to_csv(r'c:/temp/out.csv', index=False) 

    You can do this with one command:

     pd.io.json.json_normalize(d['items']).to_csv(r'c:/temp/out.csv', index=False) 

    result ( C:\Temp\out.csv ):

     first_name,home_phone,id,last_name Оля,,111111,Π‘ΡƒΡ‰Π΅Π½ΠΊΠΎ Π“Π΅ΠΎΡ€Π³ΠΈΠΉ,,222222,Голосов Максим,+79909999999,333333,Π’ΡƒΠΏΠΈΡ‡Π΅Π½ΠΊΠΎΠ² 
    • 3
      Pandas is just awesome :) - gil9red
    • @ gil9red, absolutely agree! ;) - MaxU