Under review

Import JSON file to populate local database

doug 6 years ago updated 6 years ago 8
I was wondering if it is possible to be able to import a JSON file (local or remote) and have it populate a local SQLite database within the app?

I ask because I would like to be able to take a CSV, convert it to JSON (which I found some Javascript that can do this) and the populate the database with this new data.
Under review

You would have to query the json from the app just like any other time, only once you get the querycomplete, then you can insert those data points into a SQL query string in a new datasource. 

So it would look something like this:

<datasource name="json" source="www.jsonfeed.com" query="//" providertype="json" querycomplete="save-jsonstuff">
<datasource name="db" source="sql://jsonstuff" query="CREATE TABLE IF NOT EXISTS JSONSTUFF (ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, PAGE TEXT, THUMB TEXT);" providertype="sql"/>    <datasource name="db-list" source="sql://jsonstuff" query="SELECT * FROM JSONSTUFF ORDER BY ID DESC;" providertype="sql"/> 

<action name="save-jsonstuff">    <assign property="datasource:db.query" value="INSERT INTO FAVORITES (title, page, thumb) values ('[datasource:json.1.title]', '[ <assign property="datasource:db.query" value="INSERT INTO FAVORITES (title, page, thumb) values ('[datasource:json.1.page]', '[ <assign property="datasource:db.query" value="INSERT INTO FAVORITES (title, page, thumb) values ('[datasource:json.1.thumb]')" />    
<assign property="datasource:db-list.query" value="SELECT * FROM JSONSTUFF"/></action>
Ok, great. That is exactly what I needed.

Now, what if I wanted to take this data that was in the table, construct a new JSON feed based on the data in this table, and push it back to Parse. Almost like a synching function. How would I go about doing that?

In that case you may consider using the datasource properties of add and removeindex. You can read about those here: 

These only work for JSON. 
<assign property="datasource:datasource1.add" value='{"url":"_url"}'/>
<assign property="datasource:datasource1.removeIndex" value='3'/>

Ok I understand what the purpose of removing the index. However what does the assign with the value of 3 do? I am still trying to access the response code that Parse sends back when you are logging in.
3 would be the third value in the datasourceindex. Or an item in the datasource list.  
Makes sense, however I am still getting the same results as I was before. As far as I can tell, nothing is changing. The JSON response looks like this:

	"code": "101",
	"error": "invalid credentials"
and this is my action:
<action name="sign-in" datasource="parseLogin" datasourceevent="querycomplete">
	<assign property="datasource:parseLogin.removeIndex" value="1" />
	<alert message="[datasource:parseLogin.code]" />
and what I am trying to do is get the code.
You likely will not be able to removeIndex this json, b/c it doesn't belong to a user with auth to do that. 

To get the code, your alert should be: <alert message="[datasource:parseLogin.1.code]" /> 
Ok, I swear I tried that, but it looks like it's working now. Thanks for your patience.