Make a rest API call from a JSON database to receive the number of documents

Hello all,
I am trying to figure out how i can receive the number of documents from my database.
The purpose of this is to show my users the number of current posts in total on my site (each post is a document). Hope all this makes sense :sweat_smile:
Thank you for your help!

SELECT COUNT(*) AS number_of_documents
  FROM documents

Did r937 answer your question? I only ask since it’s not an api call.

@bwclovis Yeah I do not really understand his answer. Would you know how to do it with a http api call?

‘a http api call’.
What API? There’s millions of API’s in the world, all with their own format and structure. Without knowing your specific target’s API, we can only offer vagueries.

Rudy’s post states the query to retrieve the number of rows from a table in most SQL databases.

Depends on how your api is structured, so I’m going to make some assumptions.

After your preferred method for api call(.ajax, fetch, axios…) you should return an array since this is a json db, if it’s not then you MIGHT have to convert it to one. Then it’s just a matter of getting the length so something like

const numberOfPosts = returnedData.length

While Rudy’s post is good for a direct db query, if this is a micro service or a serverless page, or about 20 other things I can think of, the call probably should be made front end to the service.

What server-side programming language are you using?

Good point. I read r937’s reply

SELECT COUNT(*) AS number_of_documents
  FROM documents 


  • from the table that has a row for each of the documents
  • run a query that returns the number of rows (implied, know or check the docs regarding COUNT)
  • alias the result name to something other than “count”

It’s a database reply to a topic in the Database category. If that piece of the puzzle is no longer a problem, or the problem is about putting the pieces together, perhaps starting a new topic in its category would be a good idea :wink:

As far as I can tell, this question has nothing to do with server side or a db query. There are many other factors that I can think of where front end has to deal with making an API call to get the info and from what OP states, that is the issue at hand I believe.