8

https://paw.cloud/

I have the following JSON coming back from a different API end point:

[
  {
    "id": 1,
    "name": "BigCartel",
    "slug": "bigcartel",
    "logo_cdn_url": "http://placehold.it/200x200",
    "active": true,
    "authentication_type": {
      "description": "Oauth Authentication Token",
      "slug": "oauthauthenticationtoken"
    }
  },
  {
    "id": 2,
    "name": "Lightspeed Retail",
    "slug": "lightspeed_retail",
    "logo_cdn_url": "http://placehold.it/200x200",
    "active": true,
    "authentication_type": {
      "description": "Oauth Authentication Token",
      "slug": "oauthauthenticationtoken"
    }
  }
]

I would like to parse this JSON and use it in another section of the paws application. Has anyone found any examples like this? I was trying the custom JS text but that appears to be a dead end.

halfer
  • 19,824
  • 17
  • 99
  • 186
Chris Hough
  • 3,389
  • 3
  • 41
  • 80
  • 1
    I guess you're trying to parse this JSON to inject it elsewhere in the middle of another JSON (in a request body)? Or I you trying to just send exactly the same? The first will be a little hard to achieve, but have some workaround. The second is easy. Let me know so I can post an adapted answer. Thx! – Micha Mazaheri Sep 08 '16 at 07:00
  • yes, I am trying to parse this json request for the id, example `bigcartel` and use it in another url i.e. "/api/providers/1/authentication/customers/" where I would like to replace the number 1 with the value parsed – Chris Hough Sep 08 '16 at 08:01
  • thank you for your help @MichaMazaheri – Chris Hough Sep 08 '16 at 08:02
  • @MichaMazaheri did you have an example? – Chris Hough Sep 08 '16 at 23:23
  • I think the environments can help you here: https://paw.cloud/docs/environments/environments it's not exactly what you're looking for but it should be helping. We'd love to add proper support for path parameters next :) It's definitely on the roadmap. – Micha Mazaheri Sep 09 '16 at 19:52
  • I have environments setup. I am looking for a way to parse the JSON responses to find ids based on a string parameter. So if `bigcartel` is id 1 one run, but later becomes id 6 I do not have to rewrite by calls. Is that possible @MichaMazaheri – Chris Hough Sep 09 '16 at 19:54
  • Ok I think I understand the question now! Sorry I wasn't on the right track so far. Seems like you added a nice bounty ;) I'll publish a complete answer then :D – Micha Mazaheri Sep 11 '16 at 00:01

1 Answers1

4

Solution 1: jq

According to their website, "jq is a lightweight and flexible command-line JSON processor". And you can do jq queries in Paw. We will use it to automatically extract the ID of the field from your latest response.

On the URL field (where you want to have this "smart ID"), right-click and pick "jq JSON processor".

Pick jq JSON processor in Paw to extract fields from jq

In the "JQ args" field, enter the query (see jq tutorial for details on how this works):

.[] | select(.slug == "bigcartel") | .id

In the JSON input field, right-click and pick Response > Response Raw Body. A popover will open, point the "Request" field to the request from which you want to extract the response body from (your "list" request). This will automatically fetch the body of the latest response of this request.

All done! You should now have this setup:

Filter with jq the JSON body of an HTTP response in Paw

Solution 2: JavaScript Snippet

Paw exposes JavaScript bindings to write extensions (e.g. jq dynamic value used above is written as an extension). It can also be used to embed little code snippets inline a request. It's helpful to achieve more advanced setups.

Right-click on the URL field where you need to insert your ID. Pick Extensions > JS Script. In the popover, paste this code:

function evaluate(context){
    var request = context.getRequestByName("List");
    var httpExchange = request.getLastExchange();
    var body = JSON.parse(httpExchange.responseBody);
    for (var i = 0; i < body.length; i++) {
        var member = body[i];
        if (member.slug == "bigcartel") {
            return member.id;
        }
    }
    return null;
};

You can find the docs of this JavaScript API that Paw exposes in the Paw documentation under the "API Reference" section at the bottom.

Micha Mazaheri
  • 3,481
  • 1
  • 21
  • 26