parsing the uri key from json file is failing - json

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)

Related

beginner help wth python

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)

TypeError: Object of type bytes is not JSON serializable - python 3 - try to post base64 image data

i received this error after try convert data to json to post request
TypeError: Object of type 'bytes' is not JSON serializable
my code
dict_data: dict = {
'img': base64.b64encode(urlopen(obj['recognition_image_path']).read())
}
json_data: str = json.dumps(dict_data)
i read image from url, convert it to base64, after i received error when try convert data to json.
Please help
You need to convert to string first by calling .decode, since you can't JSON-serialize a bytes without knowing its encoding.
(base64.b64encode returns a bytes, not a string.)
import base64
from urllib.request import urlopen
import json
dict_data: dict = {
'img': base64.b64encode(urlopen(obj['recognition_image_path']).read()).decode('utf8')
}
json_data: str = json.dumps(dict_data)
edit: rewrite answer to address actual question, encode/decode
I will do it in a two step process:
First encode the image file into BASE64
Then decode the encoded file
And then transmit back the JSON data using the decoded file.
Here is an example:
Let's say the image file is is_image_file
Encode the image file by:
enc_image_file = base64.b64encode(is_image_file.read())
Next decode it by:
send_image_file = enc_image_file.decode()
Finally transmit the data using send_image_file as JsonResponse to wherever it would be used.
Of course, add import base64 before calling the function.
Note: Using json.dumps(dict_data) one gets a string which will not load the image/s.

TypeError: the JSON object must be str, not 'list'

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.

com.jayway.jsonpath.JsonPath cannot find key

I am trying to use com.jayway.jsonpath.JsonPath to read key/values from a JSON string:
String contentAsString = mvcResult.getResponse().getContentAsString();
System.out.println(contentAsString);
Object value = JsonPath.read(contentAsString, "$.key");
But I get the error:
Expected to find an object with property ['key'] in path $ but found 'net.minidev.json.JSONArray'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
Printing contentAsString gives:
[{"firstName":"N","key":"mykey"}]
Is contentAsString not valid JSON?
The blob that you posted is not a valid JSON Object, however, it IS a valid JSON Array.
To read this using JsonPath, try using this:
Object value = JsonPath.read(contentAsString, "$[0].key");
This will get the value of the key object, from the 0th element of the initial array.

Error parsing JSON file in python 3.4

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')