Using json in bash :

  • jsawk : work with an array of JSON objects read from stdin, filter them using JavaScript to produce a results array that is printed to stdout. For example get’s json ( from Yahoo image search by ‘madonna’ query ) and add some info ( madonna’s real name ) to this
  • wget http://bokov.net/json_example.json
    cat json_example.json | ./jsawk "this.ResultSet.RealName=\"Louise Ciccone\"" > updated_info.json

  • you can use some bash/awk/sed stuff to parse json. For example – list all image urls from json
  • wget http://bokov.net/json_example.json
    cat json_example.json | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | grep '"ClickUrl":' | sed 's/:/ /1' | awk -F" " '{ print $2 }'

  • or use library from json.org for language which you like :-)

via v.kruchkov

2 Comments

  1. Ezhilraj says:

    How to get the values of applications from below JSON object using grep
    I tried with grep applications.?
    But it does not working
    {
    “id”: “145ert678fa9”,
    “aliasName”: null,
    “applications”: [
    “APPLICATIONS_1”,
    “APPLICATIONS_2”,
    “APPLICATIONS_3”
    ],
    “businessId”: “270871”,
    “vendorName”: “Testing for applicaitons”,
    “availability”: “ACTIVE”,
    “code”: “CN”,
    “countryName”: “India”,
    “updatedId”: null,
    “emailAddress”: null,
    “faxNumber”: null,
    “latitude”: null,
    “legalizationId”: null,
    “actualCompanyId”: “e254wtr543”
    }

    [Reply]

    Alexey Bokov Reply:

    Please use sed/awk filtering as I described or also jsawk may help here – please take a look on this, there’s some samples : https://github.com/micha/jsawk
    Hope it helps,
    Alexey

    [Reply]

Leave a Reply