Giter VIP home page Giter VIP logo

json-server's Introduction

json-server

A simple server that accepts JSON POSTs and allows querying the content with GET.

The server timestamps each JSON value it receives. Requests can specify results within a certain date/time range.

POST

Every POST received is expected to contain a valid JSON value. Invalid JSON is rejected with a 400; valid JSON is accepted with a 201.

All POSTed values are saved indefinitely (as far as the server knows).

GET

A simple naked '/' (just the host and port) will return "JSON Server" with some other fixed text telling how to form queries.

The query portion of a URL begins with '?'. There may later be query subdivisions, but for now there is no path portion of the URL.

Queries always return a JSON array as a UTF-8 string. If a query has no result, it returns "[]". If there is a query syntax error, an error object is returned:

{
  "json-query-error": {
    "query": "query as received",
    "where": "     ^ indicates position",
    "what": "nature of error",
    ....
  }
}

We will use this JSON value in query examples below:

{ "store": {
    "name": "Borders",
    "address": "761 Sunset Drive",
    "city": "San Francisco",
    "state": "CA",
    "book": [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

NOTHING BELOW THIS IS IMPLEMENTED YET

For a simple example,

localhost:8081?json=store

will return all objects in the store that have an outer key "store". When json is the first query component, "json=" may be omitted.

localhost:8081?store.name:"Borders"

will return at least the JSON value above.

From now on we will omit the host:port part of the URL in example queries.

?store.book.author:author?select=author

The use of a name instead of a literal after : binds the name to the value. The above will return an array like:

["Nigel Rees","Eveln Waugh","Herman Melville","J. R. R. Tolkien"]

Dicey example:

?store?select=store.name:n,store.book.category?groupby=n

Vacuous array levels are omitted, so the result is:

[ "Borders", [ "reference", "fiction" ] ]

If there were two stores, however, the result would be:

[
  [ "Borders", [ "reference", "fiction" ] ]
  [ "Barnes & Noble", [ "reference", "fiction", "popup" ] ]
]

Query Components

Valid query components are:

?json=query (json= may be omitted if first)
?select=names (comma-separated list of names, which are the "columns" of each row)
?from=datetime (starting datetime)
?to=datetime (ending datetime)
?date=datetime (this date, may be partially specified to indicate a range)

json-server's People

Contributors

bobfoster avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.