0

If I have a file with such content:

{"id":"doi-platelemetry-doi/doiplatelemetry","name":"doi/doiplatelemetry","location":"doi/doiplatelemetry/2.0.0/doiplatelemetry:2.0.0_49","component":"doi","tag":"2.0.0_49"},{"id":"doi-maintenance-service-doi/maintenance-service","name":"doi/maintenance-service","location":"doi/1.0.0/maintenance-service:1.0.0.681","component":"doi","tag":"1.0.0.681"}

How do we replace all / with - only in the value of location field? Meaning, after the replacement, the file content should be:

{"id":"doi-platelemetry-doi/doiplatelemetry","name":"doi/doiplatelemetry","location":"doi-doiplatelemetry-2.0.0-doiplatelemetry:2.0.0_49","component":"doi","tag":"2.0.0_49"},{"id":"doi-maintenance-service-doi/maintenance-service","name":"doi/maintenance-service","location":"doi-1.0.0-maintenance-service:1.0.0.681","component":"doi","tag":"1.0.0.681"}

This can be very easily achieved using jq but I am looking for a solution that works even when tools like jq are not available.

4
  • 1
    Please do add your efforts in form of code in your question, which is highly encouraged on SO, thank you. Commented Feb 24, 2021 at 3:50
  • Do not try to modify JSON as if it is just a bunch of characters. JSON is a structured format, and treating it as if it's just lines of data is going to cause you sorrow. Use a tool like jq to properly manipulate JSON. Commented Feb 24, 2021 at 15:41
  • Yes, I could see expected output using jq. Was just thinking if we can achieve the same without extra tools. What I had pasted in the question was just a small part of the entire document thinking there must be solution without jq. On my original json document, this works perfectly fine: cat images.json | jq '.payload[].location |= "gsub("/";"-")' > images.json_modified Commented Feb 24, 2021 at 15:46
  • jq is the right tool for the job. Anything you try without jq is going to be prone to errors. Commented Feb 24, 2021 at 15:50

1 Answer 1

1

With awk:

awk -F, 'BEGIN { RS=","} /location/ { gsub("/","-",$0) } {ORS=","}1' file > file.tmp && mv -f file.tmp file

Set the record separator to "," and then when the record contains location, using gsub to replace all "/" for "-"

Sign up to request clarification or add additional context in comments.

8 Comments

This splits the records and then makes the modification. This is not making the changes to the file so that we get the final output.
No worries. I've amended the solution to output to a temp file and copy the tmp file to the master.
Sorry, I should have been more explicit in my earlier comment. Redirecting to a temp file and then renaming it to original is well understood. But what goes into that temp file is not the JSON document but records splitted.
Apologies. I forgot to set the output record separator. Try it now.
Try the solution now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.