Add element in JSON without key identifier - FIREBASE - json

This is the structure of my firebase database and the json file that I used to create that firebase structure.
"Menu" it's a list of ingredients, divided for categories, like "Pane" (Bread in english). "Pane" that has a field "lista" that have alle the type of breads (each type of bread has 3 fields: "attivo", "nome", "prezzo").
I need to ask the user to add a new ingredient, so to add a new element that has the fields "attivo", "nome", "prezzo".
The problem is that my sub ingredients (types of Breads) don't have an identifier, so I don't know how to add a children to "Lista" without an id key.
With this code (used in my typescript file) all the "lista" field it's replaces by the new element, and I can't use .Child() because I don't have an identifier to pass:
firebase.database().ref('/menu/pane/lista').set({
nome: data['nome'],
prezzo: data['prezzo'],
attivo: false,
});
Is it possible to add an element without have an identifier and have a situation like my json file?
EDIT
If I use set or push, my json structure change and I don't want that:
Thank you in advance.
Solved
I've solved the problem with a workaround. I retrieve the array that populate "lista" and push the new ingredient like in a normal array, than I push into "lista" on firebase the array with the new element. In this way all "lista" content it's replaced by the array that I push, but the array has the new element so it works!

Yes it would be possible to save the values into an array that you would store under the lista node, but this would create some "extra complexity". See this Firebase Blog post for more details: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
The recommended way to add some data to a list without having a (natural) uid is to use the push method:
https://firebase.google.com/docs/reference/js/firebase.database.Reference#push.
So, you should do as follows and Firebase will automatically generate a unique id for your new record:
firebase.database().ref('/menu/pane/lista').push({
nome: data['nome'],
prezzo: data['prezzo'],
attivo: false
});
If you don't want the identifiers of your pane nodes to be auto-generated (as alphanumeric value like "-LStoAsjJ....") you would need to generate them yourself. But then you would have to use a transaction to generate this sequence, and this will add some complexity too. It is probably better to use push() and re-engineer your front end code in such a way you can deal with the alphanumeric uids generated by Firebase.

Related

receive Excel data and turn into objects to format a JSON

I have this solution that helps me creating a Wizard to fill some data and turn into JSON, the problem now is that I have to receive a xlsx and turn specific data from it into JSON, not all the data but only the ones I want which are documented in the last link.
In this link: https://stackblitz.com/edit/xlsx-to-json I can access the excel data and turn into object (when I print document.getElementById('output').innerHTML = JSON.parse(dataString); it shows [object Object])
I want to implement this solution and automatically get the specified fields in the config.ts but can't get to work. For now, I have these in my HTML and app-component.ts
https://stackblitz.com/edit/angular-xbsxd9 (It's probably not compiling but it's to show the code only)
It wasn't quite clear what you were asking, but based on the assumption that what you are trying to do is:
Given the data in the spreadsheet that is uploaded
Use a config that holds the list of column names you want returned in the JSON when the user clicks to download
based on this, I've created a fork of your sample here -> Forked Stackbliz
what I've done is:
use the map operator on the array returned from the sheet_to_json method
Within the map, the process is looping through each key of the record (each key being a column in this case).
If a column in the row is defined in the propertymap file (config), then return it.
This approach strips out all columns you don't care about up front. so that by the time the user clicks to download the file, only the columns you want are returned. If you need to maintain the original columns, then you can move this logic somewhere more convenient for you.
I also augmented the property map a little to give you more granular control over how to format the data in the returned JSON. i.e. don't treat numbers as strings in the final output. you can use this as a template if it suites your needs for any additional formatting.
hope it helps.

Multiple entry in one mysql cell and Json data

I have lots folder named as a some series name. And every series folder it has its own chapter folder. In chapter folder some images in it. My website is manga(comic) site. So I am gonna record this folder's and image's path to mysql and return to the Json data for using with AngularjS. So How should I save these folders path or names to mysql for the get proper Json data and using with angularjs.
My table is like this: Can change,
id series_name folder path
1 Dragon Ball 788 01.jpg02.jpg03.jpg04.jpg05.jpg06.jpg..........
2 One Piece 332 01.jpg...................
3 One PÄ°ece 333 01.jpg02.jpg...........
My current website:
Link to Reader Part of My WebSite
I'm assuming you're using PHP on a LAMP stack. So first you would need to grab all your SQL fields and change them into JSON keys.
Create JSON-object the correct way
Then you can create your JSON object like this and pass it to Angular when it does an AJAX request. Make sure you create the array before placing it into your JSON object (for path).
{
id: Number,
series_name: String,
folder: Number,
path: [
String, String, String, ...
]
}
Here is the Angular documentation for an Angular GET request.
https://docs.angularjs.org/api/ng/service/$http
EDIT:
It's difficult because of how your filenames are formatted. If it were formatted like "01.jpg,02.jpg,03.jpg" it would be easier.
You can use preg_split with the regex:
$string = "01.jpg02.jpg03.jpg04.jpg05.jpg06.jpg";
$keywords = preg_split("/(.jpg|.png|.bmp)/", $string);
but you would need them all to be the same extension, then you need to re-append the extension to each element after you split it.
There may be a better way.

Store multiple authors in to couchbase database

I am a newbie to "couchbase server". What i am looking for is to store 10 author names to couchbase document one after another. Someone please help me whether the structure is like a single document "author" and multiple values
{ id : 1, name : Auther 1}, { id : 2, name : Author 2}
OR store Author 1 to a document and Author 2 to another document.
If so, how can i increment the id automatically before "insert" command.
you can store all authors in a single document
{ doctype : "Authors",
AuthorNames:[
{
id: 1,
Name : "author1"
}
{
id: 2,
Name : "author2"
}
so on
]
IF you want to increase the ID, one is to enter one author name at a time in new document, but ID will be randomly generated and it would not in incremental order.
In Couchbase think more about how your application will be using the data more than how you are want to store it. For example, will your application need to get all of the 10 authors all of the time? If so, then one document might be worthwhile. Perhaps your application needs to only ever read/write one of the authors at a time. Then you might want to put each in their own, but have an object key pattern that makes it so you can get the object really fast. Objects that are used often are kept in the managed cache, other objects that are not used often may fall out of the managed cache...and that is ok.
The other factor is what your reads to writes ratio is on this data.
So like I said, it depends on how your application will be reading and writing your data. Use this as the guidance for how your data should be stored.
The single JSON document is pretty straight forward. The more advanced schema design where each author is in its own document and you access them via object key, might be a bit more complicated, but ultimately faster and more scalable depending on what I already pointed out. I will lay out an example schema and some possibilities.
For the authors, I might create each author JSON document with an object key like this:
authors::ID
Where ID is a value I keep in a special incrementer object that I will called authors::incrementer. Think of that object as a key value pair only holding an integer that happens to be the upper bound of an array. Couchbase SDKs include a special function to increment just such an integer object. With this, my application can put together that object key very quickly. If I want to go after the 5th author, I do a read by object key for "authors::5". If I need to get 10, I do a parallelized BulkGet function and get authors::1 through authors::10. If I want to get all the authors, I get the incrementer object, and get that integer and then to a parallelized bulk get. This way i can get them in order or in whatever order I feel like and I am accessing them by object key which is VERY fast in Couchbase.
All this being said, I could use a view to query this data or the upcoming "SQL for Documents" in Couchbase 4.0 or I can mix and match when I query and when I get objects by their key. Key access will ALWAYS be faster. It is the difference between asking a question then going and getting the object and simply knowing the answer and getting it immediately.

Angular trying to add scope data to an array

I am trying to add data from the Scope to an Array.
In my example I have $scope.employee which have several other fields, like name, number, email, etc.
So when I push the data to the Array I use:
array.push($scope.employee);
This works, because it adds the data to the array, but the binding still applies, so if I later on change thing in the scope, the data in the array also changes. I want this array to be static and not change. Just to hold the elements I have passed into it.
I would like to only add the data from the $scope.employee and not the binding.
I know it's possible to:
array.push({name: $employee.name, number: $employee.number});
But the $scope.employee have many fields, therefore I want to push the whole object.
You want to use angular.copy to make a deep copy without the pointers.
array.push(angular.copy($scope.employee));
documentation

Google Refine: iterate over a JSON dictionary

I've got some JSON within Google Refine - http://mapit.mysociety.org/point/4326/0.1293497,51.5464828 for the full version, but abbreviated it's like this:
{1234: {'name': 'Barking', 'type': 'WMC'},
5678: {'name': 'England', 'type': 'EUR'} }
I only want to extract the name for the object with the (presumed unique) type WMC.
Parse JSON in Google Refine doesn't help, that's working with arrays, not dicts.
Any suggestions what I should be looking at to fix this?
Edit: I don't know what the initial keys are: I believe they're unique identifiers which I can't predict ahead of time.
Refine doesn't currently know how to iterate through the keys of a dict where they keys are unknown (although I'm about to implement that functionality).
The trick to getting this working with the current implementation is to convert the JSON object to a JSON array. The following GREL expression will do that, parse the result as JSON, iterate through all elements of the array and give you the first name of type 'WMC'.
filter(('['+(value.replace(/"[0-9]+":/,""))[1,-1]+']').parseJson(),v,v['type']=='WMC')[0]['name']
Use that expression with the "Add column based on this column" command to create a new WMC name column. If there's a chance that there'll be more than one name of this type and you want them all, you can add in a forEach loop and join along the lines of
forEach(filter(('['+(value.replace(/"[0-9]+":/,""))[1,-1]+']').parseJson(),v,v['type']=='WMC'),x,x['name']).join('|')
This will give you a pipe separated list of names that you can split apart using "Split multi-valued cells."
It'll be easier in the next release hopefully!