2017-05-31 273 views
1

我有以下结构的JSON:循环通过多维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': {} 
    } 
    ] 
} 

这两个键 - 值对的JSON。第二对的值是更多JSON的列表。 对我来说,太多的信息,我想有这样的JSON:

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

我希望有一个JSON只包含一个键(“应用程序”)和它的价值,这将是一个列表更多的JSON只有三个键值对。 我很感激任何建议。

谢谢你的帮助!

+3

这应该是很容易实现的,你有没有尝试过这么远吗? – errata

+0

对不起,我绝对是新的StackOverflow ... 是的,我试过一个for-loop工作,但它感觉不对.. –

回答

0

@ bishakh-戈什我不认为你需要使用输入JSON作为字符串。它可以直接用作字典。 (从而避免ast

一个更简洁的方式:

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

而且这里的步骤:

定义要保持

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

定义新的字典了什么键slice_keys上的切片

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

就是这样。

这应该产生格式化成你想要的JSON,e.g

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

感谢您的意见!其实这正是我需要的!谢谢 ! :-) –

+0

伟大:)随时解决问题,然后! – Arnaud

+0

好的,明白! :) –

0

这可能是你在找什么:

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