The jsonextfilter package

[Tags:mit, program]

Filter select values in JSON objects to unix programs


[Skip to Readme]

Properties

Versions 0.1.0.0
Dependencies aeson, attoparsec, base (<5), bytestring, containers, monads-tf, optparse-applicative, process, scientific, string-qq, text, unordered-containers, vector [details]
License MIT
Author Daniel Choi
Maintainer MackeyRMS
Category Web
Home page https://github.com/mackeyrms/jsonextfilter#readme
Bug tracker https://github.com/mackeyrms/jsonextfilter/issues
Source repository head: git clone https://github.com/mackeyrms/jsonextfilter
Uploaded Tue Mar 21 03:03:31 UTC 2017 by mackeyrms
Distributions NixOS:0.1.0.0
Downloads 38 total (5 in the last 30 days)
Votes
0 []
Status Docs not available [build log]
Last success reported on 2017-03-21 [all 3 reports]
Hackage Matrix CI

Downloads

Maintainer's Corner

For package maintainers and hackage trustees

Readme for jsonextfilter

Readme for jsonextfilter-0.1.0.0

jsonextfilter

Applies external unix filters to specified key paths of a JSON object stream.

Example

example.json:

{
  "title": "Terminator 2: Judgment Day",
  "year": 1991,
  "stars": [
    {"name": "Arnold Schwarzenegger"},
    {"name": "Linda Hamilton"}
  ],
  "ratings": {
    "imdb": 8.5
  },
  "description":"<p>Some <strong>HTML</strong></p>"

}
{
  "title": "Interstellar",
  "year": 2014,
  "stars": [
    {"name":"Matthew McConaughey"},
    {"name":"Anne Hathaway"}
  ],
  "ratings": {
    "imdb": 8.9
  },
  "description":"<p>Some <strong>more HTML</strong></p>"
}

We want to transform the "description" fields from HTML to plain text:

jsonextfilter 'elinks -dump'  'description' < example.json  | jq -M '.' 

Output:

{
  "ratings": {
    "imdb": 8.5
  },
  "stars": [
    {
      "name": "Arnold Schwarzenegger"
    },
    {
      "name": "Linda Hamilton"
    }
  ],
  "year": 1991,
  "title": "Terminator 2: Judgment Day",
  "description": "   Some HTML\n"
}
{
  "ratings": {
    "imdb": 8.9
  },
  "stars": [
    {
      "name": "Matthew McConaughey"
    },
    {
      "name": "Anne Hathaway"
    }
  ],
  "year": 2014,
  "title": "Interstellar",
  "description": "   Some more HTML\n"
}

More than one keypath can be specified in the keypaths argument string. Separate keypaths with spaces. The external filter will be applied to all of them, e.g.

jsonextfilter 'elinks -dump'  'description review' < example.json  | jq -M '.' 

Currently only ONE external filter command can be given. If you want to apply a pipeline of commands, wrap it in a bash script.

The external filter will only be applied to STRING values. Number and boolean values are untouched.

Preconditions

You can designate a pre-filter to determine whether the main filter should run depending on the exit code of the pre-filter:

jsonextfilter -p 'grep -q more' 'elinks -dump' description  < example.json   | jq '.' -M

This causes elinks -dump to be applied only to "description" values that contain the string "more":

{
  "ratings": {
    "imdb": 8.5
  },
  "stars": [
    {
      "name": "Arnold Schwarzenegger"
    },
    {
      "name": "Linda Hamilton"
    }
  ],
  "year": 1991,
  "title": "Terminator 2: Judgment Day",
  "description": "<p>Some <strong>HTML</strong></p>"
}
{
  "ratings": {
    "imdb": 8.9
  },
  "stars": [
    {
      "name": "Matthew McConaughey"
    },
    {
      "name": "Anne Hathaway"
    }
  ],
  "year": 2014,
  "title": "Interstellar",
  "description": "   Some more HTML\n"
}