如何规范化此特定的嵌套json?

时间:2020-05-22 23:00:58

标签: json python-3.x nested normalization

我从Nasa API获取了一些数据,并希望对其进行规范化。在某种程度上,我做到了,但是我不知道如何显示更深层的嵌套列表/字典。 这是我的json文件:

{
    "collection": {
        "href": "https://images-api.nasa.gov/search?q=galaxy&description=andromeda",
        "items": [
            {
                "href": "https://images-assets.nasa.gov/image/PIA04921/collection.json",
                "data": [
                    {
                        "media_type": "image",
                        "nasa_id": "PIA04921",
                        "secondary_creator": "NASA/JPL/California Institute of Technology",
                        "keywords": [
                            "Galaxy Evolution Explorer GALEX"
                        ],
                        "description_508": "This image is from NASA Galaxy Evolution Explorer is an observation of the large galaxy in Andromeda, Messier 31. The Andromeda galaxy is the most massive in the local group of galaxies that includes our Milky Way.",
                        "description": "This image is from NASA Galaxy Evolution Explorer is an observation of the large galaxy in Andromeda, Messier 31. The Andromeda galaxy is the most massive in the local group of galaxies that includes our Milky Way.",
                        "date_created": "2003-12-10T22:41:32Z",
                        "center": "JPL",
                        "title": "Andromeda Galaxy"
                    }
                ],
                "links": [
                    {
                        "href": "https://images-assets.nasa.gov/image/PIA04921/PIA04921~thumb.jpg",
                        "rel": "preview",
                        "render": "image"
                    }
                ]
            },
            {
                "href": "https://images-assets.nasa.gov/image/PIA08787/collection.json",
                "data": [
                    {
                        "media_type": "image",
                        "nasa_id": "PIA08787",
                        "secondary_creator": "NASA/JPL-Caltech",
                        "keywords": [
                            "Galaxy Evolution Explorer GALEX,Spitzer Space Telescope"
                        ],
                        "description_508": "The many personalities of our great galactic neighbor, the Andromeda galaxy, are exposed in this new composite image from NASA Galaxy Evolution Explorer and Spitzer Space Telescope. ",
                        "description": "The many personalities of our great galactic neighbor, the Andromeda galaxy, are exposed in this new composite image from NASA Galaxy Evolution Explorer and Spitzer Space Telescope. ",
                        "date_created": "2006-09-28T23:38:45Z",
                        "center": "JPL",
                        "title": "Amazing Andromeda Galaxy"
                    }
                ],
                "links": [
                    {
                        "href": "https://images-assets.nasa.gov/image/PIA08787/PIA08787~thumb.jpg",
                        "rel": "preview",
                        "render": "image"
                    }
                ]
            },
            {
                "href": "https://images-assets.nasa.gov/image/PIA20970/collection.json",
                "data": [
                    {
                        "media_type": "image",
                        "nasa_id": "PIA20970",
                        "description": "NASA's Nuclear Spectroscope Telescope Array, or NuSTAR, has identified a candidate pulsar in Andromeda -- the nearest large galaxy to the Milky Way. This likely pulsar is brighter at high energies than the Andromeda galaxy's entire black hole population.  The inset image shows the pulsar candidate in blue, as seen in X-ray light by NuSTAR. The background image of Andromeda was taken by NASA's Galaxy Evolution Explorer in ultraviolet light.  Andromeda is a spiral galaxy like our Milky Way but larger in size. It lies 2.5 million light-years away in the Andromeda constellation.  http://photojournal.jpl.nasa.gov/catalog/PIA20970",
                        "secondary_creator": "NASA/JPL-Caltech/GSFC/JHU",
                        "description_508": "NASA's NuSTAR has identified a candidate pulsar in Andromeda, the nearest large galaxy to the Milky Way. This likely pulsar is brighter at high energies than the Andromeda galaxy entire black hole population.",
                        "keywords": [
                            "Galaxy Evolution Explorer GALEX,NuSTAR",
                            "Andromeda galaxy"
                        ],
                        "date_created": "2017-03-23T15:50:41Z",
                        "center": "JPL",
                        "title": "Pulsar Candidate in Andromeda"
                    }
                ],
                "links": [
                    {
                        "href": "https://images-assets.nasa.gov/image/PIA20970/PIA20970~thumb.jpg",
                        "rel": "preview",
                        "render": "image"
                    }
                ]
            }
            ...

我已经使用以下代码对json进行了规范化:

data = pd.json_normalize(data = request['collection']['items'], record_path = 'data', meta=['href','links'])

,输出为:

|    |                                                     description_508                                                      |                                                       description                                                        | center  |              secondary_creator               |     date_created      | media_type  | nasa_id   |                          keywords                           |             title              | photographer  | album  | location  |                             href                               |                                                        links                                                        |
+====+==========================================================================================================================+==========================================================================================================================+=========+==============================================+=======================+=============+===========+=============================================================+================================+===============+========+===========+================================================================+=====================================================================================================================+
| 0  | This image is from NASA Galaxy Evolution Explorer is an observation of the large galaxy in Andromeda, Messier 31. Th...  | This image is from NASA Galaxy Evolution Explorer is an observation of the large galaxy in Andromeda, Messier 31. Th...  | JPL     | NASA/JPL/California Institute of Technology  | 2003-12-10T22:41:32Z  | image       | PIA04921  | [Galaxy Evolution Explorer GALEX]                           | Andromeda Galaxy               | NaN           | NaN    | NaN       | https://images-assets.nasa.gov/image/PIA04921/collection.json  | [{'render': 'image', 'href': 'https://images-assets.nasa.gov/image/PIA04921/PIA04921~thumb.jpg', 'rel': 'preview'}] |
+----+--------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------+---------+----------------------------------------------+-----------------------+-------------+-----------+-------------------------------------------------------------+--------------------------------+---------------+--------+-----------+----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------+
| 1  | The many personalities of our great galactic neighbor, the Andromeda galaxy, are exposed in this new composite image...  | The many personalities of our great galactic neighbor, the Andromeda galaxy, are exposed in this new composite image...  | JPL     | NASA/JPL-Caltech                             | 2006-09-28T23:38:45Z  | image       | PIA08787  | [Galaxy Evolution Explorer GALEX,Spitzer Space Telescope]   | Amazing Andromeda Galaxy       | NaN           | NaN    | NaN       | https://images-assets.nasa.gov/image/PIA08787/collection.json  | [{'render': 'image', 'href': 'https://images-assets.nasa.gov/image/PIA08787/PIA08787~thumb.jpg', 'rel': 'preview'}] |
+----+--------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------+---------+----------------------------------------------+-----------------------+-------------+-----------+-------------------------------------------------------------+--------------------------------+---------------+--------+-----------+----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------+
| 2  | NASA's NuSTAR has identified a candidate pulsar in Andromeda, the nearest large galaxy to the Milky Way. This likely...  | NASA's Nuclear Spectroscope Telescope Array, or NuSTAR, has identified a candidate pulsar in Andromeda -- the neares...  | JPL     | NASA/JPL-Caltech/GSFC/JHU                    | 2017-03-23T15:50:41Z  | image       | PIA20970  | [Galaxy Evolution Explorer GALEX,NuSTAR, Andromeda galaxy]  | Pulsar Candidate in Andromeda  | NaN           | NaN    | NaN       | https://images-assets.nasa.gov/image/PIA20970/collection.json  | [{'render': 'image', 'href': 'https://images-assets.nasa.gov/image/PIA20970/PIA20970~thumb.jpg', 'rel': 'preview'}] |
+----+--------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------+---------+----------------------------------------------+-----------------------+-------------+-----------+-------------------------------------------------------------+--------------------------------+---------------+--------+-----------+----------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------+

我们可以看到还有另外一列要解压:“ links”是一个列表[],带有一个字典{},其中有三个键:“ render”,“ href”,“ reld”。我的问题是如何将“链接”的内容作为与其他列一样的单独列连接到表?如果能看到解决此问题的其他方法,那就太好了。

0 个答案:

没有答案