You are using loads when you need load. json.load is for file-like objects, and json.loads is for strings. (You could also load the string into memory and then parse it with json.load, but you don't want to do that).
You are using loads when you need load. json.load is for file-like objects, and json.loads is for strings. (You could also load the string into memory and then parse it with json.load, but you don't want to do that).
I would like to add that you are not reading the file in the code, which leads to the following (example):
with open('test.txt', 'r') as file:
print(file)
Output:
<_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'>
Use read():
with open('test.txt', 'r') as file:
print(file.read())
Output:
word
TypeError: the JSON object must be str, bytes or bytearray, not coroutine
TypeError: the JSON object must be str, bytes or bytearray, not dict
the JSON object must be string, bytes or bytesarray not dict
python - JSON object must be str, bytes or bytearray, not dict - Stack Overflow
here's the code:
import json
import requests
API_URL = "https://tradestie.com/api/v1/apps/reddit"
class Data:
def __init__(self, no_of_comments, sentiment, sentiment_score, ticker):
self.no_of_comments = no_of_comments
self.sentiment = sentiment
self.sentiment_score = sentiment_score
self.ticker = ticker
@classmethod
def from_json(cls, json_string):
json_dict = json.loads(json_string)
return cls(**json_dict)
def __repr__(self):
return f'<comments {self.no_of_comments}>'
json_string = requests.get(API_URL).json()
data = Data.from_json(json_string)
print(data)I don't understand the error, tried to google it, but still no luck. Isn't there an easy way to just adjust the json object? thx
json.loads take a string as input and returns a dictionary as output.
json.dumps take a dictionary as input and returns a string as output.
With json.loads({"('Hello',)": 6, "('Hi',)": 5}),
You are calling json.loads with a dictionary as input.
You can fix it as follows (though I'm not quite sure what's the point of that):
d1 = {"('Hello',)": 6, "('Hi',)": 5}
s1 = json.dumps(d1)
d2 = json.loads(s1)
import json
data = json.load(open('/Users/laxmanjeergal/Desktop/json.json'))
jtopy=json.dumps(data) #json.dumps take a dictionary as input and returns a string as output.
dict_json=json.loads(jtopy) # json.loads take a string as input and returns a dictionary as output.
print(dict_json["shipments"])
Hello guys, I got some trouble to read .json file.
I first saved texts in the notepad and change the file type as .json, in the windows property, the file shows as json file. Then, I tried to read the file in python:
with open('fred.json','r') as file:
key=json.load(file)
It returns error: JSONDecodeError: Expecting value
I tried another way as using json.loads(file), it returns another error:
TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper
Can anyone help me how to load the file?
Thanks!
json.loads(myResponse.content.decode('utf-8'))
You just put it in the wrong order, innocent mistake.
(In-depth answer). As courteously pointed out by wim, in some rare cases, they could opt for UTF-16 or UTF-32. These cases will be less common as the developers, in that scenario would be consciously deciding to throw away valuable bandwidth. So, if you run into encoding issues, you can change utf-8 to 16, 32, etc.
There are a couple of solutions for this. You could use request's built-in .json() function:
myResponse.json()
Or, you could opt for character detection via chardet. Chardet is a library developed based on a study. The library has one function: detect. Detect can detect most common encodings and then use them to encode your string with.
import chardet
json.loads(myResponse.content.decode(chardet.detect(myResponse.content)["encoding"]))
Let requests decode it for you:
data = response.json()
This will check headers (Content-Type) and response encoding, auto-detecting how to decode the content correctly.
Hello @jestemtym777
Welcome to Microsoft QnA!
based on the error message you provided, the function is being provided with a MiniBatch
The run function in your scoring script is expecting a JSON string
Please try :
- Modify the Scoring Script to Accept MiniBatch: If the input is always going to be a
MiniBatchobject, you can modify the scoring script'srunfunction to handle this type of input directly. - Modify the Input to the Endpoint: Ensure that the input you're providing to the endpoint matches what the
runfunction in your scoring script expects.
I hope this helps!
Kindly mark the answer as Accepted and Upvote in case it helped!
Regards
The batch endpoint expects a json file but for some reason Azure adds a hidden file ".amlignore" to the URI_FOLDER where the minibatches were imported from which azure couldn't process and therefore threw errors - see below content of my input folder:
"minibatch": [".amlignore", "samples.json", "samples1.json"]