0

I have JSON files on my server that needs to be passed into several different Raspberry Pis running Debian. Each of the Pis has their own JSON feed that they will pull from, but essentially, I need to automatically take the value of one key-value pair and use it as an argument for a command that is run in the terminal.

For instance: Fetching https://www.example.com/api/THDCRUG2899CGF8&/manifest.json

{
   "version": "1.5.6",
   "update_at": "201609010000",
   "body": "172.16.1.1"
}

Then that value would be dynamically output into a command that uses the body's value as an argument. EG: ping [body value]

Edit:

The point of this is to have a task that executes every minute to receive weather updates to it.

montelc0
  • 101
  • 9
  • 2
    Possible duplicate of [Unix command-line JSON parser?](http://stackoverflow.com/questions/3858671/unix-command-line-json-parser) – kostix Aug 22 '16 at 16:32
  • 1
    Well, the subject is the question is misleading: what you actually want is a command-line JSON parser which would allow you to extract the value of a particular field of the top-level JSON object. (In a more general case, that could be some arbitrarily nested object.) Hence take a look at [this](http://stackoverflow.com/q/3858671/720999). I'd start with the `jq` tool (packaged for Debian, FWIW). – kostix Aug 22 '16 at 16:34

1 Answers1

1

You are looking for command substition, specifically wrapped around a command that can extract values from a JSON value. First, you can use jq as the JSON-processing command.

$ jq -r '.body' tmp.json
172.16.1.1

Command substitution allows you to capture the output of jq to use as an argument:

$ ping "$(jq -r '.body' tmp.json)"
PING 172.16.1.1 (172.16.1.1): 56 data bytes
...
chepner
  • 497,756
  • 71
  • 530
  • 681