0

js gurus on windows,

I send some curl request via shelljs and get some JSON back.

example JS-Code

var sh = require('shelljs');
sh.exec('curl -H "Accept: application/vnd.github.v3.text-match+json" \\ ' +
          'https://api.github.com/repos/jquery/jquery/tags ',
          {silent:true}).output;

Now, I would like to parse JSON with jsawk. My thoughts are right similar like Bash shell extract object from json file and How to parse json with shell scripting.

My Question is how can I install jsawk inside of node_modules folder structur (maybe .bin folder) and run as additional statement at sh.exec?

Has somebody an idea?

Another way might be to grep and awk the JSON data. I want to check the name properties, find correct version number (e.g. 2.1.1) and call zipball_url. But I'm not good as should to do this via shell basics.

Best, Ronn

Community
  • 1
  • 1
Ronny Springer
  • 199
  • 1
  • 5
  • 16

1 Answers1

0

Just use npm:

npm install jsawk

It auto install jsawk in node_modules and then you can use it in your script:

var jsawk = require('jsawk');

But why are you using curl from shell, there are better ways to get http data, the basic is http request

var https = require('https');
var options = {
  host: 'api.github.com',
  path: '/repos/jquery/jquery/tags',
  port: 443,
  method: 'GET',
  headers: {'User-Agent': 'nodejstestagent'}
};

var req = https.get(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));

  // Buffer the body entirely for processing as a whole.
  var bodyChunks = [];
  res.on('data', function(chunk) {
    // You can process streamed parts here...
    bodyChunks.push(chunk);
  }).on('end', function() {
    var body = Buffer.concat(bodyChunks);
    var jsonResponse = JSON.parse( body) ;
    console.log( JSON.stringify( jsonResponse,null,3 ));
  })
});

req.on('error', function(e) {
  console.log('ERROR: ' + e.message);
});

Will output something like this:

[
   {
      "name": "2.1.1-rc2",
      "zipball_url": "https://api.github.com/repos/jquery/jquery/zipball/2.1.1-rc2",
      "tarball_url": "https://api.github.com/repos/jquery/jquery/tarball/2.1.1-rc2",
      "commit": {
         "sha": "c2fdcaaacd4d7f8479b2196525330c1738e30cd3",
         "url": "https://api.github.com/repos/jquery/jquery/commits/c2fdcaaacd4d7f8479b2196525330c1738e30cd3"
      }
   },
   {
      "name": "2.1.1-rc1",
      "zipball_url": "https://api.github.com/repos/jquery/jquery/zipball/2.1.1-rc1",
      "tarball_url": "https://api.github.com/repos/jquery/jquery/tarball/2.1.1-rc1",
      "commit": {
         "sha": "6ba4c8def1f9b0d03c5e8de1d64a78ae36646fb6",
         "url": "https://api.github.com/repos/jquery/jquery/commits/6ba4c8def1f9b0d03c5e8de1d64a78ae36646fb6"
      }
   },

Updated for https and github rule for User-Agent

Adrian Toma
  • 547
  • 2
  • 13
  • Oui, totally other way that is working! Nice job, thanks. I'll focus on your advice. I leave this question open 'cause _'how to run jsawk as statement at shell command via shell.js'_ is still not solved. – Ronny Springer May 22 '14 at 11:46
  • Glad to help! about jsawk, it's a bit tricky, because windows does not recognize #!/path/to/interpretor like in linux, hence you can't run directly node_modules\jsawk\bin\jsawk witout some tweaks in windows variables, here is a similar issues with php http://stackoverflow.com/questions/16646003/windows-equivalent-to-usr-bin-php – Adrian Toma May 22 '14 at 11:56
  • Set an env var is a possible way to work with jsawk. It can be unhandy for non-technical user or a automated build maschine. Someone has to register this variable manually. – Ronny Springer May 23 '14 at 10:05