Reputation: 14970
I have a json file with the format given below.I want to modify the file so as to add another key-value pair to it. The key should be url
and the value should be www.mywebsite.co.nz
extracted from the message given below. What is the easiset way to do this?
{"
Timestamp":"Mon Mar 16 21:37:22 EDT 2015","Event":"Reporting Time","Message":"load for http://xxx.xx.xx.xx:1xxxx/operations&proxy=www.mywebsite.co.nz&send=https://xxx.xx.xx.xx:xxxx/operations?event took 9426 ms (X Time: 306 ms, Y Time: 1923 ms)
StatusCode: Unknown<br>Cookies: nzh_weatherlocation=12; dax_ppv=11|NZH:home|NZH:home|NZH:home|9|undefined; _ga=GA1.4.1415798036.1426208630; _gat=1<br>Links: 225<br>Images: 24<br>Forms: 10<br>Browser: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/41.0.2272.76 Chrome/41.0.2272.76 Safari/537.36<br>CPUs: 2<br>Language: en-GB","UserInfo":"Reporting Time"}
Upvotes: 1
Views: 151
Reputation: 63892
For perl
users, using ojo:
perl -Mojo -E '$j=j(b("input.file")->slurp);if($j->{Message}=~m/proxy=(.*?)&/){$j->{url}=$1;say j($j)}'
decomposed:
b()->slurp
- reads the input.file
j()
- converts the json to perl dataMessage
contains "proxy=site&" - get the siteurl => site
j()
convert to json stringUpvotes: 1
Reputation: 44023
As a combination of jq
and sed
:
jq ".url = \"$(jq '.Message' input.json | sed 's/.*proxy=\([^&]*\).*/\1/')\"" input.json > output.json
This consists of three steps:
jq '.Message' input.json
extracts the message part from the input JSON,
sed 's/.*proxy=\([^&]*\).*/\1/'
extracts the domain from the message, and
jq ".url = \"domainname\"" input.json > output.json
sets the .url
attribute of the input json to the extracted domain name, writing the result to output.json
.
I feel compelled to point out, by the way, that a domain name by itself is not technically a URL, so you may want to rethink that attribute name.
Upvotes: 2