Sorry all!! I know there are several (many) post regarding JSON and google api. I read them all and tried fixing the problem for several days. I am going to explode if I have to clear my browsing history, again, to start with a "fresh" mind...
I could really use some help. Any and all help would be greatly appreciated.
Console error prints : "TypeError: b[le] is not a function"
Here is my function:
function timeline() {
$.post('NewQuery.php', function ( data ) {
alert (data);
var data = new google.visualization.DataTable(JSON.parse(data));
var view = new google.visualization.DataView(data)
view.setColumns ([0,2])
chart = new google.visualization.LineChart(document.getElementById('chart2'));
chart.draw(view);
});
}
Here is the JSON object that is 'alerted'- I formatted it to make it more legible.
{
"cols":[
{"label":"Dia","type":"date"},
{"label":"Fruta","type":"string"},
{"label":"CostoTotal","type":"number"}
],
"rows":[
{"c":[{"v":"Dia(2014, 3, 9)"},{"v":"Kiwis"},{"v":2628}]},
{"c":[{"v":"Dia(2014, 3, 18)"},{"v":"Cerezos"},{"v":3200841}]},
{"c":[{"v":"Dia(2014, 3, 23)"},{"v":"Peras"},{"v":113696}]},
{"c":[{"v":"Dia(2014, 3, 1)"},{"v":"Cerezos"},{"v":4294967295}]}
]
}
The problem is with your date strings, they should use the word "Date", not "Dia". This:
"Dia(2014, 3, 9)"
should be this:
"Date(2014, 3, 9)"
Related
I am studying quasar at the moment, and I want to know how to export a table to JSon. I have data in JSonimported and I want to get output in JSon too.
Now I'm using JSON.stringify to get data of my table, but that gives all data in a single line in JSon.
Here's a piece of my code in quasar :
exportTable() { const status = exportFile('table.json', JSON.stringify(this.data), 'text/json')}
and it gives me :
[{"Name":"xxx","Adress":"xxx","Zip code":"xxx"},{"Name":"yyy","Adress":"yyy","Zip code":"yyy"}]
What I want to output :
[{
"Name":"xxx",
"Adress":"xxx",
"Zip code":"xxx"
},
{
"Name":"yyy",
"Adress":"yyy",
"Zip code":"yyy"
}]
How can I get this ?
If I understood correctly, you want to use:
JSON.stringify(this.data, null, 4)
Where the last argument (4) is the spacing you desire.
You can have a look at JSON.stringify official documentation
const data = [{"Name":"xxx","Adress":"xxx","Zip code":"xxx"},{"Name":"yyy","Adress":"yyy","Zip code":"yyy"}]
console.log(JSON.stringify(data, null, 4))
I am trying to write my custom API view and I am struggling a bit with querysets and JSON. It shouldn't be that complicated but I am stuck still. Also I am confused by some strange behaviour of the loop I coded.
Here is my view:
#api_view()
def BuildingGroupHeatYear(request, pk, year):
passed_year = str(year)
building_group_object = get_object_or_404(BuildingGroup, id=pk)
buildings = building_group_object.buildings.all()
for item in buildings:
demand_heat_item = item.demandheat_set.filter(year=passed_year).values('building_id', 'year', 'demand')
print(demand_heat_item)
print(type(demand_heat_item)
return Response(demand_heat_item))
Ok so this actually gives me back exactly what I want. Namely that:
{'building_id': 1, 'year': 2019, 'demand': 230.3}{'building_id': 1, 'year': 2019, 'demand': 234.0}
Ok, great, but why? Shouldn't the data be overwritten each time the loop goes over it?
Also when I get the type of the demand_heat_item I get back a queryset <class 'django.db.models.query.QuerySet'>
But this is an API View, so I would like to get a JSON back. SHouldn't that throw me an error?
And how could I do this so I get the same data structure back as a JSON?
It tried to rewrite it like this but without success because I can't serialize it:
#api_view()
def BuildingGroupHeatYear(request, pk, year):
passed_year = str(year)
building_group_object = get_object_or_404(BuildingGroup, id=pk)
buildings = building_group_object.buildings.all()
demand_list = []
for item in buildings:
demand_heat_item = item.demandheat_set.filter(year=passed_year).values('building_id', 'year', 'demand')
demand_list.append(demand_heat_item)
json_data = json.dumps(demand_list)
return Response(json_data)
I also tried with JSON Response and Json decoder.
But maybe there is a better way to do this?
Or maybe my question is formulated clearer like this: How can I get the data out of the loop, and return it as a JSON
Any help is much appreciated. Thanks in advance!!
Also, I tried the following:
for item in buildings:
demand_heat_item = item.demandheat_set.filter(year=passed_year).values('building_id', 'year', 'demand')
json_data = json.dumps(list(demand_heat_item))
return Response(json_data)
that gives me this weird response that I don't really want:
"[{\"building_id\": 1, \"year\": 2019, \"demand\": 230.3}, {\"building_id\": 1, \"year\": 2019, \"demand\": 234.0}]"
I am making a react app that searches for a book by title and returns the results.
It's mostly working fine, but for some titles searched (such as "hello") it can't get the results because the parameters are missing.
Specially, the "amount" value is missing, and it can get me e-books that are not for sale even if I add the filter=paid-ebooks param while fetching the api. Using projection=full doesn't help either.
For example, when I call the api with
https://www.googleapis.com/books/v1/volumes?printType=books&filter=paid-ebooks&key=${APIKEY}
and use the fetched data inside books array in reactjs:
this.props.books.map((book, index) => {
return (
<CardItem
key={index}
title={book.volumeInfo.title}
authors={book.volumeInfo.authors ?
book.volumeInfo.authors.join(', ') :
"Not provided"}
price={book.saleInfo.listPrice.amount}
publisher={book.volumeInfo.publisher}
addToCart={() =>
this.props.addItem(this.props.books[index])}
/>
)
})
One of the results it gets is like this:
"saleInfo": {
"country": "TR",
"saleability": "NOT_FOR_SALE",
"isEbook": false
}
While it should be like, what's expected is :
"saleInfo": {
"country": "TR",
"saleability": "FOR_SALE",
"isEbook": true,
"listPrice": {
"amount": 17.23,
"currencyCode": "TRY"
}
And trying to search with this api answer throws the error :
TypeError: Cannot read property 'amount' of undefined
price={book.saleInfo.listPrice.amount}
As you can see in react code's authors, this issue comes up with authors parameter too, which I've bypassed as seen in the code. But I cannot do the same with amount. Is this a known error in Google Books API or is there a way to prevent this? I don't understand why it still returns me e-books that are not for sale even with filter=paid-ebooks param.
I have not dug into the API documentation. An ideal solution would be a query param that only sends back books with a list price (like you tried with filter=paid-ebooks). Because that's not working, a simple fix would be to filter your results once you get them.
Assuming the response contains an array of book objects, it would look something like this:
const paidBooks = apiResponse.data.filter(book => book.listPrice)
This code will take the response from the API, and filter out all books that do not contain a truthy value for listPrice
That totally right, actually i never used react but the same logic try using try{ }catch(error){} for those missing data
I have a problem writing a query to extract a table out of the arrays from a json file: The problem is how to get the information of the array “clientPerformance” and then make them all in a normal sql table.
The file looks like this:
"clientPerformance":[
{
"name":"opportunity",
"clientProcess":{
"value":3620000.0,
"count":1.0,
"min":3620000.0,
"max":3620000.0,
"stdDev":0.0,
"sampledValue":3620000.0
},
"networkConnection":{
"value":10000.0,
"count":1.0,
"min":10000.0,
"max":10000.0,
"stdDev":0.0,
"sampledValue":10000.0
},
"receiveRequest":{
"value":9470000.0,
"count":1.0,
"min":9470000.0,
"max":9470000.0,
"stdDev":0.0,
"sampledValue":9470000.0
},
"sendRequest":{
"value":1400000.0,
"count":1.0,
"min":1400000.0,
"max":1400000.0,
"stdDev":0.0,
"sampledValue":1400000.0
},
"total":{
"value":14500000.0,
"count":1.0,
"min":14500000.0,
"max":14500000.0,
"stdDev":0.0,
"sampledValue":14500000.0
},
"url":"https://xxxx",
"urlData":{
"base":"/main.aspx",
"host":"xxxx",
"hashTag":"",
"protocol":"https"
}
}
]
I tried to use Get array elements method and other ways but I am never able to access to clientProcess, networkConnection.. elements
I tried to use for exampls this one:
Select
GetRecordPropertyValue(GetArrayElement(Input.clientPerformance, 0), 'name') AS Name,
GetRecordPropertyValue(GetArrayElement(Input.clientPerformance, 1), 'clientProcess.count') AS clientProcessCount,
FROM [app-insights-blob-dev] Input
I would appreciate any help :)
I slightly edited your query to return the clientProcessCount.
(I changed the array index from 1 to 0). Also make sure your JSON object starts with { and ends with }.
Select
GetRecordPropertyValue(GetArrayElement(Input.clientPerformance, 0), 'name') AS Name,
GetRecordPropertyValue(GetArrayElement(Input.clientPerformance, 0), 'clientProcess.count') AS clientProcessCount
FROM [app-insights-blob-dev] Input
For other examples to query complex objects with ASA, don't hesitate to look at this blog post.
I''m using EF to query the database using anonymous type.
here the code I use for EF
public JsonResult OverdueEventsCustom()
{
var eventCustomOverdue = _eventCustomRepository.FindOverdueEventsCustom();
return Json(eventCustomOverdue, JsonRequestBehavior.AllowGet);
}
public IQueryable<dynamic> FindOverdueEventsCustom()
{
DateTime dateTimeNow = DateTime.UtcNow;
DateTime dateTomorrow = dateTimeNow.Date.AddDays(1);
return db.EventCustoms.Where(x => x.DateTimeStart < dateTomorrow)
.Select(y => new { y.EventId, y.EventTitle, y.DateTimeStart});
}
Inspecting using the debugger I see the properties is in this format
Date = {16/08/2012 00:00:00}
The resultfor the JSON is
[{
"EventId": 1,
"EventTitle": "Homework Math",
"DateTimeStart": "\/Date(1345108269310)\/"
}, {
"EventId": 4,
"EventTitle": "Homework help with Annie",
"DateTimeStart": "\/Date(1345108269310)\/"
}, {
"EventId": 6,
"EventTitle": "Physic laboratory",
"DateTimeStart": "\/Date(1345108269310)\/"
}]
I need the the json in this format
"DateTimeStart": "(16/08/2012)"
Any idea what i'm doing wrong here? thanks for your help
Related articles
http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx
How do I format a Microsoft JSON date?
"\/Date(1345108269310)\/" is the correct way to pass a Date to javascript. The way I see it, you have two options here:
If you do not explicitly need the value as a date, you could just pass a string to the JSON variable, containing the pretty-printed date.
Something along the lines of:
DateTimeStart: String.Format("{0: dd-MM-yyyy}", myDate)
If you will still need to use the variable a a date in javascript (for calculations for example), the most consice and readably way would be to create a javascript function that converts said date into the pretty-printed string you want (I don't know if such a function already exists. It isn't too hard to create though:
function prettyDate(date) {
return date.getDate() + "-" + date.getMonth() + "-" + date.getFullYear();
}
I would suggest passing it along as a string from you code behind, as it is more readable. But that only works if you do not need to use the date except for displaying.