want read a specific item from data() method in firestore - json

snapshot.forEach(doc => {
console.log("ID: "+doc.id, '=>', "Doc DATA: "+JSON.stringify(doc.data()));
});
I want to read only 1 item from doc.data() and that is an array of strings called ("supportedCurrencies").
How do i read? I'm new to firestore and TS so any help much appreciated.
Thanks!

When you call doc.data() you get a plain JavaScript object whose properties are each of the fields in the document:
const data = doc.data();
If you want one of those fields, just access it by name as a property of that object:
const supportedCurrencies = data.supportedCurrencies;
If it's an array, then you can treat it just like any other JavaScript array object.

Related

How to extract data from an API in json

I am trying to create simple code to fetch data from an api which contains the price of bitcoin and store it. I am new to json and have tried everything I could think of. Here is the code.
await getRemoteData('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
.then((response) => {
const data = JSON.parse(response);
const price = ${data.value};
})
Thanks in advance!
JSON looks something like {"foo": "bar"} so it's basically collection of strings and values. in order to get value from some json say value of key "price", in js you need to parse JSON string to JSON Object first (which you already did by const data = JSON.parse(response);) and get the price by:-
const price = data.price // assuming that the key is "price"
if you want some other value with different key, say key is "foo" then you can fetch the value by let valueOfFoo = data.foo

How to take some data from an object?

I have to put data from json file to my reducer.
And after mapping this file I got an object which includes data that I need.
export const datas = data.properties.map(data => (<store key={Math.floor(Math.random()*1234)} author={data.value} comment={data.group} rate={data.type} />));
this is console log, I just need props from this
How to get just normall react-table, not an object which is not accepting by reducer state?
Or how to implement it to reducer state to get effect that I need?
I am sorry for stupid questions, I hope you will help me :)
The code which you posted takes an array of objects from a variable data.properties and maps them to an array of JSX elements created by the component store. Perhaps it is better readable with line breaks.
export const datas = data.properties.map(
data => (
<store
key={Math.floor(Math.random() * 1234)}
author={data.value}
comment={data.group}
rate={data.type}
/>
)
);
Your console.log of datas shows that array of React JSX element instances. In your array you have 5 objects which are each a JSX element ($$typeof: Symbol(react.element)) with type: "store" (the component type), the numeric key that you created for this element, and a props object which contains all of the props that you passed: author, comment, and rate (key is a special prop so it is not included here).
You are asking to just create an object with the props rather than creating a store element. We want to take the array data.properties and use the .map() method to map it to this props object.
The variable name that you use inside of your .map() callback can be anything, but as a best practice I recommend that you should not use the variable name data again. Reusing a name is called variable shadowing and it won't cause errors, but it can make your code very confusing. I will call it datum instead.
Here is the code that you want:
export const propsArray = data.properties.map(
datum => ({
key: Math.floor(Math.random() * 1234),
author: datum.value,
comment: datum.group,
rate: datum.type,
})
);

Reading JSON file with different datatypes

I am trying to read a JSON file that contains following data
"{\"studentData\":[{\"Name\":\"Graham\",\"aggregate\":\"86\",\"regular\":\"true\",\"percentages\":[{\"sub1\":\"69\",\"sub2\":\"97\",\"sub3\":\"90\"}]},{\"Name\":\"Finley\",\"aggregate\":\"96\",\"regular\":\"false\",\"percentages\":[{\"sub1\":\"95\",\"sub2\":\"91\",\"sub3\":\"73\"}]},{\"Name\":\"Carrillo\",\"aggregate\":\"93\",\"regular\":\"true\",\"percentages\":[{\"sub1\":\"90\",\"sub2\":\"84\",\"sub3\":\"80\"}]},{\"Name\":\"Crosby\",\"aggregate\":\"68\",\"regular\":\"true\",\"percentages\":[{\"sub1\":\"63\",\"sub2\":\"92\",\"sub3\":\"77\"}]},{\"Name\":\"Small\",\"aggregate\":\"88\",\"regular\":\"true\",\"percentages\":[{\"sub1\":\"65\",\"sub2\":\"80\",\"sub3\":\"81\"}]}]}"
I have following code so far
const data = require("./testdata.json");
/*Explore the JSON file and return required JSON data*/
console.log(data)
when I run the code, I see the output in console
But how do I refer to each item in the data
e.g. Name, Regular
When I try to access using below code,
console.log(data.studentData.Name)
I get error
console.log(data.studentData.Name)
^
TypeError: Cannot read property 'Name' of undefined
data.studentData is an array so you need to loop through each value.
With forEach for example:
data.studentData.forEach((individualStudentData) => {
console.log(individualStudentData.Name);
//Do your thing (:
});
Or:
for (let individualStudentData of data.studentData)
a function like .map(() => { ... }
etc. ;)
It looks like data.studentData is actually a JSON Array. So if you wanted to log every name, you'd need to do
const data = require("./testdata.json");
data.studentData.forEach( student => console.log(student.Name));

How to update the local json field in flutter

I am new to flutter and dart I read a lot of web article and documentation but didn't able to figure out how to update JSON field I have a local JSON file like
{
"category": "Happiness",
"quotes":[
{
"quote":"I hope you will find a reason to smile",
"favorite":false
},
{
"quote":"Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.",
"favorite":false
}]}
I want to update the filed favorite to true in JSON file Is there any way to do this
You Can parse the JSON like this
Map<String, dynamic> data = jsonDecode(jsonString);
And if you want to make each favorite true then You can use looping like this
(data["quotes"] as List<dynamic>).forEach((item) => item["favorite"] = true);
if you want to set a single object value true then you need to pass the position like this
(data["quotes"] as List<dynamic>)[position]['favorite'] = true;
Addition
Here is the link to encode and decode the JSON
You can use a forEach loop on the quotes list in the JSON and set favourite to true inside it. I'm assuming you have a parsed your String data into JSON using jsonDecode or json.decode.
If not, here's a link on how to parse JSON data in Dart.
Consider the below code -
Map<String, dynamic> jsonData = {
"category": "Happiness",
"quotes":[
{
"quote":"I hope you will find a reason to smile",
"favorite":false
},
{
"quote":"Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.",
"favorite":false
}
]};
(jsonData["quotes"] as List<dynamic>).forEach((item) {
item["favorite"] = true;
});
You can also alternatively use shorthand syntax for the forEach loop function like below -
(jsonData["quotes"] as List<dynamic>).forEach((item) => item["favorite"] = true);
first take this json array to any variable and then understand the hierarchy .
e.g "jsonArr" is the array that hold that data then ..
jsonArr['quotes'][0]['favorite'] = true;
or you can put this in a for loop and traverse this loop to the length of the json array.
You can use the following method. The field favorite is changed to true when the button is pressed.
FlatButton(
child: Text('Add Favourite'),
onPressed: (){
myJson['quotes'][myIndex]['favorite'] = true;
})

Decomposing data from a json withangularjs

I have a json file that is requested from a server so i can't modify how is sent, but all the data i need is there, so i want to put that data into the charts, the thing is how i can break down the json file, because the relevant data is nested, the structure is
estados(array of object)
id (property)
name (property)
localidades (array of object)
id (property)
name (property)
type1 (object)
type2 (object) etc..
i need to retrieve the id data from the object estados and localidades then especific data one of the objects nested in localidades.
How i can do that? i only know how to go through every nested object by iteration, but decomposing it?
here is a json sample :
http://www.jsoneditoronline.org/?id=9cd4c3af94f14b1cca2d35226794ea78
The Lodash library (Or Underscore, if you prefer) is what you are looking for. You can use it to manipulate your collections any way you want (pretty much). You could potentially do without helper libraries, but it is much easier (and sometimes faster, though browser dependent [ref]) to do it this way.
Here are some examples in the jsFiddle I put together for a demo. Some example of how to access particular property of an object inside an array of objects:
$scope.findObjects = function () {
var estado = _.find($scope.allJson, {
"idEstado": $scope.idToSearch
});
$scope.exampleOutput = _.filter(estado.localidades, function (item) {
return item.demanda && (item.demanda.ata > 5);
});
};