Monday, July 25, 2022

[SOLVED] Only want filename from url in json file

Issue

I have the following json file below:

{"cloud":"https://cloudfronturl/folder/folder",
"env": "int"
"sources":["https://www.example.com/some.tar.gz","https://www.example2.com/folder1/folder2/another.tar.gz"],
"owner": "some manager"
}

How can I modify the file to read like below, where only the file names stripped from sources url? Don't want to touch cloud value

{"cloud":"https://cloudfronturl/folder/folder",
"env": "int"
"sources":["some.tar.gz","another.tar.gz"],
"owner": "some manager"
}

Solution

Assuming your JSON snippet is fixed and using jq is an option, you could do

jq '.sources[] |= ( split("/") | last )'


Answered By - Inian
Answer Checked By - Cary Denson (WPSolving Admin)