如何根据文本值的存在拆分JSON对象?

时间:2019-07-17 12:27:04

标签: python json

我正在使用Python中的Reqests模块从REST API中提取信息。数据以JSON格式返回-JSON数组的每个元素代表一个用户,groups子数组代表其Active Directory组。样本条目如下所示:

{
                    "id": 0101010101,
                    "createdAt": 111111,
                    "displayName": "Jones, Steve",
                    "firstName": "Steve",
                    "lastName": "Jones",
                    "groups": [
                        "All Users",
                        "Anyville Users (WIN10)",
                        "Windows 10 Users"
                    ],
                    "deviceCount": 0
                },
{
                    "id": 0101010102,
                    "createdAt": 111111,
                    "displayName": "Smith, Dave",
                    "firstName": "Dave",
                    "lastName": "Smith",
                    "groups": [
                        "All Users",
                        "Faketown Users (WIN7)",
                        "Windows 7 Users"
                    ],
                    "deviceCount": 0
                }

我如何遍历JSON数组,评估groups数组是否包含与一组位置(Anyville,Faketown,Nowehereland)中的任何一个匹配的文本,然后将该行放入每个位置的单独对象中?我最接近的是:

faketownUsers = {}
response = requests.request("GET", url, headers=headers)

ir = json.loads(response.text)
data = ir['result']['searchResults']
for row in data:
    if "Faketown" in str(row):
        faketownUsers.extend(row)

3 个答案:

答案 0 :(得分:0)

您的row代表完整的词典,但是您需要查看groups项的内部。 groupslist,而您要查找的字符串是该list项的一部分。 有很多方法可以解决,我将给出两个示例,方法1:

locations = {'Faketown': []}
for row in data:
    for group in row['groups']:
        if 'Faketown' in group:
            locations['Faketown'].append(row)

方法2:

locations = {'Faketown': []}
for row in data:
    if 'Faketown' in ''.join(row['groups']):
        locations['Faketown'].append(row)

您可以将其他任何位置添加到locations

答案 1 :(得分:0)

我会做类似的事情:

# load data, raise an exception on error
res = requests.get(url, headers=headers)
res.raise_for_status()

# get data from json response
data = res.json()['result']['searchResults']

# figure out which user groups match
locations = {'Faketown': [], 'Anyville': []}
for row in data:
    for locname, locarr in locations.items():
        if any(locname in grp for grp in row['groups']):
            locarr.append(row)

我已经更改了您的其他一些代码,以更好地检查/处理错误,主要是通过引发异常

答案 2 :(得分:0)

您可以将对象转换为字符串以检查它是否包含子字符串,而不是将其检查到每个字段中。

from collections import defaultdict
locations = defaultdict([])

locations_to_check = ['Anyville', 'Faketown', 'Nowehereland']
for row in data:
    for location in locations_to_check:
         if location in str(row['groups']):
            locations[location].append(row)

# output
# {'Anyville': [{...}, {...}], 'Faketown': [{...},{...}]}