2017-05-31 48 views
1

Ich habe eine JSON mit folgenden Struktur:Schleife durch mehrdimensionale JSON (Python)

{ 
    'count': 93, 
    'apps' : [ 
     { 
     'last_modified_at': '2016-10-21T12:20:26Z', 
     'frequency_caps': [], 
     'ios': { 
      'enabled': True, 
      'push_enabled': False, 
      'app_store_id': 'bbb', 
      'connection_type': 'certificate', 
      'sdk_api_secret': '--' 
     }, 
     'organization_id': '--', 
     'name': '---', 
     'app_id': 27, 
     'control_group_percentage': 0, 
     'created_by': { 
      'user_id': 'abc', 
      'user_name': 'def' 
     }, 
     'created_at': '2016-09-28T11:41:24Z', 
     'web': {} 
    }, { 
     'last_modified_at': '2016-10-12T08:58:57Z', 
     'frequency_caps': [], 
     'ios': { 
      'enabled': True, 
      'push_enabled': True, 
      'app_store_id': '386304604', 
      'connection_type': 'certificate', 
      'sdk_api_secret': '---', 
      'push_expiry': '2018-01-14T08:24:09Z' 
     }, 
     'organization_id': '---', 
     'name': '---', 
     'app_id': 87, 
     'control_group_percentage': 0, 
     'created_by': { 
      'user_id': '----', 
      'user_name': '---' 
     }, 
     'created_at': '2016-10-12T08:58:57Z', 
     'web': {} 
    } 
    ] 
} 

Es ist ein JSON mit zwei Schlüsseln-Wert-Paaren. Der Wert des zweiten Paares ist eine Liste von mehr JSONs. Für mich ist es zu viel Information, und ich möchte ein JSON wie diese haben:

{ 
    'apps' : [ 
     { 
     'name': 'Appname', 
     'app_id' : 1234, 
     'organization_id' : 'Blablabla' 
     }, 
     { 
     'name': 'Appname2', 
     'app_id' : 5678, 
     'organization_id' : 'Some other Organization' 
     } 
    ] 
} 

Ich möchte ein JSON haben, die einen Schlüssel („Apps“) und sein Wert nur enthält, die eine Liste wäre von mehr JSONs, die nur drei Schlüssel-Wert-Paare haben. Ich bin dankbar für jeden Rat.

Vielen Dank für Ihre Hilfe!

+3

Dies sollte ziemlich einfach sein zu erreichen, haben Sie etwas versuchen, so weit? – errata

+0

Sorry, ich bin absolut neu in StackOverflow ... Ja, ich habe versucht, eine for-Schleife, die funktionierte, aber es fühlte sich falsch .. –

Antwort

0

@ bishakh-ghosh ich Sie nicht denken, müssen die Eingabe json als String verwenden. Es kann direkt als Wörterbuch verwendet werden. (So ​​vermeiden ast)

Ein prägnanter Weise:

# your original json 
input_ = { 'count': 93, ... } 

Und hier sind die Schritte:

definieren, welche Tasten Sie

slice_keys = ['name', 'app_id', 'organization_id'] 

definieren das neue Wörterbuch behalten möchten als eine Scheibe auf der Scheibe_Teile

dict(apps=[{key:value for key,value in d.items() if key in slice_keys} for d in input_['apps']]) 

Und das ist es.

, dass die JSON ergeben sollte so formatiert, wie Sie wollen, z

{ 
'apps': 
    [ 
    {'app_id': 27, 'name': '---', 'organization_id': '--'}, 
    {'app_id': 87, 'name': '---', 'organization_id': '---'} 
    ] 
} 
+0

Vielen Dank für den Hinweis! Eigentlich war das genau was ich brauchte! Vielen Dank ! :-) –

+0

Großartig :) Fühlen Sie sich frei, die Frage dann zu lösen! – Arnaud

+0

ok, ich hab's! :) –

0

Dies könnte das sein, was Sie suchen:

import ast 
import json 
json_str = """{ 
    'count': 93, 
    'apps' : [ 
     { 
     'last_modified_at': '2016-10-21T12:20:26Z', 
     'frequency_caps': [], 
     'ios': { 
      'enabled': True, 
      'push_enabled': False, 
      'app_store_id': 'bbb', 
      'connection_type': 'certificate', 
      'sdk_api_secret': '--' 
     }, 
     'organization_id': '--', 
     'name': '---', 
     'app_id': 27, 
     'control_group_percentage': 0, 
     'created_by': { 
      'user_id': 'abc', 
      'user_name': 'def' 
     }, 
     'created_at': '2016-09-28T11:41:24Z', 
     'web': {} 
    }, { 
     'last_modified_at': '2016-10-12T08:58:57Z', 
     'frequency_caps': [], 
     'ios': { 
      'enabled': True, 
      'push_enabled': True, 
      'app_store_id': '386304604', 
      'connection_type': 'certificate', 
      'sdk_api_secret': '---', 
      'push_expiry': '2018-01-14T08:24:09Z' 
     }, 
     'organization_id': '---', 
     'name': '---', 
     'app_id': 87, 
     'control_group_percentage': 0, 
     'created_by': { 
      'user_id': '----', 
      'user_name': '---' 
     }, 
     'created_at': '2016-10-12T08:58:57Z', 
     'web': {} 
    } 
    ] 
}""" 

json_dict = ast.literal_eval(json_str) 

new_dict = {} 
app_list = [] 

for appdata in json_dict['apps']: 
    appdata_dict = {} 
    appdata_dict['name'] = appdata['name'] 
    appdata_dict['app_id'] = appdata['app_id'] 
    appdata_dict['organization_id'] = appdata['organization_id'] 
    app_list.append(appdata_dict) 

new_dict['apps'] = app_list 

new_json_str = json.dumps(new_dict) 

print(new_json_str) # This is your resulting json string