Parsing JSON from web API

Get help using Construct 2

Post » Sat May 28, 2016 6:07 pm

My name is Anderson Palma, I'm from OneDollarBoard project on indiegogo and we've created some python aplication to monitor our compain and notify our about new backers (it really keeps the team motivated)
Everything is open source, so, everyone can use this too
Indiegogo Backer Alert https://github.com/PalmaAnderson/Indiegogo-Backer-Alert

as expected, this is not a game, but an Application that monitor some web API that send it's data in a JSON

E.g: website is
https://api.indiegogo.com/private_api/c ... funds.json [public id, no private token needed]
the returned anwer is
{'response': {'collected_funds': 9655,
'contributions_count': 718,
'funding_ends_at': '2016-07-10T23:59:59-07:00',
'id': 1757572}}


So we want to expand this projec from .py files to mobile web aplication, and think construct 2 is a grat tool for easy graphics and interface
for this we need to parse JSON and get 'collected_funds' in a number variable

can you help us?
thanks for you time
B
4
Posts: 2
Reputation: 200

Post » Sat May 28, 2016 9:12 pm

You can fetch web-api data with AJAX object and then load it into Dictionary so you'll be able to grab any part from JSON easily. But to load it into a Dictionary you need a specific JSON format.

Here's a snipped from my lib. It's PHP but I'm sure you get the point.
Image

So you have to change your web-api output or parse the fetched sting in C2 before loading into Dictionary. You can use regular expressions as well.
ImageImageImage
B
28
S
16
G
82
Posts: 1,030
Reputation: 45,804

Post » Sat May 28, 2016 10:10 pm

Well, in fact i don't even know how to use construct to open a website (dont mean showing it content in a frame, but to store it's data)
All i need is to store 'collected_funds' in a numeric variable, I have no idea how to turn that .php code into construct....

in python all i need is 3 lines:

resp = urllib.request.urlopen(url).read()
data = json.loads(resp.decode('utf-8'))
funds = data['response']['collected_funds']


can you help with some exemple?
B
4
Posts: 2
Reputation: 200

Post » Sat May 28, 2016 10:48 pm

Just checked it out quickly. Indiegogo does not allow cross-domain "grabs", so you can't fetch this data with AJAX. I'm afraid there is no other way in C2 to download remote data so you need to make a work-around.

1. Create your own server side script which will download this JSON from Indiegogo (using CURL or whatever) once triggered and simply output it's content (kind of intermediary script).

2. Set your server (or script) to allow AJAX fetch data from the different domain.
To do that you can set Apache with .htaccess
Code: Select all
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>


3. Since you do the server script anyway, I would parse the JSON data already in that script and output only the number you want to fetch in C2.

4. Now in C2 you just make a simple AJAX request as below:
Image
ImageImageImage
B
28
S
16
G
82
Posts: 1,030
Reputation: 45,804

Post » Sat May 28, 2016 11:45 pm

[plugin] JSON

ajaxJSON.capx

Indiegogo does not allow cross-domain "grabs"...

works for me...(now??!)..

Image
B
67
S
24
G
7
Posts: 1,518
Reputation: 11,072

Post » Sun Sep 17, 2017 9:44 am

korbaach Thank you so much for the solution, I have been looking for it for days now. Thank you so much!
B
2
Posts: 2
Reputation: 154

Post » Mon Sep 18, 2017 10:12 am

Complete novice, first post yet one of the things I wanted to be able to do is this. Cheers
B
1
Posts: 1
Reputation: 127


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 48 guests