json-server
json-server copied to clipboard
How to access the nested data from json data using json-server.
How to access the nested data from json data using json-server.
This is my json data { "flightDetails": [ { "id": 1, "flight_name": "INDIGO", "flight_departs": "8:00", "flight_arrives": "9:00", "flight_from": "New", "flight_to": "Hyderabad", "flight_date": "13/6/2023", "flight_img": "/images/airindia.jpg", "flight_classes": [ "Business", "Premium economy", "first", "economy" ],
"services": [ { "id": 2, "service_name": "seat allocation", "types": [ { "Seat": "Extra Leg Room", "cost": "rs/- 1500" }, { "Seat": "Front of Aircraft", "cost": "rs/- 1000" } ] }, { "id": 1, "service_name": "Baggage", "types": [ { "Baggage_type": "Overweight", "Baggage_limit": "0-3kgs", "Baggage_cost_1kg": "rs/-200" }, { "Baggage_type": "Checked Bags ", "Baggage_limit": "15kgs", "Baggage_cost_1kg": "rs/-390" } ] }, { "id": 3, "service_name": "Baggage", "types": [ { "Baggage_type": "Overweight", "Baggage_limit": "0-3kgs", "Baggage_cost_1kg": "rs/-200" }, { "Baggage_type": "Checked Bags ", "Baggage_limit": "15kgs", "Baggage_cost_1kg": "rs/-390" } ] } ], "passenger": [ { "id": 1, "passenger_seat_number": "20A", "passenger_name": "RAGAM ARCHANA", "passenger_address": "Hyd,Telangana", "passenger_passport_details": { "Citizen": "India", "place": "Hyd,Telangana", "date_of_birth": "03-08-2000", "expire_date": "24/07", "gender": "F" } } ] } ] }
Originally posted by @archana-ragam in https://github.com/typicode/json-server/issues/359#issuecomment-1590898936
I believe this is duplicate of https://github.com/typicode/json-server/issues/608 and https://github.com/typicode/json-server/issues/1261
@archana-ragam Accessing nested data in a JSON structure using json-server can be approached in several ways, depending on what specific data you want to retrieve.
-
Accessing Nested Objects To access nested objects, you need to use the appropriate path. For example, to access the services for the first flight: http://localhost:3000/flightDetails/1/services
-
Filtering Nested Data To filter nested data, you can use query parameters. However, json-server has limited capabilities in deeply nested filtering. For simple top-level filtering, such as getting all flights from a specific location: http://localhost:3000/flightDetails?flight_from=New
-
Accessing Deeply Nested Data For deeply nested data, like accessing specific types within services, json-server's default functionality might not suffice. One workaround is to retrieve the entire object and then programmatically filter the data in your application.
For example, get all flight details, then in your application code, filter to the specific service_name or types: fetch('http://localhost:3000/flightDetails') .then(response => response.json()) .then(flightDetails => { // Process and filter the flightDetails here as needed });