You should pass the file contents (i.e. a string) to json.loads(), not the file object itself. Try this:

with open(file_path) as f:
    data = json.loads(f.read())
    print(data[0]['text'])

There's also the json.load() function which accepts a file object and does the f.read() part for you under the hood.

Answer from Eugene Yarmash on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › tough time parsing json files, i receive an iowrapper object rather than string.
r/learnpython on Reddit: Tough time parsing JSON files, I receive an IOWrapper object rather than string.
December 28, 2017 - Nothing else in the code gives me an issue, as in, I successfully fetch the data I request, and then successfully write it onto a json file. The slew of issues arises when trying to process the JSON file. I get different errors as I change my approach, such as trying to use the read() method of the TextIOWrapper, etc.
Problem parsing Json file Aug 8, 2019
r/Python
6y ago
Using Python to Parse a JSON Object Mar 6, 2026
r/PythonLearning
4mo ago
How to turn JSON into objects in python? Sep 2, 2021
r/learnpython
4y ago
JSON Serialization Mar 15, 2024
r/Python
2y ago
How do i handle large json file? Nov 30, 2021
r/learnpython
4y ago
More results from reddit.com
Discussions

Error reading JSON from text IO stream under Python 3
I've been getting errors under Python 3.7 when trying to read JSON-LD from a steam opened in text mode. The error takes this form: File ".../rdflib_jsonld/util.py", line 28, in source... More on github.com
🌐 github.com
1
August 24, 2018
python - TypeError: Object of type TextIOWrapper is not JSON serializable I don't know how to handle this - Stack Overflow
I want to parse this dictionary to a json file. I saw lots of solutions on google and on this forum but none of them worked. import json standardcategories = { "unnecessary&quo... More on stackoverflow.com
🌐 stackoverflow.com
can't read json file with python. getting type error: json object is 'TextIOWrapper' - Stack Overflow
I'm trying to read from a json file. This is how I created the file: import requests import json import time from pprint import pprint BASE_URL = "https://www.wikiart.org/en/api/2/UpdatedArtists" More on stackoverflow.com
🌐 stackoverflow.com
August 8, 2018
python - Reading JSON object: "TypeError: '_io.TextIOWrapper' object is not subscriptable" - Stack Overflow
I have a JSON file that has articles I obtained by using Webhose.io news article API. I cannot execute the API protocol again so I need to parse the individual articles in the JSON document: a samp... More on stackoverflow.com
🌐 stackoverflow.com
March 27, 2020
🌐
Researchdatapod
researchdatapod.com › home › how to solve python typeerror: the json object must be str, bytes or bytearray, not ‘textiowrapper’
How to Solve Python TypeError: the JSON object must be str, bytes or bytearray, not 'TextIOWrapper' - The Research Scientist Pod
September 18, 2024 - TypeError: the JSON object must be str, bytes or bytearray, not ‘TextIOWrapper’ ... TypeError occurs in Python when you perform an illegal operation for a specific data type. TextIOWrapper is the file object returned when the open() function opens a file. The _io.TextIOWrapper class provides ...
🌐
GitHub
github.com › RDFLib › rdflib-jsonld › issues › 55
Error reading JSON from text IO stream under Python 3 · Issue #55 · RDFLib/rdflib-jsonld
August 24, 2018 - def source_to_json(source): # TODO: conneg for JSON (fix support in rdflib's URLInputSource!) source = create_input_source(source, format='json-ld') stream = source.getByteStream() try: if PY3: if isinstance(stream, TextIOBase): use_stream = stream else: use_stream = TextIOWrapper(stream, encoding='utf-8') return json.load(use_stream) # json_data = stream.read() # if isinstance(json_data, bytes): # json_data = json_data.decode('utf-8') # return json.loads(json_data) # return json.load(StringIO(json_data)) # return json.load(StringIO(stream.read().decode('utf-8'))) else: return json.load(stream) finally: stream.close()
Author   RDFLib
🌐
ProgramCreek
programcreek.com › python › example › 5779 › io.TextIOWrapper
Python Examples of io.TextIOWrapper
def request_list( self, identity: Identity ) -> Iterator[Sequence[Mapping[str, object]]]: team = self.team if not (isinstance(team, identity.team_type) and cast(str, identity.identifier).startswith(team.server_url)): return start = 0 while True: response = self.request( identity, 'GET', self.LIST_URL.format(self.team, start) ) assert response.code == 200 payload = json.load(io.TextIOWrapper(response, encoding='utf-8')) response.close() yield from payload['values'] if payload['isLastPage']: break start = payload['nextPageStart']
Find elsewhere
🌐
LinuxTut
linuxtut.com › en › 105ef1b4ad843eb0e095
Parse a JSON string written to a file in Python
March 11, 2020 - I checked how to parse the JSON string written to the file with Python, so make a note. ... JSON parsing can be done by using ** json.load () ** for the data acquired by ʻopen ()`. import json raw = open('data.json', 'r') type(raw) # <class '_io.TextIOWrapper'> parsed = json.load(raw) type(parsed) ...
🌐
Reddit
reddit.com › r/learnpython › typeerror: '_io.textiowrapper'
r/learnpython on Reddit: TypeError: '_io.TextIOWrapper'
November 28, 2022 -

I'm supposed to create a dictionary from the values in a file and search for 'Polly' and remove it if it's in the file,

I kept getting errors so I'm just trying to get it to print the value just so I can see what I'm doing wrong and even at the most simple level I keep getting errors,

I'm in my first semester so this is all still new to me

employees = {}
employees = open('dictionary_values.txt', 'r')
print(employees['Polly'])

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(employees['Polly'])
TypeError: '_io.TextIOWrapper' object is not subscriptable

🌐
Python Morsels
pythonmorsels.com › TextIOWrapper
TextIOWrapper‽ converting files to strings in Python - Python Morsels
February 5, 2024 - Ever encountered a TextIOWrapper object in Python when you really wanted a string? Converting an _io.TextIOWrapper object to a string is fortunately pretty easy: call the read method!
🌐
NetworkLessons.com
forum.networklessons.com › lessons discussion
Python JSON - Lessons Discussion - NetworkLessons.com Community Forum
April 20, 2020 - This lesson explains how to work with the JSON data format in Python. We'll see how to convert JSON strings to dictionaries, vice versa, and more · How can I select the “routers” label · When you want to “select” a certain field in a JSON (or dictionary), it’s best to use an IDE ...
🌐
GitHub
github.com › Julian › jsonschema › issues › 384
AttributeError: '_io.TextIOWrapper' object has no attribute 'get' · Issue #384 · python-jsonschema/jsonschema
December 24, 2017 - json schema error AttributeError: '_io.TextIOWrapper' object has no attribute 'get' My files { "sender" : 6 "receiver" : 4, "transaction_type" : "same", "transaction_amount" : "10", "transaction_status" : "pending", "transaction_receiver...
Author   python-jsonschema
🌐
Reddit
reddit.com › r/learnprogramming › python help. how to write _io.textiowrapper variable type to text file? or convert it to a string?
r/learnprogramming on Reddit: Python Help. How to write _io.TextIoWrapper variable type to text file? Or convert it to a string?
August 20, 2018 -

Hello!

I am grabbing a some data off of an API. I then am trying to take the variable in which I have stored the gathered data and write it to a text file.

When I define the type my variable is it shows

<class '_io.TextIOWrapper'>

I have tried to figure out how to convert it to a string (as that what the error yells at me for) but I cannot find the function.

import time, json, requests

def btstamp():
    bitStampTick = requests.get('https://www.bitstamp.net/api/ticker/')
    return bitStampTick.json()['last']

bitstampprice = (btstamp())

print (bitstampprice)	

bitstampprice = open("testwrite1.txt", "w+")

print (type(bitstampprice))

bitstampprice.write(bitstampprice)

bitstampprice.close()

Here is the error

C:\TradingBot>export.py
6456.95
<class '_io.TextIOWrapper'>
Traceback (most recent call last):
  File "C:\TradingBot\export.py", line 15, in <module>
    bitstampprice.write(bitstampprice)
TypeError: write() argument must be str, not _io.TextIOWrapper

C:\TradingBot>

I have spent a good amount of time doing this and its getting late. Any help would be greatly appreciated.

I go into some info about _io.TextIOWrapper and the info is a bit advance for me.

Why is my data being returned in this type?

How come it is not a integer as it is shown when I "print" the variable?

Thanks

Aluad

🌐
Python
docs.python.org › 3.10 › library › io.html
io — Core tools for working with streams — Python 3.10.20 documentation
TextIOWrapper, which extends TextIOBase, is a buffered text interface to a buffered raw stream (BufferedIOBase). Finally, StringIO is an in-memory stream for text. Argument names are not part of the specification, and only the arguments of open() are intended to be used as keyword arguments.
🌐
Javadoc.io
javadoc.io › static › org.python › jython-standalone › 2.5.2 › org › python › core › io › TextIOWrapper.html
TextIOWrapper (Jython API documentation)
This differs from py3k TextIOWrapper, ... with Python 2.x's text mode) as well as universal mode. ... Contruct a TextIOWrapper wrapping the given BufferedIOBase. ... Read and return up to size bytes, contained in a String. Returns an empty String on EOF ... Read until EOF. ... Read until size, newline or EOF. Returns an empty string if EOF is hit immediately. ... Write the given String to the IO ...