I have a database with lots of tweets that I crawled using Twitter API. Those tweets are in a json format what allows me to convert them into dictionaries, right? So here we go: I'm trying to convert these strings into dictionaries using the json package. But everytime I try to run json.loads(string), it gives me an error:
ValueError: Expecting property name: line 1 column 2 (char 1).
Here's an example of the "json strings" I have in my database.
{u"contributors": "None", u"truncated": False, u"text": u"RT #WhoScored: Most clear-cut chances missed at World Cup 2014: M\xfcller / Higua\xedn / Benzema (5), de Vrij / \xd6zil / Ronaldo (4)", u"in_reply_to_status_id": "None", u"id": 487968527395983360, u"favorite_count": 0, u"source": u"Twitter for BlackBerry\xae", u"retweeted": False, u"coordinates": "None", u"entities": {u"user_mentions": [{u"id": 99806132, u"indices": [3, 13], u"id_str": u"99806132", u"screen_name": u"WhoScored", u"name": u"WhoScored.com"}], u"symbols": [], u"trends": [], u"hashtags": [], u"urls": []}, u"in_reply_to_screen_name": "None", u"id_str": u"487968527395983360", u"retweet_count": 0, u"in_reply_to_user_id": "None", u"favorited": False, u"retweeted_status": {u"contributors": "None", u"truncated": False, u"text": u"Most clear-cut chances missed at World Cup 2014: M\xfcller / Higua\xedn / Benzema (5), de Vrij / \xd6zil / Ronaldo (4)", u"in_reply_to_status_id": "None", u"id": 487955847025143808, u"favorite_count": 17, u"source": u"TweetDeck", u"retweeted": False, u"coordinates": "None", u"entities": {u"user_mentions": [], u"symbols": [], u"trends": [], u"hashtags": [], u"urls": []}, u"in_reply_to_screen_name": "None", u"id_str": u"487955847025143808", u"retweet_count": 59, u"in_reply_to_user_id": "None", u"favorited": False, u"user": {u"follow_request_sent": "None", u"profile_use_background_image": True, u"default_profile_image": False, u"id": 99806132, u"verified": True, u"profile_image_url_https": u"https://pbs.twimg.com/profile_images/477005408557486083/9MVR7GdF_normal.jpeg", u"profile_sidebar_fill_color": u"DDEEF6", u"profile_text_color": u"333333", u"followers_count": 425860, u"profile_sidebar_border_color": u"C0DEED", u"id_str": u"99806132", u"profile_background_color": u"272727", u"listed_count": 3245, u"profile_background_image_url_https": u"https://pbs.twimg.com/profile_background_images/439356280/123abc.jpg", u"utc_offset": 3600, u"statuses_count": 24118, u"description": u"The largest detailed football statistics website, covering Europe"s top leagues and more. Follow #WSTipster for betting tips. Powered by Opta data.", u"friends_count": 67, u"location": u"London", u"profile_link_color": u"0084B4", u"profile_image_url": u"http://pbs.twimg.com/profile_images/477005408557486083/9MVR7GdF_normal.jpeg", u"following": "None", u"geo_enabled": False, u"profile_banner_url": u"https://pbs.twimg.com/profile_banners/99806132/1402565693", u"profile_background_image_url": u"http://pbs.twimg.com/profile_background_images/439356280/123abc.jpg", u"name": u"WhoScored.com", u"lang": u"en", u"profile_background_tile": False, u"favourites_count": 250, u"screen_name": u"WhoScored", u"notifications": "None", u"url": u"http://whoscored.com", u"created_at": u"Sun Dec 27 23:22:45 +0000 2009", u"contributors_enabled": False, u"time_zone": u"London", u"protected": False, u"default_profile": False, u"is_translator": False}, u"geo": "None", u"in_reply_to_user_id_str": "None", u"possibly_sensitive": False, u"lang": u"ro", u"created_at": u"Sat Jul 12 13:45:14 +0000 2014", u"filter_level": u"low", u"in_reply_to_status_id_str": "None", u"place": "None"}, u"user": {u"follow_request_sent": "None", u"profile_use_background_image": True, u"default_profile_image": False, u"id": 498676612, u"verified": False, u"profile_image_url_https": u"https://pbs.twimg.com/profile_images/485720258934603776/BmUaZHax_normal.jpeg", u"profile_sidebar_fill_color": u"DDEEF6", u"profile_text_color": u"333333", u"followers_count": 192, u"profile_sidebar_border_color": u"C0DEED", u"id_str": u"498676612", u"profile_background_color": u"C0DEED", u"listed_count": 1, u"profile_background_image_url_https": u"https://pbs.twimg.com/profile_background_images/654833637/l9fp65m0xqzsmoneg8pz.jpeg", u"utc_offset": "None", u"statuses_count": 6468, u"description": u"Garut 10 July | Asda islamic school | Farmasi | #judikajude | path : Dicky Darul Majid", u"friends_count": 153, u"location": u"", u"profile_link_color": u"B30000", u"profile_image_url": u"http://pbs.twimg.com/profile_images/485720258934603776/BmUaZHax_normal.jpeg", u"following": "None", u"geo_enabled": True, u"profile_banner_url": u"https://pbs.twimg.com/profile_banners/498676612/1404927261", u"profile_background_image_url": u"http://pbs.twimg.com/profile_background_images/654833637/l9fp65m0xqzsmoneg8pz.jpeg", u"name": u"DICKY ", u"lang": u"en", u"profile_background_tile": False, u"favourites_count": 74, u"screen_name": u"DickyDarulMajid", u"notifications": "None", u"url": "None", u"created_at": u"Tue Feb 21 09:21:59 +0000 2012", u"contributors_enabled": False, u"time_zone": "None", u"protected": False, u"default_profile": False, u"is_translator": False}, u"geo": "None", u"in_reply_to_user_id_str": "None", u"possibly_sensitive": False, u"lang": u"ro", u"created_at": u"Sat Jul 12 14:35:37 +0000 2014", u"filter_level": u"medium", u"in_reply_to_status_id_str": "None", u"place": "None"}
And here is the code:
import sys, codecs, json
encode = sys.stdin.encoding
all_entries = Tweet.objects.all()[1427:1435]
for entry in all_entries:
tweet = entry.tweet.encode(encode)
json_acceptable_string = tweet.replace("'", "\"")
json_acceptable_string = json_acceptable_string.replace("None", "\"None\"")
data = json.loads(json_acceptable_string)
print data
Traceback:
Traceback (most recent call last):
File "/home/kiko/workspace/SA_WorldCup/main.py", line 6, in <module>
util.tweets_count()
File "/home/kiko/workspace/SA_WorldCup/util/__init__.py", line 25, in tweets_count
data = json.loads(json_acceptable_string, object_hook=JSONObject)
File "/usr/lib/python2.7/json/__init__.py", line 351, in loads
return cls(encoding=encoding, **kw).decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)
I tried many times, but with no succeed. Can you help me out? Thanks a lot.
It looks like your "json" is actually python literal syntax. In that case, it might be easier to ast.literal_eval the string1.
As a side benefit, then you don't have to do any (possibly sketchy) replacements of None with "None". (Consider a tweet which says "None of your base are belong to us")
1although it would probably be even better to make sure that proper json is being dumped into the database to begin with...
use eval()
In [13]: a = "[{'start_city': '1', 'end_city': 'aaa', 'number': 1},\
...: {'start_city': '2', 'end_city': 'bbb', 'number': 1},\
...: {'start_city': '3', 'end_city': 'ccc', 'number': 1}]"
In [14]: eval(a)
Out[14]:
[{'end_city': 'aaa', 'number': 1, 'start_city': '1'},
{'end_city': 'bbb', 'number': 1, 'start_city': '2'},
{'end_city': 'ccc', 'number': 1, 'start_city': '3'}
Related
In my Django project, I am using Celery to asynchronously run my tasks.
The problem I am facing is that the "task_args" column in "django_celery_results_taskresult" is truncated (i.e., text followed by ellipsis).
For example, I am expecting the entire "process_4_items" value in "task_args." However, that is not the case. I can see the same truncated text in the command prompt of the Celery worker in execution (see below "[CELERY WORKER COMMAND PROMPT FOR ONE ITERATION]").
I really am not sure what I need to do to get the entire "process_4_items" value in "task_args."
I am detailing below everything I believe I need to reproduce this item.
Most important, when I debug the project and execute line by line, the entire content of "process_4_items" value appears in "task_args."
[celeryconfig.py]
broker_url = 'amqp://localhost:5672//'
imports = ('operation_hawkeye.tasks',)
result_backend = 'django-db'
accept_content = ['json']
task_always_eager = False
[views.py]
from celery import group
process_4_items = [
{
"service_id": "7f7962b082cb8da55e01f4f013d8fefbc97948ec03400c93423b050d",
"service_id_unique": "c30ed891987f4d8d3debcaef7edb0f582e6b634f1ebed00fb88f0ec7",
"service_type": "buzz_collect_compare",
"service_file_pf_folder": "",
"service_file_nanonsportsproductivity_folder": "",
"service_file_tba_report": "",
"service_file_buzz_collect_compare": "services/buzzcollect_xml_compare/2021/06/01/file_2_name_Update_COOKINGHD_May_2021_Listin_yWoWXo1.xml",
"service_file_type": "XML 1.0 document, ASCII text",
"service_file_name": "file_2_name_Update_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml",
"service_file_size": 396557,
"service_status": "In Queue for Upload",
"service_status_remarks": {
"source_information": {
"source_id": "5191",
"source_name": "10NEWS2",
"file_1_name": "Original_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml",
"file_2_name": "Update_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml"
}
},
"service_creation_date": "2021-06-01T18:18:40.968Z",
"service_owner": "p.premlal#spi-global.com"
},
{
"service_id": "7f7962b082cb8da55e01f4f013d8fefbc97948ec03400c93423b050d",
"service_id_unique": "2edaf2535945992d13b9ee85e908b21cbd520a02642bd7faf7f229bc",
"service_type": "buzz_collect_compare",
"service_file_pf_folder": "",
"service_file_nanonsportsproductivity_folder": "",
"service_file_tba_report": "",
"service_file_buzz_collect_compare": "services/buzzcollect_xml_compare/2021/06/01/file_1_name_Original_COOKINGHD_May_2021_List_P2PlYY7.xml",
"service_file_type": "XML 1.0 document, ASCII text",
"service_file_name": "file_1_name_Original_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml",
"service_file_size": 396409,
"service_status": "In Queue for Upload",
"service_status_remarks": {
"source_information": {
"source_id": "5191",
"source_name": "10NEWS2",
"file_1_name": "Original_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml",
"file_2_name": "Update_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml"
}
},
"service_creation_date": "2021-06-01T18:18:40.935Z",
"service_owner": "p.premlal#spi-global.com"
}
]
jobs_4 = group(tasks.buzz_collect_compare_main.s(process_4_items))
result_4 = jobs_4.apply_async()
[tasks.py]
#app.task(bind=True)
def buzz_collect_compare_main(self, items):
process_items = items
...
[CELERY WORKER COMMAND PROMPT FOR ONE ITERATION]
[2021-06-01 09:37:29,972: DEBUG/ForkPoolWorker-32] (0.010)
UPDATE
"django_celery_results_taskresult"
SET
"task_id" = 'daca83c1-3297-4e64-8a5e-f4b61aef84e2',
"task_name" = 'operation_hawkeye.tasks.buzz_collect_compare_main',
"task_args" = '([{''service_id'': ''dc30fc45948975044cb00c39a4d5a0db8e28bc34f0b4d52fd4dcddef'', ''service_id_unique'': ''2004e35ae5dcc2586ae5fca16ab8a5b2708ffeca233d3bb234feb347'', ''service_type'': ''buzz_collect_compare'', ''service_file_pf_folder'': '''', ''service_file_nanonsportsproductivity_folder'': '''', ''service_file_tba_report'': '''', ''service_file_buzz_collect_compare'': ''services/buzzcollect_xml_compare/2021/06/01/file_2_name_Update_COOKINGHD_May_2021_Listin_726wZgI.xml'', ''service_file_type'': ''XML 1.0 document, ASCII text'', ''service_file_name'': ''file_2_name_Update_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml'', ''service_file_size'': 396557, ''service_status'': ''In Queue for Upload'', ''service_status_remarks'': ''{"source_information": "{\"source_id\": \"27227\", \"source_name\": \"COOKINGHD\", \"file_1_name\": \"Original_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml\", \"file_2_name\": \"Update_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml\"}"}'', ''service_creation_date'': ''2021-06-01T09:36:56.825Z'', ''service_owner'': ''p...'', ...],)',
"task_kwargs" = '{}',
"status" = 'PROGRESS',
"worker" = 'celery#AMD-RYZEN-UBUNTU',
"content_type" = 'application/json',
"content_encoding" = 'utf-8',
"result" = '{"pending": false, "current": 1254, "total": 1254, "percent": 100.0, "description": "Building row 1254 of 1254"}',
"date_created" = '2021-06-01T09:36:58.465884+00:00'::timestamptz,
"date_done" = '2021-06-01T09:37:29.962267+00:00'::timestamptz,
"traceback" = NULL,
"meta" = '{"children": []}'
WHERE
"django_celery_results_taskresult"."id" = 808;
args=('daca83c1-3297-4e64-8a5e-f4b61aef84e2', 'operation_hawkeye.tasks.buzz_collect_compare_main', '([{\'service_id\': \'dc30fc45948975044cb00c39a4d5a0db8e28bc34f0b4d52fd4dcddef\', \'service_id_unique\': >\'2004e35ae5dcc2586ae5fca16ab8a5b2708ffeca233d3bb234feb347\', \'service_type\': \'buzz_collect_compare\', \'service_file_pf_folder\': \'\', \'service_file_nanonsportsproductivity_folder\': \'\', \'service_file_tba_report\': \'\', \'service_file_buzz_collect_compare\': \'services/buzzcollect_xml_compare/2021/06/01/file_2_name_Update_COOKINGHD_May_2021_Listin_726wZgI.xml\', \'service_file_type\': \'XML 1.0 document, ASCII text\', \'service_file_name\': \'file_2_name_Update_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml\', \'service_file_size\': 396557, \'service_status\': \'In Queue for Upload\', \'service_status_remarks\': \'{"source_information": "{\\"source_id\\": \\"27227\\", \\"source_name\\": \\"COOKINGHD\\", \\"file_1_name\\": \\"Original_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml\\", \\"file_2_name\\": \\"Update_COOKINGHD_May 2021 Listings(4-26-21 thru 5-30-21).xml\\"}"}\', \'service_creation_date\': \'2021-06-01T09:36:56.825Z\', \'service_owner\': \'p...\', ...],)', '{}', 'PROGRESS', 'celery#AMD-RYZEN-UBUNTU', 'application/json', 'utf-8', '{"pending": false, "current": 1254, "total": 1254, "percent": 100.0, "description": "Building row 1254 of 1254"}', datetime.datetime(2021, 6, 1, 9, 36, 58, 465884, tzinfo=<UTC>), datetime.datetime(2021, 6, 1, 9, 37, 29, 962267, tzinfo=<UTC>), '{"children": []}', 808)
[$ celery -A hawkeye report]
software -> celery:4.4.7 (cliffs) kombu:4.6.11 py:3.8.6
billiard:3.6.3.0 py-amqp:2.6.1
platform -> system:Linux arch:64bit, ELF
kernel version:5.8.0-53-generic imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:amqp results:django-db
accept_content: ['json']
broker_url: 'amqp://guest:********#localhost:5672//'
imports: ('operation_hawkeye.tasks',)
result_backend: 'django-db'
task_always_eager: False
[UPDATE]
Solved this issue by defining the argsrepr_maxsize value in celery.py. While I had known about this solution earlier, I did not realize that argsrepr_maxsize was within celery.ampq.
[celery.py]
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hawkeye.settings')
os.environ.setdefault('FORKED_BY_MULTIPROCESSING', '1')
app = Celery('hawkeye')
default_config = 'hawkeye.celeryconfig'
app.config_from_object(default_config)
app.amqp.argsrepr_maxsize = 10485760
New to development and python and trying to create a json file.
I'm trying to generate a list of people with a start time and onboarding time and incident time:
import datetime
import random
import math
import json
import numpy as np
# generate list of candidates
cand = input("Enter number of candidates: ")
cand = int(cand)
startRange = 0.06 # standard deviation (scale)
startTime = 9.30 #mean (loc)
onboardRange = 0.5
onboardTime = 4.5
incidenceRatio = [0,0,0,0,1] #1 in 5 chance an incident arises
def calCan(): # creates a dictionary of candidates with start times, onboarding, incident times.
candattr = []
candidates = {"candidates":candattr}
a = 1
while a <= cand:
b = np.random.normal(loc=startTime, scale=startRange, size=(1, 1)) # startime
c = np.random.normal(loc=onboardTime, scale=onboardRange, size=(1, 1)) # onboarding time
d = random.choice(incidenceRatio) * np.random.normal(loc=onboardTime, scale=onboardRange, size=(1, 1)) # incident time - random.choice(incidenceRatio) - takes a random selection from incidenceRation list defined above.
btime = datetime.time(int(b//1),int(math.ceil(100 * (59 * (b % 1))/100)), 0)#converts bnum into time
ctime = datetime.time(0,int(c//1),int(math.ceil(100 * (59 * (c % 1))/100)))
dtime = datetime.time(0,int(d//1) ,int(math.ceil(100 * (59 * (d % 1))/100)))
candattr.append({
"name": "candidate {}".format(a),
"startTime": "{}".format(btime),#json doesn't recognize time so have to store as a string
"onboardTime": "{}".format(ctime),
"incidentTime": "{}".format(dtime)
})
a = a + 1
return candidates
And then I'm trying to put this into a Json.
I can get the JSON to print just fine using
candidateList = calCan()
print(json.dumps(candidateList, indent=4))
it returns something like this:
{
"candidates": [
{
"name": "candidate 1",
"startTime": "09:17:00",
"onboardTime": "00:03:37",
"incidentTime": "00:04:04"
},
{
"name": "candidate 2",
"startTime": "09:16:00",
"onboardTime": "00:04:19",
"incidentTime": "00:00:00"
},
{
"name": "candidate 3",
"startTime": "09:15:00",
"onboardTime": "00:04:13",
"incidentTime": "00:03:25"
}
]
}
but when i try to manipulate it using json.loads with
candidateList = calCan()
candidateList = str(candidateList)
data = json.loads((candidateList)
I get all sorts of horrible errors that I can't make heads nor tail of:
Traceback (most recent call last):
File "queue.py", line 56, in <module>
data = json.loads(candidateList)
File "C:\Users\Gordon Rose\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\Gordon Rose\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Gordon Rose\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
If I try and add double quotes to (candidateList):
data = json.loads("candidateList")
It still doesn't get me anywhere
Traceback (most recent call last):
File "queue.py", line 56, in <module>
data = json.loads(("candidateList"))
File "C:\Users\Gordon Rose\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\Gordon Rose\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Gordon Rose\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Following some advice below - i have stopped converting candidateList into a string and am using json.load instead.
candidateList = calCan()
data = json.load(candidateList)
this returns:
Traceback (most recent call last):
File "queue.py", line 56, in <module>
data = json.load(candidateList)
File "C:\Users\Gordon Rose\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
AttributeError: 'dict' object has no attribute 'read'
In a properly formatted JSON, strings need to be double-quoted but in a string literal for a dictionary, string keys and values are given as single-quoted strings. This is the key difference between json.dumps(candidateList) and str(candidateList) (where candidateList = calCan()).
The first is
'{"candidates": [{"name": "candidate 1", "startTime": "09:18:00", "onboardTime": "00:04:10", "incidentTime": "00:00:00"}, {"name": "candidate 2", "startTime": "09:18:00", "onboardTime": "00:04:23", "incidentTime": "00:00:00"}, {"name": "candidate 3", "startTime": "09:19:00", "onboardTime": "00:05:08", "incidentTime": "00:00:00"}, {"name": "candidate 4", "startTime": "09:14:00", "onboardTime": "00:05:17", "incidentTime": "00:00:00"}, {"name": "candidate 5", "startTime": "09:12:00", "onboardTime": "00:04:38", "incidentTime": "00:00:00"}]}'
and the second is
"{'candidates': [{'name': 'candidate 1', 'startTime': '09:18:00', 'onboardTime': '00:04:10', 'incidentTime': '00:00:00'}, {'name': 'candidate 2', 'startTime': '09:18:00', 'onboardTime': '00:04:23', 'incidentTime': '00:00:00'}, {'name': 'candidate 3', 'startTime': '09:19:00', 'onboardTime': '00:05:08', 'incidentTime': '00:00:00'}, {'name': 'candidate 4', 'startTime': '09:14:00', 'onboardTime': '00:05:17', 'incidentTime': '00:00:00'}, {'name': 'candidate 5', 'startTime': '09:12:00', 'onboardTime': '00:04:38', 'incidentTime': '00:00:00'}]}"
both
data = json.loads(json.dumps(candidateList))
as well as
data = json.dumps(str(candidateList).replace("'",'"'))
work as expected.
I have this string:
{\u0022allow_group_shows\u0022: true, \u0022needs_supporter_to_pm\u0022: true, \u0022ads_zone_ids\u0022: {\u0022300x250,centre\u0022: \u0022\u0022, \u0022300x250,right\u0022: \u0022\u0022, \u0022300x250,left\u0022: \u0022\u0022, \u0022468x60\u0022: \u0022\u0022, \u0022160x600,top\u0022: \u0022\u0022, \u0022160x600,bottom\u0022: \u0022\u0022, \u0022160x600,middle\u0022: \u0022\u0022}, \u0022chat_settings\u0022: {\u0022sort_users_key\u0022: \u0022a\u0022, \u0022silence_broadcasters\u0022: \u0022false\u0022, \u0022highest_token_color\u0022: \u0022darkpurple\u0022, \u0022emoticon_autocomplete_delay\u0022: \u00220\u0022, \u0022ignored_users\u0022: \u0022\u0022, \u0022show_emoticons\u0022: true, \u0022font_size\u0022: \u00229pt\u0022, \u0022b_tip_vol\u0022: \u002210\u0022, \u0022allowed_chat\u0022: \u0022all\u0022, \u0022room_leave_for\u0022: \u0022org\u0022, \u0022font_color\u0022: \u0022#494949\u0022, \u0022font_family\u0022: \u0022default\u0022, \u0022room_entry_for\u0022: \u0022org\u0022, \u0022v_tip_vol\u0022: \u002280\u0022}, \u0022is_age_verified\u0022: true, \u0022flash_host\u0022: \u0022edge143.stream.highwebmedia.com\u0022, \u0022tips_in_past_24_hours\u0022: 0, \u0022dismissible_messages\u0022: [], \u0022show_mobile_site_banner_link\u0022: false, \u0022last_vote_in_past_90_days_down\u0022: false, \u0022server_name\u0022: \u0022113\u0022, \u0022num_users_required_for_group\u0022: 2, \u0022group_show_price\u0022: 18, \u0022is_mobile\u0022: false, \u0022chat_username\u0022: \u0022__anonymous__eiwBXR\u0022, \u0022recommender_hmac\u0022: \u0022dae28e4e9afa15da7c6227af2e8fb8abd85a3714aca8f86f01a53a6dd1377115\u0022, \u0022broadcaster_gender\u0022: \u0022couple\u0022, \u0022hls_source\u0022: \u0022https://localhost/live\u002Dhls/amlst:jimmy_and_amy\u002Dsd\u002De73f4b67186a2ec4c13137607d02470ac61f32b60ac15e691bf33493423ef477_trns_h264/playlist.m3u8\u0022, \u0022allow_show_recordings\u0022: true, \u0022is_moderator\u0022: false, \u0022room_status\u0022: \u0022public\u0022, \u0022edge_auth\u0022: \u0022{\u005C\u0022username\u005C\u0022:\u005C\u0022__anonymous__eiwBXR\u005C\u0022,\u005C\u0022org\u005C\u0022:\u005C\u0022A\u005C\u0022,\u005C\u0022expire\u005C\u0022:1590669977,\u005C\u0022sig\u005C\u0022:\u005C\u0022454d96141c66fb42f74e9620b9d79e937de3a774a5687021f8650cc4f563d371\u005C\u0022,\u005C\u0022room\u005C\u0022:\u005C\u0022jimmy_and_amy\u005C\u0022}\u0022, \u0022is_supporter\u0022: false, \u0022chat_password\u0022: \u0022{\u005C\u0022username\u005C\u0022:\u005C\u0022__anonymous__eiwBXR\u005C\u0022,\u005C\u0022org\u005C\u0022:\u005C\u0022A\u005C\u0022,\u005C\u0022expire\u005C\u0022:1590669977,\u005C\u0022sig\u005C\u0022:\u005C\u0022454d96141c66fb42f74e9620b9d79e937de3a774a5687021f8650cc4f563d371\u005C\u0022,\u005C\u0022room\u005C\u0022:\u005C\u0022jimmy_and_amy\u005C\u0022}\u0022, \u0022room_pass\u0022: \u0022b5b2408cd91e6c595a3f732a5b7b1567b566bcc92f384ce5e6a00a26a24fb5c7\u0022, \u0022low_satisfaction_score\u0022: false, \u0022tfa_enabled\u0022: false, \u0022room_title\u0022: \u0022(STEP SIS CUM FACE) shh... Luna is here and dont know what we are doing #hairy #creampie #stockings #new #lush [3536 tokens remaining]\u0022, \u0022satisfaction_score\u0022: {\u0022down_votes\u0022: 15, \u0022up_votes\u0022: 67, \u0022percent\u0022: 82, \u0022max\u0022: 31222657}, \u0022viewer_username\u0022: \u0022AnonymousUser\u0022, \u0022hidden_message\u0022: \u0022\u0022, \u0022following\u0022: false, \u0022wschat_host\u0022: \u0022https://chatws\u002D45.stream.highwebmedia.com/ws\u0022, \u0022has_studio\u0022: false, \u0022num_followed\u0022: 0, \u0022spy_private_show_price\u0022: 30, \u0022hide_satisfaction_score\u0022: false, \u0022broadcaster_username\u0022: \u0022jimmy_and_amy\u0022, \u0022ignored_emoticons\u0022: [], \u0022apps_running\u0022: \u0022[[\u005C\u0022Tip Goal\u005C\u0022,\u005C\u0022\u005C\u005C/apps\u005C\u005C/app_details\u005C\u005C/tip\u002Dgoal\u005C\u005C/?slot\u003D0\u005C\u0022],[\u005C\u0022Ultra Bot \u002D 4Sci\u005C\u0022,\u005C\u0022\u005C\u005C/apps\u005C\u005C/app_details\u005C\u005C/ultra\u002Dbot\u002D4sci\u005C\u005C/?slot\u003D2\u005C\u0022],[\u005C\u0022Roll The Dice\u005C\u0022,\u005C\u0022\u005C\u005C/apps\u005C\u005C/app_details\u005C\u005C/roll\u002Dthe\u002Ddice\u002D5\u005C\u005C/?slot\u003D3\u005C\u0022]]\u0022, \u0022token_balance\u0022: 0, \u0022private_min_minutes\u0022: 10, \u0022viewer_gender\u0022: \u0022m\u0022, \u0022allow_anonymous_tipping\u0022: false, \u0022num_users_waiting_for_group\u0022: 0, \u0022last_vote_in_past_24_hours\u0022: null, \u0022is_widescreen\u0022: true, \u0022num_viewers\u0022: 1672, \u0022broadcaster_on_new_chat\u0022: false, \u0022private_show_price\u0022: 30, \u0022num_followed_online\u0022: 0, \u0022allow_private_shows\u0022: true}
and I want to decode it in python to view to make it easier for me to send it over the internet into our android app... anyways that why I am here it should look like
"{"allow_group_shows": true, "needs_supporter_to_pm": true, "ads_zone_ids": {"300x250,centre": "", "300x250,right": "", "300x250,left": "", "468x60": "", "160x600,top": "", "160x600,bottom": "", "160x600,middle": ""}, "chat_settings": {"sort_users_key": "a", "silence_broadcasters": "false", "highest_token_color": "darkpurple", "emoticon_autocomplete_delay": "0", "ignored_users": "", "show_emoticons": true, "font_size": "9pt", "b_tip_vol": "10", "allowed_chat": "all", "room_leave_for": "org", "font_color": "#494949", "font_family": "default", "room_entry_for": "org", "v_tip_vol": "80"}, "is_age_verified": true, "flash_host": "edge306.stream.highwebmedia.com", "tips_in_past_24_hours": 0, "dismissible_messages": [], "show_mobile_site_banner_link": false, "last_vote_in_past_90_days_down": false, "server_name": "115", "num_users_required_for_group": 2, "group_show_price": 18, "is_mobile": false, "chat_username": "bom4b5", "recommender_hmac": "ed05e292bb82262255a96944d81bb04dc2d248ca69fff35cf5d7015889c005b1", "broadcaster_gender": "couple", "hls_source": "https://***/live-edge/****-sd-e73f4b67186a2ec4c13137607d02470ac61f32b60***%22%7D", "allow_show_recordings": true, "is_moderator": false, "room_status": "public", "edge_auth": "{\"username\":\"bom4b5\",\"org\":\"A\",\"expire\":1590666696,\"sig\":\"49b6844fde2c47c2430bd05946b6cfbc9c7864788b9236d7f5af5ff88efd3f95\",\"room\":\"jimmy_and_amy\"}", "is_supporter": false, "chat_password": "****", "room_pass": "b5b2408cd91e6c595a3f732a5b7b1567b566bcc92f384ce5e6a00a26a24fb5c7", "low_satisfaction_score": false, "tfa_enabled": false, "room_title": "(STEP SIS CUM FACE) shh... Luna is here and dont know what we are doing #hairy #creampie #stockings #new #lush [4310 tokens remaining]", "satisfaction_score": {"down_votes": 15, "up_votes": 67, "percent": 82, "max": 31222657}, "viewer_username": "bom4b5", "hidden_message": "", "following": false, "wschat_host": "https://chatws-45.stream.highwebmedia.com/ws", "has_studio": false, "num_followed": 0, "spy_private_show_price": 30, "hide_satisfaction_score": false, "broadcaster_username": "jimmy_and_amy", "ignored_emoticons": [], "apps_running": "[[\"Tip Goal\",\"\\/apps\\/app_details\\/tip-goal\\/?slot=0\"],[\"Ultra Bot - 4Sci\",\"\\/apps\\/app_details\\/ultra-bot-4sci\\/?slot=2\"],[\"Roll The Dice\",\"\\/apps\\/app_details\\/roll-the-dice-5\\/?slot=3\"]]", "token_balance": 0, "private_min_minutes": 10, "viewer_gender": "f", "allow_anonymous_tipping": false, "num_users_waiting_for_group": 0, "last_vote_in_past_24_hours": null, "is_widescreen": true, "num_viewers": 331, "broadcaster_on_new_chat": false, "private_show_price": 30, "num_followed_online": 0, "allow_private_shows": true}"
I tried How to decode javascript unicode string in python?
it worked but the only one issue is that I cant decode it after I split
gett = s.get("https://localhost.com/34534535/")
# print(gett.text + "\n\n\n\n")
m = json.dumps({"k": gett.text}) # decodes it
# split
testt = (json.loads(m)["k"]).split('window.initialRoomDossier = "')
testt = testt[1].split('";')
final = testt[0] # show the same
the thing is that if I load it from a string like
a = "{\u0022allow_group_shows\u0022: true, \u0022nee..."
it does work but not after I split
Here is how I solved it
import js2py
#split
splitt = (json.loads(m)["k"]).split('window.initialRoomDossier = "')
splitt = splitt[1].split('";')
final = str(splitt[0])
final = 'var kek = "'+final+'";'#Turning it into javascript code (Later we will run it)
#Javascript Virtual Machine
context = js2py.EvalJs({'python_sum': sum})
#Slides the sexy code
context.execute(final)#Running the javascript code
#print (context.kek)#
m3u8_url = json.loads(context.kek)["hls_source"]
solved_places = {
'a1': False,'a2': False,'a3': False,'a4': False,'a5': False,
'b1': False,'b2': False,'b3': False,'b4': False,'b5': False,
'c1': False,'c2': False,'c3': False,'c4': False,'c5': False,
'd1': False,'d2': False,'d3': False,'d4': False,'d5': False,
'e1': False,'e2': False,'e3': False,'e4': False,'e5': False
}
Getting error pointer on the second line, (b1) asking for an indentation.
(ERROR: Expected an indented Block)
Whats wrong? (no Spaces, all tabs, 1 tab = 4 Spaces...)
I've tried different Levels of indentation, Spaces, commas etc...) like this:
solved_places =
{'a1': False,'a2': False,'a3': False,'a4': False,'a5': False,
'b1': False,'b2': False,'b3': False,'b4': False,'b5': False,
'c1': False,'c2': False,'c3': False,'c4': False,'c5': False,
'd1': False,'d2': False,'d3': False,'d4': False,'d5': False,
'e1': False,'e2': False,'e3': False,'e4': False,'e5': False}
This question already has answers here:
Single vs double quotes in JSON
(12 answers)
Closed 4 years ago.
I'm acctually having trouble with an json string an python. I've written a script that use find-my-iphone python module this module give me this string at the output
{u'locationType': u'', u'altitude': 0.0, u'locationFinished': True, u'longitude': 7.340714223689717, u'positionType': u'GPS', u'floorLevel': 0, u'timeStamp': 1497518502892L, u'latitude': 47.81268700030429, u'isOld': False, u'isInaccurate': False, u'verticalAccuracy': 0.0, u'horizontalAccuracy': 50.0}
After a bit of prosesing with:
loc = api.devices[deviceID].location()
locstr = str(loc).replace("u'",'"').replace("'",'"') #.replace("}","")
I obtain a string that look like this:
{"locationType": "", "altitude": 0.0, "locationFinished": False, "longitude": 7.340450948111099, "positionType": "GPS", "floorLevel": 0, "timeStamp": 1497518436368L, "latitude": 47.81275740829093, "isOld": False, "isInaccurate": False, "verticalAccuracy": 0.0, "horizontalAccuracy": 100.0}
There is my code:
`
from pyicloud import PyiCloudService
from geopy.distance import vincenty
import json
import sys
api = PyiCloudService('*****.*****#free.fr','******')
deviceID = u"Qo+Jyvct3IIl7N3MXrz6LfDvm8qjDCHjkedOvse1mhzWf1sikvSFQOHYVNSUzmWV" # Needed
deviceNAME = "<AppleDevice(iPhone 5s: David Smartphone)>" # Just an help
api.devices[deviceID].location()
api.devices[deviceID].status()
loc = api.devices[deviceID].location()
locstr = str(loc).replace("u'",'"').replace("'",'"') #.replace("}","")
But when I try to use
json.loads(locstr)
Python give me :
Traceback (most recent call last): File "distancePAPA.py", line 19,
in
t = json.loads(locstr) File "/usr/lib/python2.7/json/init.py", line 339, in loads
return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded
I don't really know what I did wrong so I'am asking fo help
PS1: I just really need gps coordinate
PS2: I'm french so sorry for mistake.
import json
>>> loc = api.devices[deviceID].location()
>>> locstr = json.dumps(loc)
'{"horizontalAccuracy": 50.0, "floorLevel": 0, "isOld": false, "isInaccurate": false, "verticalAccuracy": 0.0, "timeStamp": 1497518502892, "altitude": 0.0, "locationFinished": true, "longitude": 7.340714223689717, "positionType": "GPS", "locationType": "", "latitude": 47.81268700030429}'
>>> json.loads(locstr)
{u'timeStamp': 1497518502892, u'altitude': 0.0, u'locationFinished': True, u'longitude': 7.340714223689717, u'horizontalAccuracy': 50.0, u'floorLevel': 0, u'locationType': u'', u'latitude': 47.81268700030429, u'isOld': False, u'isInaccurate': False, u'verticalAccuracy': 0.0, u'positionType': u'GPS'}
Hi use the module ast,
import ast
loc_str = ast.literal_eval(str(loc))
Json does not support False and the postfix L in 1497518436368L; To make it regular, we need to convert it again
s = locstr.replace('False', 'false').replace('L,', ',')
And now, we could use json.loads(s)