6

I have a folder of JSON files that I'd like to use to create a simple API from.

Here's a simplified version of my folder structure:

/clients.json

/clients/1/client.json

/clients/2/client.json

...

my /clients.json file looks like this:

[
    {
        "id": 1,
        "name": "Jon Parker"
    },
    {
        "id": 2,
        "name": "Gareth Edwards"
    },
    ...
]

and my /clients/1/client.json file looks like this:

[
    {
        "date": "2014-09-12",
        "score": 40,
        ...
    },
    {
        "date": "2015-02-27",
        "score": 75,
        ...
    },  
    {
        "date": "2015-05-10",
        "score": 75,
        ...
    },
    {
        "date": "2016-08-27",
        "score": 60,
        ...
    }
]

The id from clients.json relates to the folder in which the associated details are.

I have a lot of JSON files in the clients folder and rather than loading these all individually on the client side, I wanted to create an API using Node.js that gives me more flexibility, i.e...

returning a list of client names and id's /clients

returning the client details /clients/:id/details

and most importantly returning all clients with there names and associated details /clients/all/details

I did begin playing with json-server, however it requires that your JSON be an object rather than an array, and I'm stuck with the format of this JSON unfortunately.

Appreciate any help!

tremendows
  • 4,262
  • 3
  • 34
  • 51
DanV
  • 3,193
  • 4
  • 29
  • 43
  • you can transform an array into an object after loading the data; it shouldn't take too long to do JIT, and would give you the keying your API expects. – dandavis Dec 30 '16 at 21:36

5 Answers5

4

Use the built-in file system module to get files from the file system.

Refer here

Here's an example.

var fs = require('fs');

exports.getClientDetail = function (id) {
  var result;
  fs.readFile('/clients/' + id + '/client.json', function (err, data) {
    if (err) throw err;

    result.push(JSON.parse(data));
  });
}
exports.getAllClientsDetail = function () {      
  // if the id is sequential, you can check if '/clients/' + id + '/client.json' exists for every i from 1 and up.  If it does - push it to an array of objects. if for a certain i it does not exist, quit the scan and return the array of objects.
}
guysigner
  • 2,822
  • 1
  • 19
  • 23
2

You aren't nearly as stuck as you think.

You'll have to wrap your arrays in an object. Then, in the front end, you just have to access the array property.

After all, JSON is an acronym for Javascript Object Notation.

EDIT: Okay, let's try something new...

Perhaps before using code from json-server, do a little preprocessing. Assuming that variable clientJson is the file you have already read, inserting this code before you use any code from json-server:

clientJson = "{root:"+clientJson+"}";

That will wrap the file in an object with the first property being root.

After that, it's pretty easy to get your array back:

clientData = clientData.root;
hellol11
  • 422
  • 4
  • 11
  • This is true, however these JSON files are automatically generated and I have no control of, and manually editing them isn't really an option – DanV Dec 09 '16 at 00:46
  • 1
    Why isn't manual editing an option? are these dynamic files, ie constantly changing? – Shazam Jan 02 '17 at 06:24
2

You can require the json directly as an object in node, something like this:

app.get('/clients/:id/details', (req, resp) => {
  const id = req.params.id;
  const data = require(`./clients/${id}/client.json`); // or whatever path
  resp.send(data)
});
7zark7
  • 10,015
  • 5
  • 39
  • 54
  • 1
    Just be aware require will throw an exception if it can't parse the file. It will also cache required files. – ste2425 Dec 28 '16 at 23:47
  • are there tags missing from the Q or A or does node have a new `.get` method? – dandavis Dec 30 '16 at 21:34
  • It's assuming express , I noticed DanV mention `/clients/:id/details` path syntax that express uses. – 7zark7 Dec 31 '16 at 01:42
1

You could do it without any code if you uploaded your folder structure to a cloud service (ex. Amazon S3 or Dropbox) and serve them from there. No code required.

Guido
  • 46,642
  • 28
  • 120
  • 174
1

You should use Read streams from FS module to send data to the client,catch possible errors and cleaning memory after send.

blckt
  • 81
  • 8