0

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.

    myfile.close()

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": [
                    "P248"
                ]
            }
        ],
        "type": "statement"
    }
]

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

3
  • Possible duplicate of Error while trying to load a JSON object with python 3 Commented Sep 24, 2019 at 16:26
  • What does resp_obj look like when the function is called? Commented Sep 24, 2019 at 16:32
  • Oh, a note, when using with you don't need to explicitly close the file handle - with handles that automatically when execution leaves the scope of the with. Commented Sep 25, 2019 at 9:54

2 Answers 2

1

Your problem is not in writing to the csv file, it's in decoding the json data in the first place.

using your json data as per this question as a string, and passing it into the json.loads() function:

>>> import json
>>> my_json = json.loads(json_str)
>>>

(no error)

However, if we pass that within a list:

>>> my_json = json.loads([json_str])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'list'
>>>

We get the same exception that you get.

Check 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, was very useful I solved like this: resp = wbgetentities_q(ids=item_id) resp_obj_str = (resp.json()['entities'][item_id]['claims']['P1566']) listString = json.dumps(resp_obj_str) my_json = json.loads(listString)
1

The TypeError is telling you what the problem is. You are trying to pass a list to a function when it expects bytes or a string. It's hard to say which because you didn't include the part of your error message with that information, but here's my best guess based on the structure of your data:

my_json = json.loads(resp_obj)
with open("wiki.txt", "a") as myfile:
    writer = csv.writer(myfile)
    for item in my_json:
        writer.writerow([item["mainsnak"]["datavalue"], "https://www.geonames.org/{}".format(item["mainsnak"]["datavalue"]["value"])])
myfile.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.