How to parse a JSON content

Hallo everybody.

I am trying to shorten a link with bit.ly API.

The content I get back is the following:

The screenshot show the result in Firefox, but I have it into a KM variable with the GET URL action.

How do I extract the item data / url, i.e. the shortened URL and assign it to a variable?

Thank you and have a nice day.

Hi @tsolignani,
Please post the data you got so that others don't have to type what you got and can test the macro result before uploading to the forum for you.

1 Like

Thank you for your reply. I hadn't thought of that. There you go:

{"status_code":200,"status_txt":"OK","data":{"url":"https://bit.ly/3slXmJE","hash":"3slXmJE","global_hash":"3soxCMH","long_url":"https://blog.solignani.it/2021/12/21/sinistro-stradale-dopo-quanto-va-in-prescrizione/","new_hash":0}}https://blog.solignani.it/2021/12/21/sinistro-stradale-dopo-quanto-va-in-prescrizione/

Posted code can be protected from wiki-software display-jumbling if you post it between a pair of backspace triples

```
{
  "status_code": 200,
  "status_txt": "OK",
  "data": {
    "url": "https://bit.ly/3slXmJE",
    "hash": "3slXmJE",
    "global_hash": "3soxCMH",
    "long_url": "https://blog.solignani.it/2021/12/21/sinistro-stradale-dopo-quanto-va-in-prescrizione/",
    "new_hash": 0
  }
}
```

and you can access any part of a JSON-formatted variable with some special tokens described at:

manual:JSON [Keyboard Maestro Wiki]

(i.e. the parsing is done for you automatically by Keyboard Maestro)

e.g.

JSON parts with KM variable subscripts.kmmacros (4.6 KB)

3 Likes

Hi @tsolignani,
When you post data, enclose it with the block coding marks:
```

put your data here

```

Like this:

{
  "data" : {
    "global_hash" : "3soxCMH",
    "hash" : "3slXmJE",
    "long_url" : "https:\/\/blog.solignani.it\/2021\/12\/21\/sinistro-stradale-dopo-quanto-va-in-prescrizione\/",
    "new_hash" : 0,
    "url" : "https:\/\/bit.ly\/3slXmJE"
  },
  "status_code" : 200,
  "status_txt" : "OK"
}

Otherwise, the forum will interpret some signs as markdown syntax and your data is broken, as is shown in your post.

Here is the solution:

JSON - Read JSON Value.kmmacros (6.1 KB)

Click to see screenshot

1 Like

Thank you all for both the solutions and the other advices, as always I got rich answers!

Great community for a marvellouse software.

Have a nice day and a Happy Christmas.

Thanks.

Tiziano

1 Like