json and json-server with weird be valid json file.
-
(not sure where to ask this). Still learning...not for work. I have a valid json (geojson) file like this : (reduced for visibility, original : [Murales subventionnées - Site web des données ouvertes de la Ville de Montréal](https://donnees.montreal.ca/dataset/murales) ) it's a list of features but the id is in a sub field of the feature.
{
"features": [
{
"type": "Feature",
"properties": {
"id": 1,
"annee": "2007"
},
"geometry": { "type": "Point", "coordinates": [-73.622218, 45.58041] }
},
{
"type": "Feature",
"properties": {
"id": 2,
"annee": "2007"
},
"geometry": { "type": "Point", "coordinates": [-73.558029, 45.506855] }
}
]
}I can fetch the whole list with the following and it works :
fetch ("http://localhost:4000/features );
I'm trying to use fetch to get a single "feature" from the file:
fetch ("http://localhost:4000/features/1");
But this does not work, it returns nothing. I tried different format for the fetch URL. If I move the id property to the root of each item, it works, but I don't want to do that. Am I missing something ? Is there anything I can do ? Thanks.
CI/CD = Continuous Impediment/Continuous Despair
-
(not sure where to ask this). Still learning...not for work. I have a valid json (geojson) file like this : (reduced for visibility, original : [Murales subventionnées - Site web des données ouvertes de la Ville de Montréal](https://donnees.montreal.ca/dataset/murales) ) it's a list of features but the id is in a sub field of the feature.
{
"features": [
{
"type": "Feature",
"properties": {
"id": 1,
"annee": "2007"
},
"geometry": { "type": "Point", "coordinates": [-73.622218, 45.58041] }
},
{
"type": "Feature",
"properties": {
"id": 2,
"annee": "2007"
},
"geometry": { "type": "Point", "coordinates": [-73.558029, 45.506855] }
}
]
}I can fetch the whole list with the following and it works :
fetch ("http://localhost:4000/features );
I'm trying to use fetch to get a single "feature" from the file:
fetch ("http://localhost:4000/features/1");
But this does not work, it returns nothing. I tried different format for the fetch URL. If I move the id property to the root of each item, it works, but I don't want to do that. Am I missing something ? Is there anything I can do ? Thanks.
CI/CD = Continuous Impediment/Continuous Despair
fetch
sends a request to the server, and returns the response. It doesn't attempt to interpret the response and extract any part of it. Unless you have a specific endpoint on the server to handle the request for a single feature, you'll need to fetch the full list and parse it yourself.(async () => {
const response = await fetch("http://localhost:4000/features");
if (!response.ok) {
alert("Error loading the feature list.");
return;
}const { features } = await response.json(); const specificFeature = features.find(f => f.properties.id === 1); if (!specificFeature) { alert("Feature 1 was not found."); return; } // Do something with the feature here...
})();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
fetch
sends a request to the server, and returns the response. It doesn't attempt to interpret the response and extract any part of it. Unless you have a specific endpoint on the server to handle the request for a single feature, you'll need to fetch the full list and parse it yourself.(async () => {
const response = await fetch("http://localhost:4000/features");
if (!response.ok) {
alert("Error loading the feature list.");
return;
}const { features } = await response.json(); const specificFeature = features.find(f => f.properties.id === 1); if (!specificFeature) { alert("Feature 1 was not found."); return; } // Do something with the feature here...
})();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
thanks, I'll probably do it manually. but I don't understand who/what creates those end points. for example, if I have this simple json file:
{
"items": [{
"id": 1
},
{
"id": 2
},
{
"id": 2
}
]
}I can fetch individual items with [http://localhost:3000/items/1\](http://localhost:3000/items/1) but if I have :
{
"items": [{
"properties":{
"id": 1
}}, { "properties":{ "id": 2 } }, { "properties":{ "id": 3 } } \]
}
it does not work.
CI/CD = Continuous Impediment/Continuous Despair
-
thanks, I'll probably do it manually. but I don't understand who/what creates those end points. for example, if I have this simple json file:
{
"items": [{
"id": 1
},
{
"id": 2
},
{
"id": 2
}
]
}I can fetch individual items with [http://localhost:3000/items/1\](http://localhost:3000/items/1) but if I have :
{
"items": [{
"properties":{
"id": 1
}}, { "properties":{ "id": 2 } }, { "properties":{ "id": 3 } } \]
}
it does not work.
CI/CD = Continuous Impediment/Continuous Despair
You need to look at the code that's running on your server. As far as I'm aware, there's no magic tool that would convert a URL like "/items/1" into a request to load a JSON file called "items", get the "items" property from the root object, find the entry with
id === 1
, and return just that object. You must have code running which does that, which you (or someone else) has written.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer