Flutter: Unexpected character (at character 1) - json

In Flutter, no matter what json file i try to decode I get the same above titled error.
Originally I thought it was something with my project but after starting a new template Flutter project I still receive the same error. I have the json file in my root folder and have added them to the pubspec.yaml file:
assets:
- Sample-JSON-data.json
- Sample-employee-JSON-data.json
Main.dart code at the moment:
var jsonString = 'Sample-JSON-data.json';
var response = jsonDecode(jsonString);
print(response);
I have verified my json data on multiple test sites as well as tried various methods in the Flutter Documentation. Json data:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.0763783444317,
-33.98045132346684
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.0763783444317,
-33.98045132346684
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.07774912725728,
-33.97470462237792
]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
151.0763783444317,
-33.98045132346684
]
}
}
]
}

jsonDecode expects an input to be valid JSON serialized into String fe.
{ "message": "Hello World!" }. So this would work: jsonDecode('{"message": "Hello World!"}').
What you are passing to it is a file name, it won't automatically read the file for you. You can check how to do that here:
Flutter - Read text file from assets

Actually you are not loading the data from the json file.
Try:
var jsonString = await rootBundle.loadString('Sample-JSON-data.json')

Related

Filtering on GeoJSON data

I was wondering how filtering is normally done on a FeatureCollection for GeoJSON data. For example, take the following earthquake data:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": "ak16994521", "mag": 2.3, "time": 1507425650893, "felt": null, "tsunami": 0 }, "geometry": { "type": "Point", "coordinates": [ -151.5129, 63.1016, 0.0 ] } },
{ "type": "Feature", "properties": { "id": "ak16994519", "mag": 1.7, "time": 1507425289659, "felt": null, "tsunami": 0 }, "geometry": { "type": "Point", "coordinates": [ -150.4048, 63.1224, 105.5 ] } },
{ "type": "Feature", "properties": { "id": "ak16994517", "mag": 1.6, "time": 1507424832518, "felt": null, "tsunami": 0 }, "geometry": { "type": "Point", "coordinates": [ -151.3597, 63.0781, 0.0 ] } },
{ "type": "Feature", "properties": { "id": "ci38021336", "mag": 1.42, "time": 1507423898710, "felt": null, "tsunami": 0 }, "geometry": { "type": "Point", "coordinates": [ -118.497, 34.299667, 7.64 ] } },
{ "type": "Feature", "properties": { "id": "hv61900626", "mag": 2.91, "time": 1504833891990, "felt": null, "tsunami": 0 }, "geometry": { "type": "Point", "coordinates": [ -155.011833, 19.399333, 2.609 ] } }
]
}
Now, if this data is all within a single FeatureCollection, how would anyone filter the data, for example to view earthquakes with magnitude > 2.5 ? It seems like when dealing with a FeatureCollection the first thing to do would be to extract each of the features into its own item: is that what is usually done, so that individual properties may be queried?
GeoJSON is a transfer format and as you have noticed any operation on it requires you to read and parse the whole file every time. If you plan to do anything with the data you should translate it into a more useful format which supports indexes. If you need to keep to a file based format then I recommend GeoPackage which is supported by the majority of modern GIS. Alternatively, you could use a spatially enabled database such as PostGIS.
In either case the easiest way to convert the data is to use ogr2ogr

Multiple items for a single coordinate in GeoJSON

I've set up a rest API that is supplying geojson data from a database. The issue is each coordinate could/does have multiple items associated to it.
Currently troubleshooting the application's javascript file to load, and plot - but looking for feedback on best practice. Any information is appreciated. Thanks!
var foo = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"item": "A",
},
"geometry": {
"type": "Point",
"coordinates": [
90,
135
]
}
},
{
"type": "Feature",
"properties": {
"item": "B",
},
"geometry": {
"type": "Point",
"coordinates": [
90,
135
]
}
},
{
"type": "Feature",
"properties": {
"item": "C",
},
"geometry": {
"type": "Point",
"coordinates": [
90,
135
]
}
},
...

Invalid response of JSON data from Django QuerySet

With this query:
def high_hazard(request):
reference_high = FloodHazard.objects.filter(hazard='High')
ids_high = reference_high.values_list('id', flat=True)
flood_hazard = []
djf = Django.Django(geodjango='geom', properties=['bldg_name', 'bldg_type'])
geoj = GeoJSON.GeoJSON()
for myid in ids_high:
getgeom = FloodHazard.objects.get(id=myid).geom
response_high = BuildingStructure.objects.filter(geom__intersects=getgeom)
get_hazard = geoj.encode(djf.decode(response_high.transform(900913)))
flood_hazard.append(get_hazard)
return HttpResponse(flood_hazard, content_type='application/json')
I was able to filter the BuildingStructure model based on FloodHazard type which is in this case with "high" value. Although it returns a JSON data, the output is messed up. I guess because it tests all the geometry from the FloodHazard model during loop. So, it returns several null set or empty and lots of FeatureCollection which makes it an invalid JSON data. The output of the query above is like this:
{
"crs": null,
"type": "FeatureCollection",
"features": [
]
}{
"crs": null,
"type": "FeatureCollection",
"features": [
]
}{
"crs": null,
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "MultiPoint",
"coordinates": [
[
13974390.863509608,
1020340.6129766875
]
]
},
"type": "Feature",
"id": 3350,
"properties": {
"bldg_name": "",
"bldg_type": ""
}
},
{
"geometry": {
"type": "MultiPoint",
"coordinates": [
[
13974400.312472697,
1020356.5477410051
]
]
},
"type": "Feature",
"id": 3351,
"properties": {
"bldg_name": "",
"bldg_type": ""
}
}
]
}
As I test it with a JSON validator, it is invalid. So, is there a way to restructure(using underscore.js or jquery) this JSON to output like below? or I need to change my query?
{
"crs": null,
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "MultiPoint",
"coordinates": [
[
13974390.863509608,
1020340.6129766875
]
]
},
"type": "Feature",
"id": 3350,
"properties": {
"bldg_name": "",
"bldg_type": ""
}
},
{
"geometry": {
"type": "MultiPoint",
"coordinates": [
[
13974400.312472697,
1020356.5477410051
]
]
},
"type": "Feature",
"id": 3351,
"properties": {
"bldg_name": "",
"bldg_type": ""
}
}
]
}
and just ignore/remove all the FeatureCollection without values and group all with values. Here is the result of the query above for reference.
Instead of
return HttpResponse(flood_hazard, content_type='application/json')
Try
return HttpResponse(json.dumps(flood_hazard), content_type='application/json')
You will have to import json at the top.

Can't fetching records from nested JSON objects in Sencha Touch Application

I'm stuck with issue for getting web-service response from JSON in Sencha Touch.
I'm getting response from server.
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "business_poi_en.1",
"geometry": {
"type": "Point",
"coordinates": [
28.21962354993591,
36.452844361147314
]
},
"geometry_name": "geom",
"properties": {
"status": "M",
"score": 100,
"side": "R",
"ref_id": 0,
"id": null,
"business_p": "AQUARIOUM",
}
},
{
"type": "Feature",
"id": "business_poi_en.2",
"geometry": {
"type": "Point",
"coordinates": [
28.225417523605692,
36.436470953176716
]
},
"geometry_name": "geom",
"properties": {
"status": "M",
"score": 68.44,
"match_type": "A",
"side": "L",
"ref_id": 0,
"id": null,
"business_p": "ZIGOS",
}
},
.... So On ....
I want to fetch data for coordinates from geometry tag, so I can display it on map via these coordinates.I also want to fetch data for business_p from properties tag as displaying title on map.
But, I can't get both of values for coordinates & business_p at same time from same response.
Any idea for getting values at same time ?
Any suggestion will be appreciated.
Please help with this issue.
Thanks in advance.
Your Store's proxy's reader should have correct rootProperty which is common parent of both coordinates and business_p. For that your response should be wrapped into a top level tag like this
{
"featureResponse": [
{
"type": "FeatureCollection",
"features": []
},
{
"type": "Feature",
"features": []
},
]
}
Then you can define reader like this:
reader: {
type: 'json',
rootProperty: 'featureResponse'
}
once you get record from this store, you can go to data or raw children object to fetch required data.

TileMill and Geojson; Datasource could not be loaded

Im following this article to get a geojson file from an excel using Google Refine.
http://support.mapbox.com/kb/tilemill/converting-addresses-in-spreadsheets-to-custom-maps-in-tilemill
Now I've got the weird issue that I sometimes get the error message that the Datasource could not be loaded, while I'm using the exact same template everytime. The only thing different seems to be the data I'm loading.
Also if I compare the 2 geojson files (1 that is working and 1 that is not) I can see no difference.
Does anyone know what the problem could be? .. is it the use of spaces/breaks or whatever perhaps??
This one is working:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": 25.019909,
"geometry": { "type": "Point", "coordinates": [121.365599, 25.019909] },
"properties":
{
"Users" : 1
}
},
{
"type": "Feature",
"id": 24.446706,
"geometry": { "type": "Point", "coordinates": [117.818197, 24.446706] },
"properties":
{
"Users" : 1
}
},
{
"type": "Feature",
"id": 25.100632,
"geometry": { "type": "Point", "coordinates": [117.03403, 25.100632] },
"properties":
{
"Users" : 1
}
},
{
"type": "Feature",
"id": 47.354348,
"geometry": { "type": "Point", "coordinates": [123.918186, 47.354348] },
"properties":
{
"Users" : 1
}
},
{
"type": "Feature",
"id": 30.199652,
"geometry": { "type": "Point", "coordinates": [115.03852, 30.199652] },
"properties":
{
"Users" : 3
}
},
...
...
...
}
]
}
While this one is giving me the error;
{
"type": "FeatureCollection",
"features": [ {
"type": "Feature",
"id": 25.019909,
"geometry": { "type": "Point", "coordinates": [121.365599, 25.019909] },
"properties":
{
"Users" : 1
}
},
{
"type": "Feature",
"id": 24.446706,
"geometry": { "type": "Point", "coordinates": [117.818197, 24.446706] },
"properties":
{
"Users" : 1
}
},
{
"type": "Feature",
"id": 25.100632,
"geometry": { "type": "Point", "coordinates": [117.03403, 25.100632] },
"properties":
{
"Users" : 1
}
},
...
...
...
}
]
}
You have an extra '}' in the second one. But, of course this may only look this way because of what you removed when adding the ..., so its impossible to say unless you post the whole thing that is not working.
Hint: find a text editor that highlights matching braces - most should do this if you tell the editor you are viewing javascript or json code.
I'm sorry.. it was something in the data after all :( .. tilemill is very strict in what it accepts and what not apparently..