1

I'm using json-server for hosting an API in my project.

What json-server gives me is http://localhost:3000/records.

{
    "record": [
        {
            "userID": "2",
            "userFirstName": "Arya",
            "userLastName": "Stark",
            "userMiddleName": "null",
            "roleName": "Admin",
            "roleID": "1"
        },
        {
            "userID": "1",
            "userFirstName": "John",
            "userLastName": "Snow",
            "userMiddleName": "null",
            "roleName": "Moderator",
            "roleID": "1"
        }
    ]
}

I'm using Postman for making GET request to the server.

How do I go into record array and access the object with userID as 2 ?

I tried this way but it's returning an empty object.

Please correct me if I'm wrong:

http://localhost:3000/records/record?userID=2

My actual JSON object file:

{
  "records": {
    "record": [
      {
        "userID": "2",
        "userFirstName": "Arya",
        "userLastName": "Stark",
        "userMiddleName": "null",
        "roleName": "Admin",
        "roleID": "1"
      },
      {
        "userID": "1",
        "userFirstName": "John",
        "userLastName": "Snow",
        "userMiddleName": "null",
        "roleName": "Moderator",
        "roleID": "1"
      }
    ]
  }
}
3
  • Have you written any server side code to handle such a request? If you just have a GET "/records" route on the server side then its not going to know what to do when it sees "/records/record?userID=2" Commented Feb 24, 2018 at 20:37
  • Okay so I'm not having any server side code. Just hoping if I could do a plain get request. @EndaMolloy Commented Feb 24, 2018 at 20:38
  • Unfortunately your server side is not going to magically know what to do. You can either write server side logic to handle the query string if you want to persist with such a route or simply have your /records route which returns the full object and then filter out what you want from there. Commented Feb 24, 2018 at 20:52

3 Answers 3

2

Just so slightly change your JSON structure:

{
  "records": [
    {
      "userID": "2",
      "userFirstName": "Arya",
      "userLastName": "Stark",
      "userMiddleName": "null",
      "roleName": "Admin",
      "roleID": "1"
    },
    {
      "userID": "1",
      "userFirstName": "John",
      "userLastName": "Snow",
      "userMiddleName": "null",
      "roleName": "Moderator",
      "roleID": "1"
    }
  ]
}

Then http://localhost:3000/records?userID=2 returns

[
  {
    "userID": "2",
    "userFirstName": "Arya",
    "userLastName": "Stark",
    "userMiddleName": "null",
    "roleName": "Admin",
    "roleID": "1"
  }
]

Aside from this solving the actual access problem I was also unable to find a solution to filter based on object properties that are part of an array which is stored in a property of your db.json object.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. Finally I've had my actual json file modified as I couldn't find any resource help online.
1

Simple but have you tried just using /records/record/2 the json-server module takes most of the implementation away from the user and this may just work, as it’s designed to be a quick REST API to get people started.

Check out the examples on the documentation page or take a look at the API running here http://jsonplaceholder.typicode.com/ as it’s using the same code.

1 Comment

Unfortunately, I have tried this way as well but it did not work.
0

You have to change something on your server side to filter request according to the request parameters

Or just use javascript:

data.records.record.filter((user)=>user.userID==2)[0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.