Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python在嵌套json中查找特定值_Python_Json - Fatal编程技术网

使用Python在嵌套json中查找特定值

使用Python在嵌套json中查找特定值,python,json,Python,Json,我有一个包含大量嵌套json对象的文件。我在下面贴了一段。我正在尝试使用python查询文件中的所有对象,以拉出至少有一个自定义提要的对象—url值以“”开头。某些对象将没有任何自定义提要,而其他对象将有一个或多个自定义提要,每个自定义提要可能以我正在搜索的字符串开头,也可能不以该字符串开头。任何帮助都将不胜感激!我对Python非常陌生 示例JSON: [{ "empid": "12345", "values": { "custom_feeds": {

我有一个包含大量嵌套json对象的文件。我在下面贴了一段。我正在尝试使用python查询文件中的所有对象,以拉出至少有一个自定义提要的对象—url值以“”开头。某些对象将没有任何自定义提要,而其他对象将有一个或多个自定义提要,每个自定义提要可能以我正在搜索的字符串开头,也可能不以该字符串开头。任何帮助都将不胜感激!我对Python非常陌生

示例JSON

 [{
    "empid": "12345",
    "values": {
      "custom_feeds": {
        "custom_feeds": [
          {
            "name": "Bulletins",
            "url": "http://infoXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
          }
        ]
      },
      "gadgetTitle": "InfoSec Updates",
      "newWindow": false,
      "article_limit_value": 10,
      "show_source": true
    }
  },
  {
    "empid": "23456",
    "values": {
      "custom_feeds": {
        "custom_feeds": [
          {
            "name": "1 News",
            "url": "http://blogs.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
          },
          {
            "name": "2 News",
            "url": "http://info.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
          },
          {
            "name": "3 News",
            "url": "http://blogs.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
          },
          {
            "name": "4 News",
            "url": "http://commshare.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
          }
        ]
      },
      "gadgetTitle": "Org News",
      "newWindow": false,
      "article_limit_value": 10,
      "show_source": true
    }
  },  {
    "empid": "34567",
    "values": {
      "custom_feeds": {
        "custom_feeds": []
      },
      "gadgetTitle": "Org News",
      "newWindow": false,
      "article_limit_value": 10,
      "show_source": true
    }
  }]

假设您的文件名为
input.json
,并且希望每个提要都有对象,您可以解析json并使用列表理解创建一个新列表,其中提要满足您的条件:

import json

with open('input.json') as input_file:
    items = json.loads(input_file.read())

feeds = [{'name': feed['name'], 'url': feed['url'], 'empid': item['empid']}
    for item in items
    for feed in item['values']['custom_feeds']['custom_feeds']
    if feed['url'].startswith('http://commshare')]

assert feeds == [{'name': '4 News', 'url': 'http://commshare.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'empid': '23456'}]

使用
json.Load(open('path/to/input/file'))将其加载到python dict中
。遍历每个对象(字典)并检查
len(obj['values']['custom_feed']['custom_feed'])
我不相信这是有效的json这很好-谢谢!如何在上添加相应的empid,以便知道附加到每个url的empid?一种方法是使用
empid
字段(编辑以显示示例)在理解中构造一个新对象。