1

I am trying to convert a Json string, that i got from a Website, to a python string, but it doesent work.

import json

y = json.loads({"title":null,"icon":null,"iphoneURL":null,"splitURL":null,"splitPercent":null,"expiresAt":null,"expiredURL":null,"clicksLimit":null,"source":"api","integrationGA":null,"integrationFB":null,"integrationAdroll":null,"integrationGTM":null,"id":,"originalURL":"http://instagramm.com","DomainId":,"archived":false,"path":"","cloaking":null,"redirectType":null,"createdAt":"2021-08-03T17:02:20.935Z","OwnerId":,"updatedAt":"2021-08-03T17:02:20.935Z","secureShortURL":"","idString":"lnk_X1P_RGeQY","shortURL":"","duplicate":false})

print(y["originalURL"])

As far as I can tell, I am missing some ' in front and back of the JSON string, but I have no idea, how to add them.

Sorry for my bad English and python Skills

Edit: ive tried adding ' with comma, + and .join ('"""')

Edit2:

import requests

url = "https://api.short.io/links/xxxxxx"
wurl = "youtube.com"

response = requests.request("POST", url, json={"allowDuplicates": False, "domain": "1cr0.short.gy", "originalURL": wurl}, headers={"Accept": "application/json", "Content-Type": "application/json", "Authorization": "xxxxxxxxxxxxxxxxxxxxxx"})

print(response.text)

6 Answers 6

2

You need to make the json string an actual sting. So try putting single quotes around it. Like so:

import json

y = json.loads('{"title":null,"icon":null,"iphoneURL":null,"splitURL":null,"splitPercent":null,"expiresAt":null,"expiredURL":null,"clicksLimit":null,"source":"api","integrationGA":null,"integrationFB":null,"integrationAdroll":null,"integrationGTM":null,"id":793212684,"originalURL":"http://instagramm.com","DomainId":226909,"archived":false,"path":"5n316HkujOzP","cloaking":null,"redirectType":null,"createdAt":"2021-08-03T17:02:20.935Z","OwnerId":221852,"updatedAt":"2021-08-03T17:02:20.935Z","secureShortURL":"https://1cr0.short.gy/5n316HkujOzP","idString":"lnk_X1P_RGeQY","shortURL":"https://1cr0.short.gy/5n316HkujOzP","duplicate":false}')

print(y["originalURL"])
Sign up to request clarification or add additional context in comments.

9 Comments

yes, but I don't know how to add them automatically. That's my problem
I don't understand the question here, sorry. What are you actually receiving? Can you give a more precise example?
I am getting the string without the quotes, as I put it there, now my problem is, to add quotes automatically in front and in the back, but I don't know, how to.
The string itself should not include the quotes. They are just denoting the sequence of characters as a string. So if you already receive a str try passing this to json.loads()
If you look to my code, that is my string, that's how I got him, but I don't know, how to add quotes, and if I pass it directly through, it gives me an error.
|
0

You forgot the quotes around your y variable to make it a string, I recommend an ide so it can point out stuff like this.

y = json.loads('{"title":null,"icon":null,"iphoneURL":null,"splitURL":null,"splitPercent":null,"expiresAt":null,"expiredURL":null,"clicksLimit":null,"source":"api","integrationGA":null,"integrationFB":null,"integrationAdroll":null,"integrationGTM":null,"id":793212684,"originalURL":"http://instagramm.com","DomainId":226909,"archived":false,"path":"5n316HkujOzP","cloaking":null,"redirectType":null,"createdAt":"2021-08-03T17:02:20.935Z","OwnerId":221852,"updatedAt":"2021-08-03T17:02:20.935Z","secureShortURL":"https://1cr0.short.gy/5n316HkujOzP","idString":"lnk_X1P_RGeQY","shortURL":"https://1cr0.short.gy/5n316HkujOzP","duplicate":false}')

3 Comments

yes, but I don't know how to add them. That's my problem
I have already added them you can copy them
but automatically?
0

Please pass your json data in a string.

Try:

import json

y = json.loads('{"title":null,"icon":null,"iphoneURL":null,"splitURL":null,"splitPercent":null,"expiresAt":null,"expiredURL":null,"clicksLimit":null,"source":"api","integrationGA":null,"integrationFB":null,"integrationAdroll":null,"integrationGTM":null,"id":793212684,"originalURL":"http://instagramm.com","DomainId":226909,"archived":false,"path":"5n316HkujOzP","cloaking":null,"redirectType":null,"createdAt":"2021-08-03T17:02:20.935Z","OwnerId":221852,"updatedAt":"2021-08-03T17:02:20.935Z","secureShortURL":"https://1cr0.short.gy/5n316HkujOzP","idString":"lnk_X1P_RGeQY","shortURL":"https://1cr0.short.gy/5n316HkujOzP","duplicate":false}')

print(y["originalURL"])

Output:

http://instagramm.com

2 Comments

I know, but i don't know, how to add them automatically, that's my problem.
There's nothing that can be done automatically, you have to get it done by yourself
0

You have to pass a JSON String to json.loads(). You are just passing the JSON as is (Not a string).

From the Docs:

json.loads(s) - Deserialize s (a str, bytes or bytearray instance containing a JSON document)

import json

y = json.loads('{"title":null,"icon":null,"iphoneURL":null,"splitURL":null,"splitPercent":null,"expiresAt":null,"expiredURL":null,"clicksLimit":null,"source":"api","integrationGA":null,"integrationFB":null,"integrationAdroll":null,"integrationGTM":null,"id":793212684,"originalURL":"http://instagramm.com","DomainId":226909,"archived":false,"path":"5n316HkujOzP","cloaking":null,"redirectType":null,"createdAt":"2021-08-03T17:02:20.935Z","OwnerId":221852,"updatedAt":"2021-08-03T17:02:20.935Z","secureShortURL":"https://1cr0.short.gy/5n316HkujOzP","idString":"lnk_X1P_RGeQY","shortURL":"https://1cr0.short.gy/5n316HkujOzP","duplicate":false}')

print(y["originalURL"])
http://instagramm.com

4 Comments

I know, but i don't know, how to add them automatically, that's my problem.
What do you mean by automatically ? Put your JSON in between single quotes- 'JSON'. Is your JSON stored in any variable ?
yes, but can´t python do that for me? I am getting these strings relatively often, and I don't want to add quotes every single time.
If you have the JSON stored in a variable, you don't need to add any quotes. You can just do json.loads(x) - x is the variable containing JSON. If you want more clarity, I suggest you clearly mention (in your question from where you are getting that JSON. Is it an API/file etc.,) what your problem is so that we can help you out.
0

Try This: import json

y = '''{"title":null,"icon":null,"iphoneURL":null,"splitURL":null,"splitPercent":null,"expiresAt":null,"expiredURL":null,"clicksLimit":null,"source":"api","integrationGA":null,"integrationFB":null,"integrationAdroll":null,"integrationGTM":null,"id":793212684,"originalURL":"http://instagramm.com","DomainId":226909,"archived":false,"path":"5n316HkujOzP","cloaking":null,"redirectType":null,"createdAt":"2021-08-03T17:02:20.935Z","OwnerId":221852,"updatedAt":"2021-08-03T17:02:20.935Z","secureShortURL":"https://1cr0.short.gy/5n316HkujOzP","idString":"lnk_X1P_RGeQY","shortURL":"https://1cr0.short.gy/5n316HkujOzP","duplicate":false}'''

y = json.loads(y)

print(y["originalURL"])

I just converted the json to string before loading

Comments

0

print(response.text)

You can save the return of this code into a variable.

var = str(response.text)
y = json.loads(var)

print(y["originalURL"])

and type cast it into a string before passing it into the variable.

4 Comments

OMG, Thx! that's exactly, what i was looking for, tywm!
Do upvote and mark it as a correct answer!
cant, too les carma
That's actually sad :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.