1

I am using json-server to push json data as REST api.

Server returns (above) for get request like this:

http://localhost:3000/items?itemid=534

return:

[
  {
    "itemid": 534,
    "type": "textcontent",
    "icon": "http://exampple.com/1png",
    "title": "John",
    "description": "Description",
    "url": "http://example.net/project/doSomething"
  }
]

return is JSON array, but not JSON object.

What should i do, to return json object

Data file is: data.json

{
  "items": [
    {
      "itemid": 534,
    "type": "textcontent",
    "icon": "http://exampple.com/1png",
    "title": "John",
    "description": "Description",
    "url": "http://example.net/project/doSomething"
    },
    {
      "itemid": 234,
    "type": "textcontent",
    "icon": "http://exampple.com/2png",
    "title": "Smith",
    "description": "Description2",
    "url": "http://example.net/project/doSomething2"
    }
    ]
}
Emin Hasanov
  • 1,299
  • 1
  • 13
  • 29

3 Answers3

1

If you change your data to be like this:

{
  "items": [
    {
      "id": 534,
      "type": "textcontent",
      "icon": "http://exampple.com/1png",
      "title": "John",
      "description": "Description",
      "url": "http://example.net/project/doSomething"
    },
    {
      "id": 234,
      "type": "textcontent",
      "icon": "http://exampple.com/2png",
      "title": "Smith",
      "description": "Description2",
      "url": "http://example.net/project/doSomething2"
    }
  ]
}

Then you can access the individual items with urls like this: http://localhost:3000/items/534

Sami Siren
  • 11
  • 1
0

It is very simple. All you need to do is map the data once you have it on the client. I have used the Rx.Observable.from method but this will work with another other methods like fromPromise.

const data = [{
    "itemid": 534,
    "type": "textcontent",
    "icon": "http://exampple.com/1png",
    "title": "John",
    "description": "Description",
    "url": "http://example.net/project/doSomething"
  }
];

const data$ = Rx.Observable.from(data)
  .map(arr=> {
     return JSON.stringify({
       "items": arr
     });
  });

 data$.subscribe(x=> console.log(x));
 // this will print out something like this
//{"items":{"itemid":534,"type":"textcontent","icon":"http://exampple.com/1png","title":"John","description":"Description","url":"http://example.net/project/doSomething"}}

Also note that you could just return {'items':arr} if you just need an object after the fact.

jamesRH
  • 420
  • 3
  • 12
0

I assume this is the desired behaviour for the URL scheme you are using. The server should only return a single object if you use an URL identifying a single resource.

When you are using query params, your request says "please return all matching items that have an item_id of 534". This means that json-server will of course return an array of items. There is no special knowledge that this itemid is a unique one. If you want to specify one special item, you should use the URL of the specific item which should look somehow like http://localhost:3000/items/534.

This post explains the concepts pretty good: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

Update

I got your question wrong. You want to have all resulting data contained under one special key instead of the array value directly. Looking into the docs of json-server it seems to be easily possible to extend the existing behaviour. Could the router.render() function help you achieving your goals? https://github.com/typicode/json-server#module

router.render = function (req, res) {
  res.jsonp({
    yourkey: res.locals.data
  })
}

If you want the key to match the type of resource you are querying, you probably have to take a look in the req object to find out which entity type was requested.

Andreas Jägle
  • 11,632
  • 3
  • 31
  • 31
  • Thanks @ajaegle, but i need that response for all items should return not as an object – Emin Hasanov Mar 01 '16 at 22:13
  • Your answer is usefull, but i found solution with .`map(res => JSON.parse(res.text))` `JSON.parse` helps to parse this object. If you consideryour post must be marked as an aswer, i am ready – Emin Hasanov Mar 02 '16 at 05:58