You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过URL访问Mock JSON Server中JOBS数组的Parcels子节点?

Accessing Parcels Sub-Nodes in Mock JSON Server

Hey there! Nice to see you leveraging Mock JSON Server for your API testing—let's walk through exactly how to access those Parcels sub-nodes using URL-based queries, just like you do with the type filter.

Since Mock JSON Server is built on top of json-server, it supports a set of handy built-in parameters and routing rules for nested data. Here are the most common use cases based on your needs:

1. Get Parcels for a single specific Job

If you know the Id of the Job you want to target, use nested routing to directly fetch its Parcels array. For example, with your sample data where the Job has Id: "1", use this URL:

localhost:3000/JOBS/1/Parcels

This will return only the Parcels array for that Job:

[
  { "parcelId": "90" }
]

2. Get Parcels for filtered Jobs (e.g., type=open)

If you want to fetch Parcels only for Jobs that match a filter (like type=open), combine the filter with the _fields parameter to isolate the Parcels data. Use this URL:

localhost:3000/JOBS?type=open&_fields=Parcels

The response will be an array of Job objects, but each object will only contain the Parcels field:

[
  {
    "Parcels": [{"parcelId": "90"}]
  }
]

3. Combine Parcels from filtered Jobs into a single array

Mock JSON Server doesn't have a built-in way to flatten nested arrays directly via URL, but you can easily handle this on the frontend. After fetching the data from the URL above, loop through the results and merge all Parcels arrays into one:

// Example frontend code (JavaScript)
fetch('localhost:3000/JOBS?type=open&_fields=Parcels')
  .then(res => res.json())
  .then(data => {
    const allParcels = data.flatMap(job => job.Parcels);
    console.log(allParcels); // Output: [{ "parcelId": "90" }]
  });

Bonus: Custom Routes (for cleaner URLs)

If you find yourself using the same filtered Parcels query often, set up a custom route in json-server to simplify the URL. Create a routes.json file with:

{
  "/open-jobs-parcels": "/JOBS?type=open&_fields=Parcels"
}

Then start your server with the routes file:

json-server --watch your-db-file.json --routes routes.json

Now you can use localhost:3000/open-jobs-parcels to get the filtered Parcels data directly.


内容的提问来源于stack exchange,提问作者PremKumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:39:31