I've some difficulties to parse a unicode JSON string.
sample:
js = "{'to': 1234, 'message': u'sample message', 'user': 65773722, 'msgId': 28198}"
I want to iterate the JSON object to retrieve the values.
I've already tried, json.dumps, json.loads and js.decode('unicode-escape'),
but I keep getting error messages.
Please help..I'm stuck !
Many thanks !
Unfortunately someone goofed, and that's not JSON.
>>> ast.literal_eval(js)
{'to': 1234, 'message': u'sample message', 'user': 65773722, 'msgId': 28198}
it is not valid json
s = "{'to': 1234, 'message': u'sample message', 'user': 65773722, 'msgId': 28198}"
valid = s.replace("u'", "'")
supervalid = v.replace("'", '"')
json.loads(super_valid)
Related
I am new to Python and I can not seem to find a solution that I understand.
• When I load in the JSON file I can print out the list as a whole; but if I try to access the key I get KeyError.
• Am I right in saying that Python converts the JSON file into a Python dictionary?
• Also, how would I go about accessing keys and then checking against them?
Main Code:
JSON File:
Console Error:
Welcome to Stackoverflow Community.
JSON you have:
{
"credentials": [
{
"username": "Admin",
"password": "Password"
}
]
}
When Converted to the dictionary:
{'credentials': [{'username': 'Admin', 'password': 'Password'}]}
Understanding the output of credentials:
print(data["credentials"])
# Output:
[{'username': 'Admin', 'password': 'Password'}]
# Do observe that the output is in a list format.
# For better understanding let's assume there is more than 1 credential:
[
{'username': 'Admin', 'password': 'Password'}, # 0 of list
{'username': 'Admin1', 'password': 'Password1'} # 1 of list
]
Understanding the mistake:
# Instead of
data["username"]
# do
data["credentials"][0]["username"]
The Right way:
for i in len(data["credentials"]):
print("The username is ", data["credentials"][i]["username"])
Also, I encourage you to go through Python Tutorials before working on something.
Python is widely considered among the easiest programming languages for beginners to learn.
I am trying to convert the following (json) string into a python data type:
data = "{'id': 26, 'photo': '/media/f082b5af-ad0.png', 'first_name': 'Islam', 'last_name': 'Mansour', 'email': 'islammansour06+8#gmail.com', 'city': 'Giza', 'cv': '/media/fbb61609-442.pdf', 'reference': 'Facebook', 'campaign': OrderedDict([('id', 2), ('name', 'javascript')]), 'status': 'Invitation Sent', 'user': None, 'at': '2020-01-20', 'time': '23:02:58.359179', 'technologies': [OrderedDict([('id', 46), ('name', 'Django'), ('category', OrderedDict([('id', 24), ('name', 'Framework'), ('_type', 'skill')]))])]}"
I am trying to convert it to JSON by using
json.loads(data.replace("\'", "\""))
but I am having the following error
json.decoder.JSONDecoderError: Expecting value: line 1 column 219 (char 218)
The issue is that your data is not valid json.
The main problem starts here: [OrderedDict([('id', 46), ('name', 'Django'), ('category', OrderedDict([('id', 24), ('name', 'Framework'), ('_type', 'skill')]))])]}. This looks like it is a string representaion of some python objects.
Below is a more friendly representation of your json data.
I have marked the problematic parts (with **) (basically everywhere there is a OrderedDict).
{
"id":26,
"photo":"/media/f082b5af-ad0.png",
"first_name":"Islam",
"last_name":"Mansour",
"email":"islammansour06+8#gmail.com",
"city":"Giza",
"cv":"/media/fbb61609-442.pdf",
"reference":"Facebook",
"campaign":**OrderedDict**([("id",
2), ("name", "javascript")]), "status":"Invitation Sent",
"user":None,
"at":"2020-01-20",
"time":"23:02:58.359179",
"technologies":[
**OrderedDict**([("id",
46),
("name",
"Django")
]("category", OrderedDict([("id", 24), ("name", "Framework"), ("_type", "skill")]))])]
}```
You could try making use of an [online json parser][1] which might give you some friendlier output.
[1]: http://json.parser.online.fr/
As previously said, OrderedDict is not correct JSON. But this is correct python.
To fix it:
from collections import OrderedDict # direct import because this is as this in your string
import json
jsonCorrect = json.dumps(eval(data))
json.loads(jsonCorrect) # it works
Not sure why you are adding the replace call. Should work with just the following:
json.loads(data)
You can read about it here.
I am trying to start cheating data from twitter using twitter module and python. Here's is my code
import twitter
import win_unicode_console
win_unicode_console.enable()
CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxx'
OAUTH_TOKEN = 'xxxxxxxxxxxxxxxxx'
OAUTH_TOKEN_SECRET = 'xxxxxxxxxxxx'
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
print(twitter_api)
WORLD_WOE_ID = 1
US_WOE_ID = 23424977
world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
us_trends = twitter_api.trends.place(_id=US_WOE_ID)
print(us_trends)
print(world_trends)
I was getting encoding error . so i used
print((us_trends).encode('utf-8'))
which resulted in
AttributeError: 'TwitterListResponse' object has no attribute 'encode'
so i decided to use win_unicode_console module
But whats confusing is that us_trends is returning value.
[{'trends': [{'name': 'El Chapo', 'url': 'http://twitter.com/search?q=%22El+Chapo%22', 'promoted_content': None, 'query': '%22El+Chapo%22', 'tweet_volume': 103536}, {'name': 'Antonio Brown', 'url': 'http://twitter.com/search?q=%22Antonio+Brown%22', 'promoted_
but the statement
print(world_trends)
gives below error
File "C:\Users\nawendu\Desktop\TWIT.PY", line 25, in <module>
print(world_trends)
File
line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 24-
29: character maps to <undefined>
How can the encoding work in us trends and not in world trends??
encode is a method of a string.
You have a json object, it doesn't have this method.
When you print an object it needs to convert the object to a string representation for your output encoding (probably windows encoding here). If there are characters in there (e.g. Emoji) that are not in the output encoding then you get an error.
Encodings are a difficult topic (and a pain point in Python), but you'll need to learn about them if you want to print output.
I have a python script that pulls all of the EC2 instance ids and tags in all of the AWS accounts I own. I am trying to parse for only one value of one key. Specifically I only want to parse the Value of the Key email from the response, but I am getting the error: list indices must be integers or slices, not str. Below is my code and the json response.
Code:
import boto3
import json
conn = boto3.resource('ec2',
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
aws_session_token=session_token)
instances = conn.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
for instance in instances:
host_list = instance.id
host_tags = instance.tags
print(host_tags['Key']['email']['Value'])
Sample JSON:
[{
'Key': 'gitlab',
'Value': 'true'
}, {
'Key': 'portfolio',
'Value': 'xxx'
}, {
'Key': 'runner-manager-name',
'Value': 'xxxxxx'
}, ...
]
Error:
list indices must be integers or slices, not str
Your problem is with the lines:
host_tags = instance.tags
print(host_tags['Key']['email']['Value'])
Rewrite it like this:
host_tags = instance.tags
for tag in host_tags:
print('Key: ' + tag['Key'] + ' Value: ' + tag['Value'])
instance.tags is an array of dict. You need to process each item (tag) in the array. Then you need to process the dict extracting its key / value pairs.
I am new to Azure Cognitive Services. I am using python 3.5 and the Azure Face service, specifically the "Face - Find Similar" API. I am getting an error formatting the Post JSON body. The documentation says that I should either provide a single faceId or a list of faces in a face list, e.g., 'extended_family'. In my case, I want to use the face list. I do not know how to format the faceID parameter in the JSON so that call processes the face list. I have tried different alternatives to setting faceId to '{}' or 'Null', '', 'False', and this results in errors.
Below is my JSON body for my POST:
{'faceId': '97522b8b-02b6-4115-99e0-6dc1f5d45f51', 'faceIds': ['97522b8b- 02b6-4115-99e0-6dc1f5d45f51', '0ca8f3e4-edf1-4c14-b926-3b47eae7e29c', '2fadbb12-b10b-4761-aaaa-c50f1dc765c3', '56f464d5-b388-4fc7-9051-6991cf5f1d0d', '29931480-632e-40b6-aa0c-9e03e36e95f9', '7a8085b2-2013-4742-a51a-a5543a0347e8'], 'faceListId': 'extended_family', 'maxNumOfCandidatesReturned': 20, 'mode': 'matchPerson'}
Since faceId is populated, I get:
error: 'code': 'BadArgument', 'message': 'Argument faceListId and faceIds cannot be provided at the same time.'
If I leave faceId blank, such as:
{'faceId': '', 'faceIds': ['824e3d83-a94f-4ef2-949e-55a55b2ef256', '51f3c1a5-4e16-4b14-89fa-f1342a2c46ec', '0480d2e0-ff05-44de-b3d8-94408277b1c5', 'c7d767fb-0fbe-46c8-b7af-2c8f675bfd8d', 'ca7e82a7-cd3f-417b-bffa-77c9d47c1439', 'f7130e90-9e1f-428a-a773-93c87932a420'], 'faceListId': 'extended_family', 'maxNumOfCandidatesReturned': 20, 'mode': 'matchPerson'}
I get the following:
{'error': {'code': 'BadArgument', 'message': 'Request body is invalid.'}}
If I remove the faceId term from the JSON:
{'faceIds': ['690feffd-5c86-47d7-ac3c-224b0eafa90f', '936564e0-31aa-43e3-916e-c7b236bea8e0', '614c04cb-4375-44c8-b393-89d64b4c1ebd', 'a29f8e5c-50ba-4bb8-8bf8-98e356a9125a', '073d7865-2aaf-4806-9bef-ddca478137ea', '7e416e83-5973-4aa1-b1fc-3a25b5174bb3'], 'faceListId': 'andersen_extended_family', 'maxNumOfCandidatesReturned': 20, 'mode': 'matchPerson'}
{'error': {'code': 'BadArgument', 'message': 'Face ID is invalid.'}}
My code is as follows:
facelist = list() #This is populated upstream using the Face Detect API
facelistid = 'extended_family'
faceid = ''
payload = {'faceId': faceid, 'faceIds':facelist,'faceListId':facelistid, 'maxNumOfCandidatesReturned':20,'mode': "matchPerson"}
req = requests.post(serviceurlpersongroup, data = json.dumps(payload) , headers = {'Ocp-Apim-Subscription-Key': key})
jinfo = req.json()
Just as the error message explained, faceListId and faceIds (not faceId) should not be provided at the same time. So the payload you use should be
payload = {'faceId': faceid, 'faceListId': facelistid, 'maxNumOfCandidatesReturned': 20, 'mode': "matchPerson"}
or
payload = {'faceId': faceid, 'faceIds': facelist, 'maxNumOfCandidatesReturned': 20, 'mode': "matchPerson"}
Feel free to update if you have any further questions.