Home:ALL Converter>How to write json output to a csv file in python?

How to write json output to a csv file in python?

Ask Time:2019-09-25T00:19:51         Author:Pelide

Json Formatter

I would write in a CSV the JSON output I have from an http request but I'm having this error:

TypeError: the JSON object must be str, bytes or bytearray, not list

here the snap of my code:

my_json = json.loads(resp_obj)

    with open("wiki.txt", "a") as myfile:

            writer = csv.writer(myfile)

            for item in my_json["mainsnak"]["datavalue"]:

                    writer.writerow([item, "https://www.geonames.org/{}".format(item["value"])])  #Write row.


I tried with this but I still have the error.

Here the resulting JSON from the request:

        "id": "Q6761$59FB3973-0123-4EB4-9C98-F7FEB6AAA32B",
        "mainsnak": {
            "datatype": "external-id",
            "datavalue": {
                "type": "string",
                "value": "6540122"
            "hash": "e7602dcd11d9a83e46716925865bca8e36a9b12c",
            "property": "P1566",
            "snaktype": "value"
        "rank": "normal",
        "references": [
                "hash": "88694a0f4d1486770c269f7db16a1982f74da69d",
                "snaks": {
                    "P248": [
                            "datatype": "wikibase-item",
                            "datavalue": {
                                "type": "wikibase-entityid",
                                "value": {
                                    "entity-type": "item",
                                    "id": "Q830106",
                                    "numeric-id": 830106
                            "hash": "1b3ef912a2bd61e18dd43abd184337eb010b2e96",
                            "property": "P248",
                            "snaktype": "value"
                "snaks-order": [
        "type": "statement"

In the CSV file I would parse just "value": "6540122"

Author:Pelide,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/58084486/how-to-write-json-output-to-a-csv-file-in-python
Baldrickk :

Your problem is not in writing to the csv file, it's in decoding the json data in the first place.\n\nusing your json data as per this question as a string, and passing it into the json.loads() function:\n\n>>> import json\n>>> my_json = json.loads(json_str)\n>>>\n\n\n(no error)\n\nHowever, if we pass that within a list:\n\n>>> my_json = json.loads([json_str])\nTraceback (most recent call last):\n File \"<stdin>\", line 1, in <module>\n File \"/usr/lib/python3.4/json/__init__.py\", line 312, in loads\n s.__class__.__name__))\nTypeError: the JSON object must be str, not 'list'\n>>>\n\n\nWe get the same exception that you get.\n\nCheck the structure of your resp_obj object. I think you will find that it is being passed into your function as a list. You will want to pass in just the list item that you are interested in, instead of the list itself.",