Ad Widget

Collapse

Easy way of parsing a json blob from api endpoint?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen Wood
    Member
    • Feb 2012
    • 43

    #1

    Easy way of parsing a json blob from api endpoint?

    I have an endpoint that spits out an array of json blobs on a single line, in random order. It looks like this:
    Code:
    [{"name":"machine3","status":"up","processes":"2000"},{"name":"machine2","status":"up","processes":"1322"},{"name":"machine5","status":"down","processes":"0"}.... etc]
    The machines are displayed in no particular or reliable order in the array, and the values are in a randomish order within the pair themselves.

    What's the best way to separate these values reliably?

    Currently what I'm doing is using the zabbix server to hit the api endpoint and digest the data with shell-fu:

    Code:
    system.run[echo $(curl endpoint.api) | tr '}' '\n' | grep 'machine2..... etc]
    This works by breaking the json into newlines, where I can then do various greps and awks to get the values for different keys.

    I'd like to call the api once and run regular expressions against the string, but there doesn't seem to be the notion of group matching and printing, which makes it impossible to grab the values.

    Anyone have any ideas on how I can accomplish this?
  • MaxM
    Member
    • Sep 2011
    • 42

    #2
    Any reason you wouldn't call an external script to run an api call, properly parse the output, and then return to zabbix? Shell is absolutely terrible for grok'ing through json, and there are dozens of good api's for perl/python/php. The python library found here is what I have used a lot of times in the past:

    Source code: Lib/json/__init__.py JSON (JavaScript Object Notation), specified by RFC 7159(which obsoletes RFC 4627) and by ECMA-404, is a lightweight data interchange format inspired by JavaScript...

    Comment

    Working...