How do I export complex json data to excel/google sheet - javascript

I am getting the following Json string from Firebase Realtime database.
{
'algotable': {
'-NJ6blqyqu8RgjvdQJyT': {
'actual': '123',
'cipher': '39bc62c2ad990f494e0ad2b5715a3f05',
'decrypted': '123'
},
'-NJ6bmsPnb1GNi_jYyPb': {
'actual': 'abcd',
'cipher': '39bc62c2ad990f494e0ad2b5715a3f05',
'decrypted': 'abcd',
}
}
}
I tried importing json file in micrsoft excel but it shows only two columns without any data i.e. algotable,records
I tried google appscript
console.log(string1.algotable.getData);
but I don't get any output because object inside algotable always changes its name whenever I push an object through android-java as below
public String writeNewRecordEncrypt(Algotable algotable) {
DatabaseReference pushref = mDatabase.push();
pushref.setValue(algotable);
return pushref.getKey();
}
please help me amend this. thanks in advance

Related

How do I fetch Weather's API 'forecastday' using axios?

I use WeatherAPI's service, which returns the weather forecast given the city name
The URL looks like this https://api.weatherapi.com/v1/forecast.json?key=[API_KEY]&q=tokyo&aqi=no
After trying to paste that URL into my browser and pasting the result into a JSON beautifier, here's the result
Here's the weird part. I tried using axios to fetch the information from my app and printing it out, this is what it gave me
It was unable to fetch forecastday and instead gave me a [Object], which didn't make any sense to me since it worked just fine on my browser
Here's my code (sorry for the spaghetti formatting)
https://pastebin.com/9eJQy5Bf
I tried reinstalling the library, using proxies but the result remained the same.
forecast.forecastday is an object array, to access a property from a particular object, you have to specify the index of the object in the array.
For example if you want the date property of the first object.
const data = {
forecast: {
forecastday: [{
date: "2022-04-03"
}]
}
}
const date = data.forecast.forecastday[0].date; // index [0] = first element
console.log(date);
If you would have multiple days, you could loop over them or use a function like map() to get the specific items.
const data = {
forecast: {
forecastday: [{
date: "2022-04-03",
date_epoch: 123456789
},
{
date: "2022-04-04",
date_epoch: 123456789
}
]
}
}
const dates = data.forecast.forecastday.map(({ date }) => {
return { date }
});
console.log(dates);

How to create an object through loop using vue multiselect

I'm using vue multiselect for a project with laravel.
The selected values are saved in the database in this way.
['value_on','value_two','value_three', and so on...]
In the part where the user can do the updates I need to display the values coming from the database.
This is the object I need to create
sizeValue: [
{size: 'val_one},
{size: 'val_two},
{size: 'val_three}
...and so on
]
Right now I just have an empty array where I should loop the data from the database.
//This is the empty array
sizeValue: [];
//This is the array coming from the server it will return['val_one','val_two','val_three',...and so on]
product.sizes
Since I don't have much experience with javascript I would like to ask how I can get this result
sizeValue: [
{size: 'val_one},
{size: 'val_two},
{size: 'val_three}
...and so on
]
looping the values (product.sizes) coming from the DB in the empty sizeValue.
you could simply create the sizeValue as a computed property and use the javascript map function.
This could look like this:
<script>
export default {
computed: {
sizeValue() {
return this.product.sizes.map(productSize => {
return {
size: productSize
};
});
}
}
};
</script>
Hope this helps :)

How can I accesss to the elements of array objects?

How Can I get data from this json data ?
I get this data from the server, and it sends some database's datas by using sequelize.
Data what I want to access is
{result: "[{"userid":"a","speed":10},{"userid":"b","speed":20},{"userid":"c","speed":30}]"}]" }
And I tried console.log(result[0].userid);
But, I only got errer like this. ' Uncaught TypeError: Cannot read property 'userid' of undefined'.
Could you tell me what I have to fix?
You gave an invalid JSON, the json should be something like this:
Formatted JSON Data
{
"result":[
{
"userid":"a",
"speed":10
},
{
"userid":"b",
"speed":20
},
{
"userid":"c",
"speed":30
}
]
}
And after that you can access it:
const json = {
"result":[
{
"userid":"a",
"speed":10
},
{
"userid":"b",
"speed":20
},
{
"userid":"c",
"speed":30
}
]
};
console.log(json.result[0].userid);
Your JSON is badly formatted.
Try this instead...
{"result":[{"userid":"a","speed":10},{"userid":"b","speed":20},{"userid":"c","speed":30}]}
The main issues were...
{result: "[{"userid"
Should be...
{"result":[{"userid"
And
"speed":30}]"}]" }
Should be...
"speed":30}]}
Please note, the whitespace between the items makes no difference, but I have remove to reduce the size of the JSON string

Adding a new key-value to a map in Dynamo DB in JavaScript

I have an item in a dynamodb table that has a key of type map. The name of the key is mykey and the map stored in the key is:
{
name: something
address: somewhere
}
I want to add a new item to this map data. Let's say for example, color. The updated map should look like this:
{
name: something
address: somewhere
color: red
}
I am using JavaScript SDK but I'm unable to figure out how to go about this. After reading documentation, I think I need to use list_append in updateItem function but I am not able to figure out how.
I do not want to read data, add the new key-value, and write back. This will create a 'read before write' concurrency problem as I have multiple processes trying to update the map.
You need to use the updateItem API. I don't have a specific example in JavaScript, but the basic idea is this:
var params = {
Key: {
KeyName: {
S: "KeyValue"
}
},
TableName: "TheTableName",
AttributeUpdates: {
color: {
Action: "ADD",
Value: {
S: "red"
}
}
}
};
client.updateItem(params, callback);

firebase getting only keys then getting data

i have a data structure as following at the url
www.example.firebase.com/
{
"companyList" : {
"compkey1" : {
"url1":"somelink1",
"url2":somelink2
},
"compkey2" : {
"url1":"somelink1",
"url2":"somelink2"
}
}
}
What i want to achieve is that i want firebase to return first the list of companies which is
compkey1
compkey2
and not any child data
then if the user want to see a specific company i want them to go to that url
like so
www.example.firebase.com/companyList/compkey2
new to firebase so explain as such.
The Firebase JavaScript client always retrieves complete nodes. It has no option to retrieve only the keys.
If you want to retrieve only the keys/names of the company, you'll have to store them in a separate node.
{
"companyList" : {
"compkey1" : {
"url1":"somelink1",
"url2":"somelink2"
},
"compkey2" : {
"url1":"somelink1",
"url2":"somelink2"
}
},
"companyKeys" : {
"compkey1": true,
"compkey2": true
}
}
A common recommendation in Firebase (and many other NoSQL databases) is to model your data in a way that your application will need to read it. In the above example, it seems like you need to read a list of company keys, so that is what you should model.
Note: the Firebase REST API does have a shallow=true parameter that will return only the keys. But I recommend solving the problem by modeling the data differently instead.
Firebase has a shallow parameter which can retrieve only the keys. I verified it's way faster (by a factor of 100), than retrieving the whole nodes.
Here it is in Google App Script (sorry):
class FirebaseNamespace {
get database() {
if(!this._database) {
var firebaseUrl = "https://mydatabase.firebaseio.com/";
var secret = "mysecret";
this._database = FirebaseApp.getDatabaseByUrl(firebaseUrl, secret);
}
return this._database;
}
get(path, parameters) {
return this.database.getData(path, parameters);
}
keys(path) {
return Object.keys(this.get(path, {shallow:true}));
}
save(path, value) {
this.database.setData(path, value);
return value;
}
}

Categories

Resources