0

I have this endpoint - https://gbfs.urbansharing.com/oslobysykkel.no/station_information.json

This is response

{
  "last_updated": 1553592653,
  "data": {
    "stations": [
      {  
        "station_id":"627",
        "name":"Skøyen Stasjon",
        "address":"Skøyen Stasjon",
        "lat":59.9226729,
        "lon":10.6788129,
        "capacity":20
      },
      {  
        "station_id":"623",
        "name":"7 Juni Plassen",
        "address":"7 Juni Plassen",
        "lat":59.9150596,
        "lon":10.7312715,
        "capacity":15
      },
      {  
        "station_id":"610",
        "name":"Sotahjørnet",
        "address":"Sotahjørnet",
        "lat":59.9099822,
        "lon":10.7914482,
        "capacity":20
      }
    ]
  }
}

How I can parse the data to receive only this parameters for every object?

    "address":"Skøyen Stasjon",
    "lat":59.9226729,
    "lon":10.6788129

I was trying to do something like this

const GbfsClient = require('gbfs-client');
const gbfsClient = new GbfsClient('https://gbfs.urbansharing.com/oslobysykkel.no/');

gbfsClient.stationInfo()
    .then(osloStatus => console.log(osloStatus));

But I receive everything in console as well with that. I don't understand how to call endpoints which is ending on .json

Can anyone please give me a hint on how I can do this.

Hexycode
  • 428
  • 8
  • 26
  • 2
    You you need the `address`, `lat` and `lon` for each object in the `stations` array? Or just the first one? – 0stone0 Oct 01 '20 at 12:27
  • For each object, sorry forgot to mention. Fixing queition now – Hexycode Oct 01 '20 at 12:28
  • I'm not sure I (and you) understand what you want to do. Are you trying to create a custom JSON parser that skips the data that is irrelevant to you ? – Touffy Oct 01 '20 at 12:30
  • (as opposed to using the regular JSON parser and then removing the extra data ?) – Touffy Oct 01 '20 at 12:31
  • The *.json ending suggests this is just a file, not an API. So you have to parse it and then access and map what you want. See the duplicate questions for details. – str Oct 01 '20 at 12:31
  • @Touffy Yes, there is some data in object which is not interesting for me, the only lat, lon and address is interested for me. – Hexycode Oct 01 '20 at 12:33
  • [You're welcome](https://jsfiddle.net/rq9bvk8t/1/) :) `data.stations.map(({ address, lat, lon }) => ({address, lat, lon}))` – Alexandre Elshobokshy Oct 01 '20 at 12:34
  • AGAIN, I was writing code to help the guy and when I wanted to submit, the Q was closed. I'm done with posting answers. – Ozone Oct 01 '20 at 12:35
  • @Ozone Post the answer in the comments, no big deal, it's a duplicate anyway – Alexandre Elshobokshy Oct 01 '20 at 12:36
  • The usual solution of mapping the result to remove extra props is fine but inefficient. If performance is an issue or you just need to optimize everything for fun, use something like [Ajv](https://www.npmjs.com/package/ajv#performance) to compile a specialized JSON parser with a schema of just what you need. – Touffy Oct 01 '20 at 12:43

1 Answers1

0

// Fetch data
fetch("https://gbfs.urbansharing.com/oslobysykkel.no/station_information.json", {
    method: 'GET',
    redirect: 'follow'
})
.then(response => response.json())
.then(result => {

    // For each 'station'
    result.data.stations.forEach((station) => {
    
      // Process data
      console.log(station.address, station.lat, station.lon);
    });
})
.catch(error => console.log('error', error));
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • This is a great answer to your Q. You could use only specific parameters By `Destructuring assignment` (Cleaner code - but the idea will be the same). Anyway, you get her Over-fetching related to your specific API call (capacity -or- address for example) – Ezra Siton Oct 01 '20 at 12:41
  • @Hexycode The api returns all the fields, therefore the response knows the other parameters. Just as [Ezra Siton](https://stackoverflow.com/questions/64155359/how-to-parse-correct-json-data/64155490?noredirect=1#comment113448256_64155490) mentioned, you could use [destructing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) to 'remove' those values. However, you could use 'not use' those. – 0stone0 Oct 01 '20 at 13:09