-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*

Han Jinn
  • 41
  • 8
  • 1
    Please edit your question for readability. And try describing the question properly. Maybe provide an example about what you are trying to do. – rajkris Jul 03 '18 at 10:00
  • http://idownvotedbecau.se/itsnotworking/ http://idownvotedbecau.se/noattempt/ http://idownvotedbecau.se/nocode/ http://idownvotedbecau.se/nodebugging/ http://idownvotedbecau.se/noresearch/ – Alfe Jul 03 '18 at 10:01
  • I'm really sorry, I'm new to this page. I've edited it to be a little clearer now. Thanks for the feedback. – Han Jinn Jul 04 '18 at 01:12

2 Answers2

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.

Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73
  • 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. – Han Jinn Jul 04 '18 at 01:13
  • 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. – Han Jinn Jul 04 '18 at 02:21
  • 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. – Ankur Sinha Jul 04 '18 at 06:26
  • https://stackoverflow.com/questions/34600003/converting-json-to-string-in-python – Ankur Sinha Jul 04 '18 at 06:26
  • 1
    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! – Han Jinn Jul 04 '18 at 08:43
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)
sandeep
  • 21
  • 5
  • 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 – Han Jinn Jul 04 '18 at 02:06