Python Executing Json with backslash - python

I have json string, which i need to execute in selenium script using python, but i think /" making it difficult to parse
var t = {
"s": {
"2MqcJqtmNYDZlcbDZY3yjA==": "[{\"id\":\"global_mute\",\"expiration\":0}]",
"BrowserId": "\"TAMGVxD/wr9fL47xJZAciw==\"",
"SecretBundle": "{\"key\":\"2Eiw3rJZI9oErblie5mx+xXPjd+tj6t0hqQHC+EQ8Bc=\",\"encKey\":\"aGLPEm0dyq6W/gHN5cVONhdOR6lv2CGm4v4GLJ7LyoM=\",\"macKey\":\"2Eiw3rJZI9oErblie5mx+xXPjd+tj6t0hqQHC+EQ8Bc=\"}",
"Token1": "\"OG7aZ02ndonc1DGnfJ3bswG3na3Wr5OB7bwdgiwdd4Y=\"",
"Token2": "\"1#Sf53vRMCbVuLW0Q1diEg95TVBWRMDOjG7mqI8XIfOjr+hMiTwyqjKM72NKgxZJBGtfkETDmqfCgVLQ==\"",
"Xz+yNzfxx8EbdaUFWVxmjg==": "false",
"debugCursor": "191",
"ghI8QhVRLDvFWJDpV7Cx5g==": "false",
"logout-token": "\"1#MZ9F8QbLN9iGKNeZGbCJdzKwPHq7L5sJrpO6RtiG35rdu5TVLu/+z3gGNmbYG59h1Grzfwcnz44uLVencqRdP044ZpWuT02+ltcEM+9aCURZQkBG+oRyeWNAHcInXMWCpWysPF5d/ISLnbfeV1NVoA==\"",
"remember-me": "true",
"storage_test": "storage_test"
},
"c": ""
}
i used r'var t = whole json string', but its not working.
Do i need to covert backslash first ?

Related

Python json How to add data

I don't speak English well
So the question may be a bit odd
{
"arg1": {
"1": "1",
"2": "2"
},
"arg2": {
"1": "1",
"2": "2"
},
"arg3": {
"1": "1",
"2": "2"
}
}
I want to store data this way. What should I do?
json_data = {arg3: {"1": "1", "2": "2"}}
with open(f'./Json/test.json', 'w', encoding='utf-8') as make_file:
json.dump(json_data, make_file, ensure_ascii=False ,indent="\t")
Is this right?
I would appreciate it if you let me know.
I don't know what to do by deleting the original content.
Your code works fine. The only problem I see when your running it is that that arg3 needs to be written as "arg3" in double quotes (single quotes are invalid in json) unless you have a value for it defined previously.
json_data = {"arg3": {"1": "1", "2": "2"}}
Make that change, and you should be able to load and properly display your JSON with:
with open(f'output.json') as f:
a = json.load(f)
print(json.dumps(a, indent="\t"))
If you do the json.dumps() you get a properly formatted json which you can then call print on to display.

How to get rid of backslashes in nested json

I make a request to a backend API and get the data back in json format
The response looks something like this. Kindly note that the body key values will be different and there are over 100's of them. The data1.json looks like this
[
{
"body": "[{\"task_ids\":[],\"accounts\":[],\"entity_ids\":[12814],\"guid\":\"2DFEB337-5F5D-4DF5-84CF-E951D237D448\",\"id\":\"0034030fb97251b3\",\"subject\":\"Uploaded Application\"}]",
code": 200,
"headers": {
"Content-Type": "application/json"
},
"msg": "OK",
"name": "0"
},
{
"body": "[{\"task_ids\":[],\"accounts\":[],\"entity_ids\":[12814],\"guid\":\"2DFEB337-5F5D-4DF5-84CF-E951D237D448\",\"id\":\"0034030fb97251b3\",\"subject\":\"Uploaded Application\",\}]",
code": 200,
"headers": {
"Content-Type": "application/json"
},
"msg": "OK",
"name": "0"
},
...
]
I need to get rid of the
"\" in all of the body key in the json response
Concatenate the key[body'] into one array
ideally it should look something like this.
[
{"body":"[{"task_ids":[],"accounts":[],"entity_ids":[12814],"guid":"2DFEB337-5F5D-4DF5-84CF-E951D237D448","id":"0034030fb97251b3","subject":"Uploaded Application",]","[{"task_ids":[],"accounts":[],"entity_ids":[12814],"guid":"2DFEB337-5F5D-4DF5-84CF-E951D237D448","id":"0034030fb97251b3","subject":"Uploaded Application",]",..}
]
I have tried replace and a lot of methods but none of them are replacing the \ so I cannot even go to step 2. What I did find out was if I save it to a text file the backslashes are replaced but then I cannot again send the response back as a json object. The code to get the data1.json file so far looks like this.
data = json.loads(r.text)
with open('data1.json', 'w') as outfile:
json.dump(data, outfile, sort_keys = True, indent = 4,
ensure_ascii = False)
Any suggestions on how to achieve the first points as in my desired output? Thanks.
(For starters, what you gave is invalid JSON, and json will either fail to parse it completely or produce something bogus. You need to make sure you extract the response correctly, and if this is really what the response is, have the sending party fix it.)
Now, about the question as asked:
You don't need to do anything special. That's just how JSON represents values that themselves contain JSON's special characters ("escapes" them with a backslash).
If you load the data via a proper JSON parser (e.g. json.loads()), it will undo that escaping, and in e.g. data[0]['body'], you will see proper data.
Of course, since that string is JSON itself, you will need to further parse it with json, too, if you need to split it into its meaningful parts...
The JSON data is incorrectly formatted and invalid JSON (missing quotes in "key" strings (e.g. code": 200,), invalid syntax in second dictionary body object, as mentioned in comment (e.g. "Uploaded Application\",\}]")).
However, after fixing these, a simple str.replace() statement can be used to get the expected JSON format. Then, simply parse the JSON content and build the desired list:
import json
data = '''[
{
"body": "[{\"task_ids\":[],\"accounts\":[],\"entity_ids\":[12814],\"guid\":\"2DFEB337-5F5D-4DF5-84CF-E951D237D448\",\"id\":\"0034030fb97251b3\",\"subject\":\"Uploaded Application\"}]",
"code": 200,
"headers": {
"Content-Type": "application/json"
},
"msg": "OK",
"name": "0"
},
{
"body": "[{\"task_ids\":[],\"accounts\":[],\"entity_ids\":[12814],\"guid\":\"2DFEB337-5F5D-4DF5-84CF-E951D237D448\",\"id\":\"0034030fb97251b3\",\"subject\":\"Uploaded Application\"}]",
"code": 200,
"headers": {
"Content-Type": "application/json"
},
"msg": "OK",
"name": "0"
}
]'''
r = json.loads(data.replace('\\', '').replace('"[', "[").replace("]\"", "]"))
l = []
for d in r:
l.append(d)
Now inspect the contents of l:
>>> l
[{u'body': [{u'entity_ids': [12814], u'accounts': [], u'task_ids': [], u'guid': u'2DFEB337-5F5D-4DF5-84CF-E951D237D448', u'id': u'0034030fb97251b3', u'subject': u'Uploaded Application'}], u'headers': {u'Content-Type': u'application/json'}, u'code': 200, u'name': u'0', u'msg': u'OK'},
{u'body': [{u'entity_ids': [12814], u'accounts': [], u'task_ids': [], u'guid': u'2DFEB337-5F5D-4DF5-84CF-E951D237D448', u'id': u'0034030fb97251b3', u'subject': u'Uploaded Application'}], u'headers': {u'Content-Type': u'application/json'}, u'code': 200, u'name': u'0', u'msg': u'OK'}]

Converting JSON file into a suitable string issue with Python

I have a JSON file as follows :
{
"desired":{
"property1":{
"port":"/dev/usbserial",
"rx":{
"watchdoginterval":3600
},
"state":{
"path":"/Users/user1"
},
"enabled":"true",
"active":{
"enabled":"true"
}
},
"property2":{
"signal_interrupt":"USR2",
"signal_description_path":"/tmp/logger.log"
},
"property3":{
"periodmins":40
},
}
}
I am having issues trying to convert this into a string for use with AWS IoT. The function I am using is deviceShadowHandler.shadowUpdate(JSONPayload, customShadowCallback_Update, 5)
Where JSONPayload should be the JSON string.
I have tried :
with open('JSONfile.json' , 'r') as f:
dict = json.load(f)
JSONPayload = str(dict)
but I receive an "Invalid JSON file error".
An attempt to manually create a literal string from the jSON file gets messy with complaints about "EOL while scanning string literal" etc.
What is the best solution to solve this? I am new to JSON and stuff and Python.
Trailing commas are not allowed in JSON.
{
"desired":{
"property1":{
"port":"/dev/usbserial",
"rx":{
"watchdoginterval":3600
},
"state":{
"path":"/Users/user1"
},
"enabled":"true",
"active":{
"enabled":"true"
}
},
"property2":{
"signal_interrupt":"USR2",
"signal_description_path":"/tmp/logger.log"
},
"property3":{
"periodmins":40
} # <- no comma there
}
}

TypeError: string indices must be integers // working with JSON as dict in python

Okay, so I've been banging my head on this for the last 2 days, with no real progress. I am a beginner with python and coding in general, but this is the first issue I haven't been able to solve myself.
So I have this long file with JSON formatting with about 7000 entries from the youtubeapi.
right now I want to have a short script to print certain info ('videoId') for a certain dictionary key (refered to as 'key'):
My script:
import json
f = open ('path file.txt', 'r')
s = f.read()
trailers = json.loads(s)
print(trailers['key']['Items']['id']['videoId'])
# print(trailers['key']['videoId'] gives same response
Error:
print(trailers['key']['Items']['id']['videoId'])
TypeError: string indices must be integers
It does work when I want to print all the information for the dictionary key:
This script works
import json
f = open ('path file.txt', 'r')
s = f.read()
trailers = json.loads(s)
print(trailers['key'])
Also print(type(trailers)) results in class 'dict', as it's supposed to.
My JSON File is formatted like this and is from the youtube API, youtube#searchListResponse.
{
"kind": "youtube#searchListResponse",
"etag": "",
"nextPageToken": "",
"regionCode": "",
"pageInfo": {
"totalResults": 1000000,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "",
"id": {
"kind": "youtube#video",
"videoId": ""
},
"snippet": {
"publishedAt": "",
"channelId": "",
"title": "",
"description": "",
"thumbnails": {
"default": {
"url": "",
"width": 120,
"height": 90
},
"medium": {
"url": "",
"width": 320,
"height": 180
},
"high": {
"url": "",
"width": 480,
"height": 360
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
}
]
}
What other information is needed to be given for you to understand the problem?
The following code gives me all the videoId's from the provided sample data (which is no id's at all in fact):
import json
with open('sampledata', 'r') as datafile:
data = json.loads(datafile.read())
print([item['id']['videoId'] for item in data['items']])
Perhaps you can try this with more data.
Hope this helps.
I didn't really look into the youtube api but looking at the code and the sample you gave it seems you missed out a [0]. Looking at the structure of json there's a list in key items.
import json
f = open ('json1.json', 'r')
s = f.read()
trailers = json.loads(s)
print(trailers['items'][0]['id']['videoId'])
I've not used json before at all. But it's basically imported in the form of dicts with more dicts, lists etc. Where applicable. At least from my understanding.
So when you do type(trailers) you get type dict. Then you do dict with trailers['key']. If you do type of that, it should also be a dict, if things work correctly. Working through the items in each dict should in the end find your error.
Pythons error says you are trying find the index/indices of a string, which only accepts integers, while you are trying to use a dict. So you need to find out why you are getting a string and not dict when using each argument.
Edit to add an example. If your dict contains a string on key 'item', then you get a string in return, not a new dict which you further can get a dict from. item in the json for example, seem to be a list, with dicts in it. Not a dict itself.

python loop with JSON, rather then manually print out each match

I have successfully written a script to pull information from a json file and parse this in a way in which I will I can use but I at the moment I have to manually print each string. I would like to loop this if possible but stuck on where to start?
Python
from __future__ import print_function
import json
import sys
from pprint import pprint
with open('screen.json') as data_file:
data = json.load(data_file)
#json_file.close()
# Print all json data
#pprint(data)
#screen_list = data['screen']
print ("Screens availble",len (data['screen']))
#pprint(data["screen"][1]["id"])
#pprint(data["screen"][1]["user"])
#pprint(data["screen"][1]["password"])
#pprint(data["screen"][1]["code"])
#How to loop this
print ("https://",data["screen"][0]["server"],"/test/test.php?=",data["screen"][0]["code"],sep='')
print ("https://",data["screen"][1]["server"],"/test/test.php?=",data["screen"][1]["code"],sep='')
print ("https://",data["screen"][2]["server"],"/test/test.php?=",data["screen"][2]["code"],sep='')
JSON
{
"screen": [
{
"id": "1",
"user": "user1#example.com",
"password": "letmein",
"code": "123456",
"server": "example.com"
},
{
"id": "2",
"user": "user2#example.com",
"password": "letmein",
"code": "123455",
"server": "example.com"
},
{
"id": "3",
"user": "user3#example.com",
"password": "letmein",
"code": "223456",
"server": "example.com"
}
]
}
You've already pulled data['screen'] out into a variable named screen_list. That variable is a list, so you can use it the same as any other list—call len on it, index it, or loop over it. So:
screen_list = data['screen']
print("Screens availble", len(screen_list))
for screen in screen_list:
pprint(screen['id'])
pprint(screen['user'])
pprint(screen['password'])
pprint(screen['code'])
And, down below, just loop again:
for screen in screen_list:
print("https://", screen["server"], "/test/test.php?=", screen["code"], sep='')
(I'm assuming you want to print out all the screens' info, then print out all the URLs. If you want to print each URL at the same time you print the info, just merge them into one loop.)
As a side note, this is a good time to learn about string formatting. If you want to use those URLs to, e.g., pass them to urllib2 or requests, you can't just print them out, you have to make them into strings. Even if you do just want to print them out, formatting is usually easier to read and harder to get wrong. So:
print("https://{}/test/test.php?={}".format(screen["server"], screen["code"]))
… or …
print("https://{server}/test/test.php?={code}".format(**screen))
You can iterate over the list of dicts:
for d in data["screen"]:
print ("https://",d["server"],"/test/test.php?=",d["code"],sep='')
Out:
https://example.com/test/test.php?=123456
https://example.com/test/test.php?=123455
https://example.com/test/test.php?=223456

Categories