I am trying my first steps with python and json and hope you can halp me with this.
I am getting a json in a variable which looks like this:
host10=[{'hostid': '10084', 'proxy_hostid': '0'},
{'hostid': '10085', 'proxy_hostid': '1'}]
when I run
hosts = json.loads(host10)
print(hosts)
I am getting the error:
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not list
what do I wrong?
you can find your answer here
import json
json.dumps(list_name)
Related
Hi everyone I am trying to parse the uri key from a json file - it is properly loading the JSON file, but when I try to parse uri its failing with:
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper
with open(RROOT) as data_file:
data = json.loads(data_file)
for key, value in data.items():
if key["uri"] in data:
print(value)
What am I doing wrong here? Thank you
Firstly, use json.load(takes file object) instead of json.loads(takes json string).
Secondly, key["uri"] will error out. If you're trying to just get the value for the key "uri" then do:
with open(RROOT) as data_file:
data = json.load(data_file)
for value in data['uri']:
print(value)
Hi i want to make JSON readable by using loads and dumps, but i encountered this error
"TypeError: the JSON object must be str, not 'list'"
Here is my code:
parsedCoin = json.loads(coin)
print(json.dumps(parsedCoin, indent=4, sort_keys=True))
how i can solve this problem?
json.loads expects a string. json.dumps(coin) will give you a string version of coin.
So I have some json that looks like this, which I got after taking it out of some other json by doing response.body.to_json:
{\n \"access_token\": \"<some_access_token>\",\n \"token_type\": \"Bearer\",\n \"expires_in\": 3600,\n \"id_token\": \<some_token>\"\n}\n"
I want to pull out the access_token, so I do
to_return = {token: responseJson[:access_token]}
but this gives me a
TypeError: no implicit conversion of Symbol into Integer
Why? How do I get my access token out? Why are there random backslashes everywhere?
to_json doesn't parse JSON - it does the complete opposite: it turns a ruby object into a string containing the JSON representation of that object is.
It's not clear from your question what response.body is. It could be a string, or depending on your http library it might have already been parsed for you.
If the latter then
response.body["access_token"]
Will be your token, if the former then try
JSON.parse(response.body)["access_token"]
Use with double quotes when calling access_token. Like below:
to_return = {token: responseJson["access_token"]}
Or backslashes are escaped delimiters and make sure you first parse JSON.
I am trying to load a Json file from a url and parse it on Python3.4 but i get a few errors and I've no idea what they are pointing to. I did verify the json file on the url from jsonlint.com and the file seems fine. The data.read() is returning 'byte' file and i've type casted it. The code is
import urllib.request
import json
inp = input("enter url :")
if len(inp)<1: inp ='http://python-data.dr-chuck.net/comments_42.json'
data=urllib.request.urlopen(inp)
data_str = str(data.read())
print(type(data_str))
parse_data = json.loads(data_str)
print(type(parse_data))
The error that i'm getting is:
The expression str(data.read()) doesn't "cast" your bytes into a string, it just produces a string representation of them. This can be seen if you print data_str: it's a str beginning with b'.
To actually decode the JSON, you need to do data_str = data.read().decode('utf=8')
I am following the simple piece of code from the documentation
http://hdfscli.readthedocs.org/en/latest/quickstart.html
with client.read(path, encoding='utf-8') as reader:
print reader
from json import load
model = load(reader)
the path is valid. i get
<requests.packages.urllib3.response.HTTPResponse object at 0x0000000003148048>
newchars, decodedbytes = self.decode(data, self.errors)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: invalid start byte
the first line is the result of print reader. Why am i getting this error? is there any other way to load json object from hdfs? I know that the object is JSON as thats how i had put it in. Is there a way to ignore the error? Why doesn't the encoding work?