如何导入这种格式的嵌套对象.JSON文件?

时间:2018-03-02 14:07:40

标签: python json python-3.x

您好我正在尝试从此JSON文件导入数据以在我的python脚本中使用它。但是,我收到以下错误,我相信由于JSON对象的嵌套特性。我的最终目标是收集每个JSON对象的文件名和标签。我不关心重复项,类似于.JSON文件的当前格式。

File "test.py", line 4, in <module>
    big_json_file = json.loads(file_string)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

下面是python脚本“test.py”:

import json

file_string = 'output.json'
big_json_file = json.loads(file_string)
big_list_of_labels = []

for file in big_json_file:
    big_list_of_labels.append(file['Analysis']['Labels'])

以下是我尝试导入的.JSON文件:

[
   {
      "File_Name": "1.jpg",
      "Analysis": {
         "Labels": [
            {
               "Confidence": 94.77251434326172,
               "Name": "Flora"
            },
            {
               "Confidence": 94.77251434326172,
               "Name": "Grass"
            },
            {
               "Confidence": 94.77251434326172,
               "Name": "Plant"
            },
            {
               "Confidence": 78.49254608154297,
               "Name": "Animal"
            },
            {
               "Confidence": 78.49254608154297,
               "Name": "Cheetah"
            },
            {
               "Confidence": 78.49254608154297,
               "Name": "Mammal"
            },
            {
               "Confidence": 78.49254608154297,
               "Name": "Wildlife"
            },
            {
               "Confidence": 69.79740142822266,
               "Name": "Field"
            },
            {
               "Confidence": 69.79740142822266,
               "Name": "Grassland"
            },
            {
               "Confidence": 69.79740142822266,
               "Name": "Outdoors"
            },
            {
               "Confidence": 67.31356048583984,
               "Name": "Leisure Activities"
            },
            {
               "Confidence": 67.31356048583984,
               "Name": "Walking"
            },
            {
               "Confidence": 57.44683837890625,
               "Name": "Jaguar"
            },
            {
               "Confidence": 57.44683837890625,
               "Name": "Leopard"
            },
            {
               "Confidence": 57.44683837890625,
               "Name": "Panther"
            },
            {
               "Confidence": 55.88261032104492,
               "Name": "Bush"
            },
            {
               "Confidence": 55.88261032104492,
               "Name": "Vegetation"
            },
            {
               "Confidence": 53.4413948059082,
               "Name": "Lawn"
            }
         ],
         "ResponseMetadata": {
            "RetryAttempts": 0,
            "HTTPStatusCode": 200,
            "RequestId": "978e32e4-1da8-11e8-a380-cd680f89684e",
            "HTTPHeaders": {
               "date": "Thu, 01 Mar 2018 23:30:59 GMT",
               "x-amzn-requestid": "978e32e4-1da8-11e8-a380-cd680f89684e",
               "content-length": "947",
               "content-type": "application/x-amz-json-1.1",
               "connection": "keep-alive"
            }
         },
         "OrientationCorrection": "ROTATE_0"
      }
   }
][
   {
      "File_Name": "2.jpg",
      "Analysis": {
         "Labels": [
            {
               "Confidence": 98.57389068603516,
               "Name": "Astronomy"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Galaxy"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Nebula"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Night"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Outdoors"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Outer Space"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Space"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Universe"
            }
         ],
         "ResponseMetadata": {
            "RetryAttempts": 0,
            "HTTPStatusCode": 200,
            "RequestId": "98d2c109-1da8-11e8-a2d9-b91cf22c7f33",
            "HTTPHeaders": {
               "date": "Thu, 01 Mar 2018 23:30:59 GMT",
               "x-amzn-requestid": "98d2c109-1da8-11e8-a2d9-b91cf22c7f33",
               "content-length": "449",
               "content-type": "application/x-amz-json-1.1",
               "connection": "keep-alive"
            }
         },
         "OrientationCorrection": "ROTATE_0"
      }
   },
   {
      "File_Name": "2.jpg",
      "Analysis": {
         "Labels": [
            {
               "Confidence": 98.57389068603516,
               "Name": "Astronomy"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Galaxy"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Nebula"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Night"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Outdoors"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Outer Space"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Space"
            },
            {
               "Confidence": 98.57389068603516,
               "Name": "Universe"
            }
         ],
         "ResponseMetadata": {
            "RetryAttempts": 0,
            "HTTPStatusCode": 200,
            "RequestId": "98d2c109-1da8-11e8-a2d9-b91cf22c7f33",
            "HTTPHeaders": {
               "date": "Thu, 01 Mar 2018 23:30:59 GMT",
               "x-amzn-requestid": "98d2c109-1da8-11e8-a2d9-b91cf22c7f33",
               "content-length": "449",
               "content-type": "application/x-amz-json-1.1",
               "connection": "keep-alive"
            }
         },
         "OrientationCorrection": "ROTATE_0"
      }
   }
][
   {
      "File_Name": "3.jpg",
      "Analysis": {
         "Labels": [
            {
               "Confidence": 98.6434555053711,
               "Name": "Human"
            },
            {
               "Confidence": 98.6434097290039,
               "Name": "People"
            },
            {
               "Confidence": 98.6434555053711,
               "Name": "Person"
            },
            {
               "Confidence": 96.43375396728516,
               "Name": "Audience"
            },
            {
               "Confidence": 96.43375396728516,
               "Name": "Crowd"
            },
            {
               "Confidence": 96.43375396728516,
               "Name": "Speech"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Badge"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Emblem"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Logo"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Trademark"
            },
            {
               "Confidence": 59.94097137451172,
               "Name": "Worker"
            }
         ],
         "ResponseMetadata": {
            "RetryAttempts": 0,
            "HTTPStatusCode": 200,
            "RequestId": "99394a95-1da8-11e8-876f-6db2ab84a252",
            "HTTPHeaders": {
               "date": "Thu, 01 Mar 2018 23:30:59 GMT",
               "x-amzn-requestid": "99394a95-1da8-11e8-876f-6db2ab84a252",
               "content-length": "583",
               "content-type": "application/x-amz-json-1.1",
               "connection": "keep-alive"
            }
         },
         "OrientationCorrection": "ROTATE_0"
      }
   },
   {
      "File_Name": "3.jpg",
      "Analysis": {
         "Labels": [
            {
               "Confidence": 98.6434555053711,
               "Name": "Human"
            },
            {
               "Confidence": 98.6434097290039,
               "Name": "People"
            },
            {
               "Confidence": 98.6434555053711,
               "Name": "Person"
            },
            {
               "Confidence": 96.43375396728516,
               "Name": "Audience"
            },
            {
               "Confidence": 96.43375396728516,
               "Name": "Crowd"
            },
            {
               "Confidence": 96.43375396728516,
               "Name": "Speech"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Badge"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Emblem"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Logo"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Trademark"
            },
            {
               "Confidence": 59.94097137451172,
               "Name": "Worker"
            }
         ],
         "ResponseMetadata": {
            "RetryAttempts": 0,
            "HTTPStatusCode": 200,
            "RequestId": "99394a95-1da8-11e8-876f-6db2ab84a252",
            "HTTPHeaders": {
               "date": "Thu, 01 Mar 2018 23:30:59 GMT",
               "x-amzn-requestid": "99394a95-1da8-11e8-876f-6db2ab84a252",
               "content-length": "583",
               "content-type": "application/x-amz-json-1.1",
               "connection": "keep-alive"
            }
         },
         "OrientationCorrection": "ROTATE_0"
      }
   },
   {
      "File_Name": "3.jpg",
      "Analysis": {
         "Labels": [
            {
               "Confidence": 98.6434555053711,
               "Name": "Human"
            },
            {
               "Confidence": 98.6434097290039,
               "Name": "People"
            },
            {
               "Confidence": 98.6434555053711,
               "Name": "Person"
            },
            {
               "Confidence": 96.43375396728516,
               "Name": "Audience"
            },
            {
               "Confidence": 96.43375396728516,
               "Name": "Crowd"
            },
            {
               "Confidence": 96.43375396728516,
               "Name": "Speech"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Badge"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Emblem"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Logo"
            },
            {
               "Confidence": 80.42803192138672,
               "Name": "Trademark"
            },
            {
               "Confidence": 59.94097137451172,
               "Name": "Worker"
            }
         ],
         "ResponseMetadata": {
            "RetryAttempts": 0,
            "HTTPStatusCode": 200,
            "RequestId": "99394a95-1da8-11e8-876f-6db2ab84a252",
            "HTTPHeaders": {
               "date": "Thu, 01 Mar 2018 23:30:59 GMT",
               "x-amzn-requestid": "99394a95-1da8-11e8-876f-6db2ab84a252",
               "content-length": "583",
               "content-type": "application/x-amz-json-1.1",
               "connection": "keep-alive"
            }
         },
         "OrientationCorrection": "ROTATE_0"
      }
   }
]

2 个答案:

答案 0 :(得分:3)

所以问题是json.loads期望传递实际的json(字符串),而不是文件名。您可以使用with打开一个文件,并使用json.load中的filepointer。

import json

with open('output.json') as thefile:
    big_json_file = json.load(thefile)
    big_list_of_labels = []

    for file in big_json_file:
        big_list_of_labels.append(file['Analysis']['Labels'])

此外,您的json无效..在两个地方需要,而不是][

答案 1 :(得分:1)

也许阅读文档会有所帮助吗?

  

json.loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

     

将s(包含JSON文档的str,bytes或bytearray实例)反序列化为Python对象

以上是几行的例子:

>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
['foo', {'bar': ['baz', None, 1.0, 2]}]

在您的代码段中:

file_string = 'output.json'
big_json_file = json.loads(file_string)

文字字符串"output.json"显然不是JSON文档。它可能是包含json文档的文件的名称,但不是json.loads()所期望的(参见示例)。

您希望它阅读文件的内容并将其传递给json.loads()

with open ("output.json") as f:
    data = json.loads(f.read())
print(data)

可以使用json.load()(没有&#39; s&#39;)进一步简化,而不是将打开的文件作为参数:

with open ("output.json") as f:
    data = json.load(f)
print(data)