简体   繁体   中英

How to pretty print a Github Webhook payload from shell?

I have a CGI Webhook end point that simply mails the $POST to me.

I'm now looking to pretty print the JSON so I can easily read the email:

payload=%7B%22ref%22%3A%22refs%2Fheads%2Fmaster%22%2C%22after%22%3A%2255deef96327f0625b5f1f724636a84ded63540c3%22%2C%22before%22%3A%22c87b66be0d2bcc93be6de0f0c4ac248642d5abf5%22%2C%22created%22%3Afalse%2C%22deleted%22%3Afalse%2C%22forced%22%3Afalse%2C%22compare%22%3A%22https%3A%2F%2Fgithub.com%2Fhackerspacesg%2Fhackerspace.sg%2Fcompare%2Fc87b66be0d2b...55deef96327f%22%2C%22commits%22%3A%5B%7B%22id%22%3A%2255deef96327f0625b5f1f724636a84ded63540c3%22%2C%22distinct%22%3Atrue%2C%22message%22%3A%22Harmonising+directory+structure+for+punch%22%2C%22timestamp%22%3A%222013-11-25T23%3A27%3A01-08%3A00%22%2C%22url%22%3A%22https%3A%2F%2Fgithub.com%2Fhackerspacesg%2Fhackerspace.sg%2Fcommit%2F55deef96327f0625b5f1f724636a84ded63540c3%22%2C%22author%22%3A%7B%22name%22%3A%22Kai+Hendry%22%2C%22email%22%3A%22hendry%40webconverger.com%22%2C%22username%22%3A%22kaihendry%22%7D%2C%22committer%22%3A%7B%22name%22%3A%22Kai+Hendry%22%2C%22email%22%3A%22hendry%40webconverger.com%22%2C%22username%22%3A%22kaihendry%22%7D%2C%22added%22%3A%5B%22contents%2Fcalendar%2F_index%2Fmain.markdown%22%2C%22contents%2Fconnect%2F_index%2Fmain.markdown%22%2C%22contents%2Fmembership%2F_index%2FMembershipAgreement.pdf%22%2C%22contents%2Fmembership%2F_index%2Fmain.markdown%22%5D%2C%22removed%22%3A%5B%22contents%2F_calendar%2Fmain.markdown%22%2C%22contents%2F_connect%2Fmain.markdown%22%2C%22contents%2F_membership%2FMembershipAgreement.pdf%22%2C%22contents%2F_membership%2Fmain.markdown%22%5D%2C%22modified%22%3A%5B%5D%7D%5D%2C%22head_commit%22%3A%7B%22id%22%3A%2255deef96327f0625b5f1f724636a84ded63540c3%22%2C%22distinct%22%3Atrue%2C%22message%22%3A%22Harmonising+directory+structure+for+punch%22%2C%22timestamp%22%3A%222013-11-25T23%3A27%3A01-08%3A00%22%2C%22url%22%3A%22https%3A%2F%2Fgithub.com%2Fhackerspacesg%2Fhackerspace.sg%2Fcommit%2F55deef96327f0625b5f1f724636a84ded63540c3%22%2C%22author%22%3A%7B%22name%22%3A%22Kai+Hendry%22%2C%22email%22%3A%22hendry%40webconverger.com%22%2C%22username%22%3A%22kaihendry%22%7D%2C%22committer%22%3A%7B%22name%22%3A%22Kai+Hendry%22%2C%22email%22%3A%22hendry%40webconverger.com%22%2C%22username%22%3A%22kaihendry%22%7D%2C%22added%22%3A%5B%22contents%2Fcalendar%2F_index%2Fmain.markdown%22%2C%22contents%2Fconnect%2F_index%2Fmain.markdown%22%2C%22contents%2Fmembership%2F_index%2FMembershipAgreement.pdf%22%2C%22contents%2Fmembership%2F_index%2Fmain.markdown%22%5D%2C%22removed%22%3A%5B%22contents%2F_calendar%2Fmain.markdown%22%2C%22contents%2F_connect%2Fmain.markdown%22%2C%22contents%2F_membership%2FMembershipAgreement.pdf%22%2C%22contents%2F_membership%2Fmain.markdown%22%5D%2C%22modified%22%3A%5B%5D%7D%2C%22repository%22%3A%7B%22id%22%3A14509629%2C%22name%22%3A%22hackerspace.sg%22%2C%22url%22%3A%22https%3A%2F%2Fgithub.com%2Fhackerspacesg%2Fhackerspace.sg%22%2C%22description%22%3A%22Hackerspace.sg+2.0+using+Punch%22%2C%22homepage%22%3A%22http%3A%2F%2Fhsg.dabase.com%2F%22%2C%22watchers%22%3A1%2C%22stargazers%22%3A1%2C%22forks%22%3A0%2C%22fork%22%3Afalse%2C%22size%22%3A264%2C%22owner%22%3A%7B%22name%22%3A%22hackerspacesg%22%2C%22email%22%3Anull%7D%2C%22private%22%3Afalse%2C%22open_issues%22%3A0%2C%22has_issues%22%3Atrue%2C%22has_downloads%22%3Atrue%2C%22has_wiki%22%3Afalse%2C%22language%22%3A%22CSS%22%2C%22created_at%22%3A1384821217%2C%22pushed_at%22%3A1385450838%2C%22master_branch%22%3A%22master%22%2C%22organization%22%3A%22hackerspacesg%22%7D%2C%22pusher%22%3A%7B%22name%22%3A%22kaihendry%22%2C%22email%22%3A%22hendry%40iki.fi%22%7D%7D

Via a command line pipe.

Are there good node.js tools for the job? I'm guessing I need to urldecode and run "payload" through a JSON pretty printer. Bonus points if the answer is one line. ;)

How about this?

var payload = "your_big_string";
console.log(JSON.stringify(JSON.parse(decodeURIComponent(payload)), null, 4));

and if you want to use process.stdin as an input, the accum module can help you turn a stream into a buffer (install with npm i accum ):

var accum = require('accum');
process.stdin.pipe(accum(function (data) {
  console.log(JSON.stringify(JSON.parse(decodeURIComponent(data.toString())), null, 4));
}));

Take a look at the project https://github.com/nlf/node-github-hook

As the project page reads,

This is a very simple, easy to use evented web hook for github.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM