-3

jsonCallback1530150433250_46028 && jsonCallback1530150433250_46028({"context":"synthesis%3Ddisabled%26q%3D%2523all%2Bcard_content_lang%253Aen%2B%2B%2B%2Bcard_content_type%253A%2528%2522career%2522%2529%2B%26b%3D0%26s%3Ddesc%2528score_with_card_update_timestamp%2529%26output_format%3Djson%26callback%3

  • The above is a part example of a json that i want to parse from the full link below.

  • The main problem is I cannot extract it from the web as it is invalid. i was previously using json.loads() but the structure changed. How do i get the whole json data into string with a different library or anything else for editing?

  • Thanks for the help

Here is the link for full json: https://apisearch.3ds.com/card_search_api?q=%23all%20card_content_lang%3Aen%20%20%20%20card_content_type%3A(%22career%22)%20&s=desc(score_with_card_update_timestamp)&b=0&hf=10&output_format=json&callback=jsonCallback1530150433250_46028

I can't post an image for now, but here is an anology:

link = 'https://apisearch.3ds.com/card_search_api?q=%23all%20card_content_lang%3Aen%20%20%20%20card_content_type%3A(%22career%22)%20&s=desc(score_with_card_update_timestamp)&b=0&hf=10&output_format=json&callback=jsonCallback1530150433250_46028'   
response = requests.get(link)       
time.sleep(random.randint(3,5) 
json_obj = json.loads(response.text)
print json_obj

Which gives me, *ValueError: No JSON object could be decoded*

3

2 Answers 2

0

I am not going to copy your entire JSON data here but here is the gist.

x = "jsonCallback1530150433250_46028 && jsonCallback1530150433250_46028({the_json_data_you_want}"

I am going to split at the first "(" because you want everything after it:

a = x.split('(', 1)
reqJson = a[1]
print(reqJson)

This will give you the required JSON. Now you can do:

import json 
json_data = json.loads(reqJson) 

And then proceed with whatever you want to achieve.

PS: Please edit your question in a readable format so that others can help you as well, if you need further help.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for reaching out this quick! I totally understand the concept now thank you, but just at the part of "a = x.split('(', 1)". How do i load x into python as it is invalid. I've tried different library to parse invalid json but I'm not sure how to get it into a string format as you have shown.
As for now, I'll be copying the whole data manually into my code. Unless, there would be a easier way to load them directly from the link.
This link should help you in loading a JSON file and reading it as a string, and then you can go about with my solution.
Oh my god, that link really helps. I never knew json.dump could help produce a valid json string. Thank you so much. That answers it all!
0

what you may have to do is truncate the matching string until you find a '{' to do this:

# split the json string with '{' as separator:
va1='jhdgfhsgdf&&sdkfhskjdfh({sdfhsj({})dfhsj})'
v1=va1.split('{')

# now delete the first element of the array
del v1[0]

# now join rest of the elements of the array to get proper json string
'{'.join(v1)

1 Comment

Thanks for your speedy reply. This fully works, the problem is how do i get the json data from the link above and load it into a string like you have. Any libraries or methods? Thanks once again

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.