DEV Community

Cover image for A new way to reduce JSON API output without all the hassles of graphql | Restmap
Saksham Khurana
Saksham Khurana

Posted on

A new way to reduce JSON API output without all the hassles of graphql | Restmap

To create a simple way to query only the required data from a Rest API without relying on a lot of external tools or code.


What's all the fuss about?

The problem is, Frontend devs have to constantly deal with lots of data in each API request & they have no way of limiting this response according to their needs. So consider they only need 1 key, they still have to get all the keys in the response.

The earliest solution and still a very good solution is using, GraphQl which is a query language for your API (read more here).

So have you created a GraphQl alternative?

NO !!!

In no way is Restmap a replacement for GraphQl which is a very extensive query language that does a lot of things.

Restmap solely aims to limit your data without lots of development time on fronend/backend. All you need is just few lines of code on server side

  • Graphql requires you to configure/change a lot of your frontend & backend.
  • For Graphql in frontend especially you need a new way to consume data by configuring a new library other than the existing REST API which put additional strain on frontend devs.

Restmap simply plugs & play in your existing system !!!


Sounds Good? Well let me introduce you to Restmap

  • Restmap simply works in the existing API by making a slight tweak on the server-side
  • Just import the library and call the reduceData method on the final output of your API
  • Clients can send restmap string via header, query, or body (however you wish to have it on the server-side)

Do you want examples?

{
    "rest" : {
        "query: {
            "lang" : "",
            "map" : "",
            "name" : "",
            "age" : 2
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Now if I just want 2 keys inside query, we can write a special restmap string

{rest{query{lang,map}}}

which will return only the required data

{
    "rest" : {
        "query: {
            "lang" : "",
            "map" : ""
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Example 2 (escape)

{
    "rest" : {
        "query": {
            "lang" : "",
            "map" : "",
            "name" : "",
            "age" : 2,
            "something" : {
                "good": true,
                "is" : true,
                "here" : true
            }
        },      
    }
}
Enter fullscreen mode Exit fullscreen mode

What is we need to remove just 1 or 2 keys and return all other keys.
Suppose I need all keys inside rest.query except lang & map while also reducing rest.query.something

{rest{query{-lang,-map,something{good}}}}
- marks the key as an escape key (you can also customize it; read more below)

which will return only the required data

{
    "rest" : {
        "query: {
            "name" : "",
            "age" : 2,
            "something" : {
                "good": true
            }
        },      
    }
}
Enter fullscreen mode Exit fullscreen mode

You can also test out more on our Playground


So how to get started, properly?

For that, you have to check out the github profile to learn how to use this in your existing system.

GitHub logo restmap / restmap-node

Simple way to query only the required data from a Rest API without relying on lot of external tools or code

restmap

logo

npm ts CI codecov Buy us a tree NPM Conventional Commits

Goal

To create a simple way to query only the required data from a Rest API without relying on lot of external tools or code.

This project is inspired from GraphQl

But unlike graphql which is itself a whole new query language, restmap aims to create a much simpler approach (for reducing data) where you just write a simple string which is used to parse and minify the data.

Why replace GraphQl ?

In no way restmap is replacement for GraphQl which is a very extensive query language that do a lot of things.

Restmap solely aims to limit your data without lots of development time on fronend/backend. All you need is just few lines of code on server side

  • Graphql requires you to configure/change a lot of your frontend & backend.
  • For Graphql in frontend specially you need a new way to consume data by configuring a new…

Thanks for checking out this article, hope it helps you :)

Top comments (0)