Getting specific parts of json with Regex

Using Google Vision (CURL through a shell script) I’m pulling in json so that I can capture values to crop images in photoshop.
I’m having a difficult time grabbing the values I need.

The data returned looks like this:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed

0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  541k    0     0  100  541k      0  3419k --:--:-- --:--:-- --:--:-- 3408k
100  543k    0  1838  100  541k   3805  1121k --:--:-- --:--:-- --:--:-- 1121k
{
 "responses": [
  {
  "localizedObjectAnnotations": [
    {
      "mid": "/m/07mhn",
      "name": "Pants",
      "score": 0.9494914,
      "boundingPoly": {
        "normalizedVertices": [
          {
            "x": 0.3794051,
            "y": 0.49376252
          },
          {
            "x": 0.623296,
            "y": 0.49376252
          },
          {
            "x": 0.623296,
            "y": 0.96466535
          },
          {
            "x": 0.3794051,
            "y": 0.96466535
          }
        ]
      }
    },
    {
      "mid": "/m/03gx245",
      "name": "Top",
      "score": 0.93232113,
      "boundingPoly": {
        "normalizedVertices": [
          {
            "x": 0.33775452,
            "y": 0.08114735
          },
          {
            "x": 0.6737402,
            "y": 0.08114735
          },
          {
            "x": 0.6737402,
            "y": 0.54397774
          },
          {
            "x": 0.33775452,
            "y": 0.54397774
          }
        ]
      }
    },
    {
      "mid": "/m/01g317",
      "name": "Person",
      "score": 0.850009,
      "boundingPoly": {
        "normalizedVertices": [
          {
            "x": 0.32060996,
            "y": 0.013396601
          },
          {
            "x": 0.6699771,
            "y": 0.013396601
          },
          {
            "x": 0.6699771,
            "y": 0.9879022
          },
          {
            "x": 0.32060996,
            "y": 0.9879022
          }
         ]
        }
       }
      ]
     }
    ]
  }

My end goal is to say something like

if name = "pants" then
//return the X and Y vertices for the pants object
//do some math to turn them into actual coordinates that I can then send to Photoshop
end if

I guess I’m just unsure on how to achieve this in Xojo.
My regex pattern of ("normalizedVertices"\s*:\s*(?:\[[^]]*]|[^,}]+)) grabs every chunk of coordinates. Great.
Also, (?:"name"\s*:\s*)("Pants") gives me the word “Pants”.
Alright…

Where do I go from here? How can I make the connection between “Pants” and the 4 sets of vertices that correspond to it?

Start with Xojo.ParseJSON.

It’s technically not a json object, but I’m treating it as one.
It’s a CURL response. So it’s actually a string if I’m not mistaken?

What you posted here is definitely JSON, which Xojo will parse for you faster than Regex.

JSON is just a specially-formatted string. What you posted above is JSON.

I’d recommend ParseJSON too, then walk the resulting Dictionary/Array.

1 Like

I edited the response I get back. Will that fly as valid json data?

Any reason you’re using CURL instead of URLConnection? I forget the syntax though, if you really want to use CURL, there should be parameters to silence the status info.

2 Likes

No reason other than that is what I used for a Python/Apple Script combo as a proof of concept before turning to Xojo for the final product. I will look into those parameter. I wasn’t aware of that. Thanks!

Good call. Thank you.

It’s -s or --silent, but I second using URLConnection.

2 Likes