I took the liberty to modify OP's input slightly, because as it stands , it's not properly structured json
data (due to the {...}
part) and implemented a small python script that works with multiple dictionaries, assuming that we're dealing with a dictionary per line. Additionally, as has been discussed in the comments to the question, OP also wanted to remove http://
part.
The script below implements everything discussed above.
#!/usr/bin/env python
import json,sys
with open(sys.argv[1]) as f:
for line in f:
data=json.loads(line)
if data["url"][-1] == '/':
data["url"]=data["url"][:-1].replace('http://','')
if data["originalUrl"][-1] == '/':
data["originalUrl"]=data["originalUrl"][:-1].replace('http://','')
json.dump(data,sys.stdout)
print("")
Test run:
$ cat input.txt
{"url":"http://example.com/vary/file/","originalUrl":"http://example.com/vary/file/","applications":[{"somedata": "blah"}]}
{"url":"http://another-example.com/vary/file/","originalUrl":"http://example.com/vary/file/","applications":[{"somedata": "blah"}]}
$ ./remove_slash.py input.txt
{"url": "example.com/vary/file", "applications": [{"somedata": "blah"}], "originalUrl": "example.com/vary/file"}
{"url": "another-example.com/vary/file", "applications": [{"somedata": "blah"}], "originalUrl": "example.com/vary/file"}
http://
from the input.http://
should be easy, just anothersed 's,http://,,g'
json
APIs (and maybe learn those in the process ). I understand it's fun and simple sometimes to usesed
or other tools, butjson
APIs were created specifically for that purpose. Of course, structuringjson
data properly. Not trying to teach you what to do, but seriously - you can save yourself a lot of time if you start using proper tools for proper job.