Firebase dump json data - android

I'm no back-end developer. So perspective is always appreciated.
I have written a script which requests from an API and creates this huge JSON file I want to save in firebase, how can I accomplish this? And would it be possible to filter this json with python for example; when I add region=eu in the url this returns the objects which have Europe as region or do I absolutely need to request the entire json file and parse in my code (java android) ?

Since there are a few parts to your question:
You can save JSON to Firebase and the data will be mapped to child locations:
Using PUT, we can write a string, number, boolean, array or any JSON object to our Firebase database...When a JSON object is saved to the database, the object properties are automatically mapped to child locations in a nested fashion.
https://firebase.google.com/docs/database/rest/save-data
And for your next question:
And would it be possible to filter this json with python for example; when I add region=eu in the url this returns the objects which have Europe as region
Looks like you should be able to jimmy something together with Firebase's filters, startAt and endAt:
We can combine startAt and endAt to limit both ends of our query.
https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-filtering
For your example you might do something like this:
curl 'https://yourfirebase.firebaseio.com/yourendpoint.json?orderBy="$REGION_NAME"&startAt="EU"&endAt="EU"&print=pretty'
...or do I absolutely need to request the entire json file and parse in my code (java android) ?
The facts that JSON objects are stored hierarchically in Firebase and that you can filter based on those object values makes me think you do not, in fact, have to request the entire JSON file. However, I don't have personal experience with this particular aspect of Firebase, so give it a shot!
As #ackushiw mentions in the comments, you can also use the equalTo query (https://firebase.google.com/docs/reference/js/firebase.database.Query#equalTo):
curl 'https://yourfirebase.firebaseio.com/yourendpoint.json?orderBy="$REGION_NAME"&equalTo="EU"&print=pretty'

It really depends on how you are structuring your JSON. It's generally recommended to make your JSON tree as shallow as possible since all children are loaded when you have a matching query.
FIREBASE DATA:
{
"-id1": {
"region": "eu" // bear in mind queries are case sensitive
"title": "Foo"
"nested": {
"city": "berlin"
}
},
"-id2": {
"region": "other"
"title": "Bar"
"nested": {
"city": "berlin"
}
},
"-id3": {
"region": "eu"
"title": "Baz"
"nested": {
"city": "paris"
}
}
}
Querying with (using the Android API)
.orderByChild("region").equalTo("eu")
would return "-id1" and "-id3"
with
.orderByChild("nested/city").equalTo("berlin")
would return "-id1" and "-id2"
The REST API Returns Unsorted Results: JSON interpreters do not enforce any ordering on the result set. While orderBy can be used in combination with startAt, endAt, limitToFirst, or limitToLast to return a subset of the data, the returned results will not be sorted. Therefore, it may be necessary to manually sort the results if ordering is important.
If you're using a more complex structure I recommend watching this video
https://www.youtube.com/watch?v=vKqXSZLLnHA
I'd also recommend using the firebase library for Android
https://firebase.google.com/docs/android/setup
And Firebase-UI, It does a lot for you.
https://firebaseopensource.com/projects/firebase/firebaseui-android/

Related

How to assign GUID to records in Firebase Realtime Database when importing JSON

I need to import a large dataset into a Firebase Realtime Database for a new Android app. Preparing the data is not a problem, I have a VBA script in Excel that does this, however, I would like to have some nodes in the imported database to have a GUID, like the ones generated when "pushing" new data to Firebase. I am not able to find out how to do this.
For example if I import this test dataset:
{
"Genera": {
"Genus_1": {
"S001" : {
"name": "name_1",
"common_name": "common_1"
},
"S002": {
"name": "name_2",
"common_name": "common_2"
}
}
}
I would like to replace the "Genus_1" and "Sxxxx" strings with a GUID-type string.
If this is possible how would I achieve it.
Thanks,
Sid
When you import a single JSON snippet into the Firebase Realtime Database, Firebase does not generate any keys for child data for you. This means you have two options:
Import each part in an individual call.
Pre-generate your own keys.
Import each part in an individual call
If you want Firebase to generate a key for specific parts of your JSON, you will need to call the Firebase API for each such part, calling push() or its equivalent for the platform you use. E.g.
curl -X POST -d '{
"S001" : {
"name": "name_1",
"common_name": "common_1"
}
}' 'https://yourdb.firebaseio.com/Genera.json'
Since you're using POST here, the Firebase server will generate a new key under Genera for the JSON in this call.
Pre-generate your own keys
If you want to import the JSON in one go, you will have to pre-generate the keys yourself. Luckily the algorithm for generating push IDs is available online, and documented in this blog post: The 2^120 Ways to Ensure Unique Identifiers.

Mlab Contains Query

Is it possible to use a like/contains query in mLab?
I have json data like so on the server https://mlab.com/:
"County": "Kildare, Laois, Carlow"
I can query the dataset to return all where trails = County Kildare:
#GET("databases/walks/collections/walks")
Call<List<Trail>> byCounty (#Query("q") String county,#Query("apiKey") String apiKey);
The parameter "q" contains: q={"County": "Kildare"}
But that only select data that match the following :
"County": "Kildare"
Is there an Mlab query that has the same function as Contains in MongoDB or Like in SQL. So i can search the county field if it contains "Kildare".
Or even how to do it in A HTTP request, i might be able to use it within the q={ }.
Ive tried using q={"County":"/.*Kildare.*/"}
Thank You.
The q parameter in the mLab Data API is just a MongoDB query document. You can use any MongoDB operator like you would normally in a MongoDB query document.
MongoDB doesn't have a $contains operator. But if you're searching for text in a string, you can use the $regex or $text operator. See the documentation for $regex and the documentation for $text to learn more.
To use $regex, try this as the q parameter:
q={"County": {"$regex": "Kildare"}}
To use $text, first build a text index on the County field. You can do this on the mLab management portal (instructions in the documentation) or run this command on the mongo shell:
db.walks.createIndex({ County: "text" })
You can then try this as the q parameter:
q={"$text": {"$search": "Kildare"}}
Important Note: $regex and $text can be extremely inefficient operations. It may be much more efficient and easier for you to restructure your data model. Rather than having a list of counties in a string, perhaps have them in an array like so:
"County": ["Kildare", "Laois", "Carlow"]
Then to search for all documents that contain Kildare in their County array, you can simply use this query:
q={"County": "Kildare"}
This may be a better option for you than using $regex or $text.

How can we post dynamic json values in Retrofit

I am developing an e-commerce android application. am stuck in a situation, i don't know how to handle the situation. That is,
How can i post dynamic json values in Retrofit. For example
{
"type":"1",
"typeId":"10",
"userId":"15",
"filters":{
"price":"from-to",
"attribute_code":"values",
"...":"....",
"...":"...."
}
}
in this the "filter" is another object in the main json. the values in "filter" json is dynamic like sometime it may have "price", "size" or sometimes it may have "price" only or some times it may have "price","size","offers" etc..
How can we handle this circumstance..
Thanks

Android, Json modifying element

I have a json file on server:
{"images":[
{"url":"...", "likes":"123"},
{"url":"...", "likes":"234"},
{"url":"...", "likes":"345"}
]}
I get the json file on android read it, but if someone likes a picture i want to change the value of the first picture from 123 to 124, is this possible and how can i do this?
The whole point is to change a json value on server,from client side.
Also if this isn't possible how can i make this happen?
Also if i want to get the Top50 rated pictures,how can i sort them and get only the 50 picture,without getting all pictures and sorting them on android ?
Which one is better,initializing the Top50 images,when the user starts the app,or when the user click on the button Top50.I assume that if its when he click the button,there might be some performance issues server side?
My other idea is to have a function server side,which every 10 min,executes automatically and gets the Top50 rated and makes something like json file.So it all happens server side automatically.
To make this happen, client should expose some interface, i.e. function that will allow to modify file on server side. The interface and implementation of this function greatly depends on server itself, i.e. which protocols it handles, what built-in or external modules it supports, which languages are supported, etc... For example, the classic scenario is using apache as HTTP server, CGI enabled, and write CGI function in perl. So, in this case interface would look like http://server.name/like.cgi?image=image123.
How to modify the values on the server ?
For this every like of a photo should be a post request of this sort.
{
"data": [
{
"image_id": 3133456,
"likes": 343
},
{
"image_id": 3133456,
"likes": 343
}
]
}
On parsing this request server updates the corresponding image's like on the server.
How to get the top 50 rated/liked images from the server ?
Again you send a get request to such a url
http://server.getsomething.com/getTop50Images
On server side
On receiving such a request you make a query on the table in your database something like this
select image_id , image_url, likes from image_table limit 50 ORDER BY likes ASC
Now getting those query results and returning them as a json would not be a performance hit until you have huge bulk of data. like some million rows may be in your database.
Response can be something like this
{
"result": [
{
"image_id": 3133456,
"likes": 34400,
"url": "http://flickr.com/someimage"
},
{
"image_id": 3133456,
"likes": 34380,
"url": "http://flickr.com/someimage"
}
]
}
You still avoid using a database yourself but can lease it from clouds services like parse.
However if you won't be using those services then you can take a look at ftp packages for js. Like the neo JavaScript library by apache.
But still a good choice will be to go with the database approach (is quiet simpler).

With Android GCM, can you use a deep JSON 'data' field?

That is, can you send
{
"registration_ids": ["whatever", ...],
"data": {
"foo": {
"bar": {
"baz": [42]
}
}
}
}
or is the "data" member of the GCM request restricted to one level of key-value pairs? I ask b/c that limitation is suggested by the wording in Google's doc[1], where it says "data" is:
A JSON object whose fields represents the key-value pairs of the message's payload data. If present, the payload data it will be included in the Intent as application data, with the key being the extra's name. For instance, "data":{"score":"3x1"} would result in an intent extra named score whose value is the string 3x1 There is no limit on the number of key/value pairs, though there is a limit on the total size of the message. Optional.
[1] http://developer.android.com/guide/google/gcm/gcm.html#request
Just did a test myself and confirmed my conjecture.
Send a GCM to myself with this payload:
{
"registration_ids": ["whatever", ...],
"data": {
"message": {
"bar": {
"baz": [42]
}
}
}
}
And my client received it and parse the 'message' intent extra as this:
handleMessage - message={ "bar": { "baz": [42] } }
So the you can indeed do further JSON parsing on the value of a data key.
Although it appears to work (see other answers and comments), without a clear statement from Google, i would not recommend relying on it as their documentation consistently refers to the top-level members of the json as "key-value pairs". The server-side helper jar they provide [1] also reinforces this idea, as it models the user data as a Map<String, String>. Their Message.Builder.addData method doesn't even support non-string values, so even though booleans, numbers, and null are representable in json, i'd be cautious using those, too.
If Google updates their backend code in a way that breaks this (arguably-unsupported) usage, apps that relied on it would need an update to continue to work. In order to be safe, i'm going to be using a single key-value pair whose value is a json-stringified deep object [2]. My data isn't very big, and i can afford the json-inside-json overhead, but ymmv. Also, one of my members is a variable-length list, and flattening those to key-value pairs is always ugly :)
[1] http://developer.android.com/guide/google/gcm/server-javadoc/index.html (The jar itself is only available from within the Android SDK in the gcm-server/dist directory, per http://developer.android.com/guide/google/gcm/gs.html#server-app )
[2] e.g. my whole payload will look something like this:
{
"registration_ids": ["whatever", ...],
"data": {
"player": "{\"score\": 1234, \"new_achievements\": [\"hot foot\", \"nimble\"]}"
}
}
Scuse me if I'm wrong, Map<String, String> denotes key=string and value=string.
If string is a long unreasonable json extract which is UTF-8 formatted and well escaped. It stands to reason that should you call new JSONObject(receivedString); and it works then all other json calls follow.
Do not forget that raw JSON is a string! We do not need google to clarify how to work with strings..this is why your test worked!

Categories

Resources