Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. json and json-server with weird be valid json file.

json and json-server with weird be valid json file.

Scheduled Pinned Locked Moved JavaScript
sysadmindevopsjsonquestionlearning
4 Posts 2 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    (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

    Richard DeemingR 1 Reply Last reply
    0
    • M Maximilien

      (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

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        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

        Richard DeemingR 1 Reply Last reply
        0
        • M Maximilien

          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

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups