简体   繁体   English

如何在pandas数据帧中读取mongodb导出的Json

[英]How to read mongodb exported Json in pandas dataframe

I'm using the following code to export a json from a mongoDB query: 我正在使用以下代码从mongoDB查询中导出json:

 querywith open(r'/Month/Applications_test.json', 'w') as f:
for x in dic:
    json.dump(x, f, default=json_util.default)

That is working well and is returning the following json: 这很好用,并返回以下json:

{
  "_class": "Application",
  "_id": "123",
  "applicationTimeStamp": {
    "$date": 1541466008000
  },
  "createdDateTime": {
    "$date": 1541466008084
  }
}
{
  "_class": "Application",
  "_id": "124",
  "applicationTimeStamp": {
    "$date": 1540080000000
  },
  "createdDateTime": {
    "$date": 1540080000096
  }
}
{
  "_class": "Application",
  "_id": "125",
  "applicationTimeStamp": {
    "$date": 1540080000000
  },
  "createdDateTime": {
    "$date": 1540080000097
  }
}

I'm using the following pandas code to try to read it: 我正在使用以下pandas代码尝试阅读它:

data_df = pd.read_json(r'/Month/Applications_test.json', lines = True)

I'm getting the following error: 我收到以下错误:

ValueError: Unexpected character found when decoding array value (2)

What I want is a pandas dataframe that has: 我想要的是一个pandas数据帧:

_class      | _id | applicationTimeStamp | createdDateTime
Application | 123 | 10/07/2018           | 10/07/2018
Application | 124 | 10/07/2018           | 10/07/2018
Application | 125 | 10/07/2018           | 10/07/2018

How could I read the json above into a pandas dataframe? 我怎么能将上面的json读入熊猫数据帧?

Thank you! 谢谢!

you have to use read_json in this way: 你必须以这种方式使用read_json:

df = pd.read_json(path_or_buf="file_path\json.txt",  typ='frame')

it returns a dataframe like: 它返回一个数据帧,如:

            _class  _id  applicationTimeStamp  createdDateTime
$date  Application  123         1541466008000    1541466008084

or: 要么:

        _class            ...                      createdDateTime
0  Application            ...             {'$date': 1541466008084}
1  Application            ...             {'$date': 1540080000096}
2  Application            ...             {'$date': 1540000000097}

then you can convert timestamp. 那么你可以转换时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM