Thursday 15 May 2014

bash - Formatting a json file to add another field -


I have a json file with the format given below. I want to modify the file so that the other key-value pair should be the key url and the value to www.mywebsite.co.nz from the message given below Should be removed. What's the easiest way to do this?

  {"Timestamp": "Mon March 16 21:37:22 ADT 2015", "Event": "Reporting Time", "Message": "Http: //xxx.xx Load for .xx.xx: 1xxxx / Operation and Proxy = www.mywebsite.co.nz & amp; Send = https: //xxx.xx.xx.xx: xxxx / Operations? Event 9426 MS (X Time: 306 MS, Y Time: 1923 MS) Status Code: Unknown & lt; br & gt; Cookies: nzh_weatherlocation = 12; dax_ppv = 11 | NZH: Home | NZH Home | NZH: Home | 9 | Undefined; _ga = GA1 4.1415798036 1426208630; _gat = 1 
Link: 225
Images: 24
Form: 10
Browser: I Ozilla / 5.0 (X11; Linux X86_64) Apple WebKat / 537.36 (KHTML, like Geller) Ubuntu Chromium /41.0.2272.76 Chrome / 41.0.2272.76 Safari / 537.36
CPU: 2 & lt; br & Gt; Language: N-GB "," UserInfo ":" Reporting Time "}

jq as a combination of sed :

  jq ".url = \" $ (jq. 'Message' input .json | Sed 's /.* proxy = \ ([^ & amp;; * *) * / \ 1 / ') \ "" Input.json> Output.Jason  

There are three steps in it:

  jq. 'Message' input.json  

Extract the message from the input JSON,

  sed 's /.* proxy = \ ([^ & amp; ; * *) * / / 1 / ' 

removes the domain from the message, and

  jq ".url = \" domain name \ "" input.json & Gt; Output.Jasson  

The set of .url attribute domain name writes in JSL to the result in output.json . .

I think the way in which it feels compelled to indicate that a domain name itself is not technically a URL, so you might want to re-think that attribute name.


No comments:

Post a Comment