57

I have json file named "panamaleaks50k.json". I want to get ['text'] field from the json file but it shows me following error

the JSON object must be str, bytes or bytearray, not 'TextIOWrapper'

this is my code

with open('C:/Users/bilal butt/Desktop/PanamalEakJson.json','r') as lst:
    b = json.loads(lst)
    print(b['text'])

my json file look

[
{
   "fullname": "Mohammad Fayyaz",
   "id": "885800668862263296",
   "likes": "0",
   "replies": "0",
   "retweets": "0",
   "text": "Love of NS has been shown in PanamaLeaks scandal verified by JIT...",
   "timestamp": "2017-07-14T09:58:31",
   "url": "/mohammadfayyaz/status/885800668862263296",
   "user": "mohammadfayyaz"
 },
{
  "fullname": "TeamPakistanPTI \u00ae",
  "id": "885800910357749761",
  "likes": "0",
  "replies": "0",
  "retweets": "0",
  "text": "RT ArsalanISF: #PanamaLeaks is just a start. U won't believe whr...",
  "timestamp": "2017-07-14T09:59:29",
  "url": "/PtiTeampakistan/status/885800910357749761",
  "user": "PtiTeampakistan"
 }
]

how I can read all ['text'] and just single ['text'] field?

Andrew T.
  • 70
  • 1
  • 6
Bilal Butt
  • 1,202
  • 3
  • 12
  • 15

2 Answers2

138

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.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
23

Use json.load(), not json.loads(), if your input is a file-like object (such as a TextIOWrapper).

Given the following complete reproducer:

import json, tempfile
with tempfile.NamedTemporaryFile() as f:
    f.write(b'{"text": "success"}'); f.flush()
    with open(f.name,'r') as lst:
        b = json.load(lst)
        print(b['text'])

...the output is success.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Flagged community-wiki to avoid harvesting rep from a known dupe. Folks should feel free to edit/amend, though the linked answer should be considered more canonical. – Charles Duffy Dec 17 '17 at 16:51
  • 1
    i got this error by using **load()** funciton. ` the JSON object must be str, bytes or bytearray, not 'TextIOWrapper'` – Bilal Butt Dec 17 '17 at 16:53
  • @BilalButt, `load()` does not generate that error -- unlike `loads()`, which the code in the question uses, `load()` accepts file-like objects as input. If you assert that it does, edit your question to add a [mcve] -- the shortest possible code someone else can run, without modifications, to see the problem themselves. (Right now the indentation is wrong, and nobody but you will have a `C:\Users\bilal butt` directory, so this isn't code other people can invoke). – Charles Duffy Dec 17 '17 at 16:56
  • thanks for your quick rply .. i edit json file in my Question. – Bilal Butt Dec 17 '17 at 17:15
  • The contents of the file aren't relevant -- the file could just contain `[]` and the error would be exactly the same. Thus, a *minimal* reproducer has no need to include anything more than `[]` as the file contents being parsed (but *should* actually create a file with those contents, if it can't be reproduced without one). – Charles Duffy Dec 17 '17 at 19:01
  • @BilalButt, ...see the edit I made to your question, such that the code can be copied-and-pasted to produce the problem *even if the file doesn't exist beforehand*, and the edit made to this answer, to let the answer be tested in the same way (with a copy-and-paste-style test). – Charles Duffy Dec 17 '17 at 19:10