0

I'm trying to find a way to erase the ".png" extension in this JSON file but only for the attribute (so "image": "test.png" should keep .png and "uri": "0.png" should keep it too)

But all the attribute values should be without .png at the end so the value for the background would be Yellow and not Yellow.png

Any tips about this? I tried to use sed command like:

sed -i 's/Yellow.png/Yellow/g' myfile.json

It didn't work and it's not the best way because I would have to do each attribute by hand... (I put a sample but I have more than 100 attributes)

{
"name": "test",
"symbol": "test",
"image": "test.png",
"properties": {
    "files": [
        {
            "uri": "0.png",
            "type": "image/png"
        }
    ],
    "category": "image",
    "creators": []
},
"description": "test",
"seller_fee_basis_points": 0,
"attributes": [
    {
        "trait_type": "Head",
        "value": "1.png"
    },
    {
        "trait_type": "Body",
        "value": "Big.png"
    },
    {
        "trait_type": "Left Arm",
        "value": "Gun.png"
    },
    {
        "trait_type": "Right Arm",
        "value": "Gun.png"
    },
    {
        "trait_type": "Left Legs",
        "value": "Normal.png"
    },
    {
        "trait_type": "Background",
        "value": "Yellow.png"
    },
    {
        "trait_type": "Right Legs",
        "value": "Normal.png"
    }
],
"collection": {
    "name": "Test",
    "family": "test"
}

}

Raed Ali
  • 549
  • 1
  • 6
  • 22
xif
  • 343
  • 1
  • 9
  • 25
  • Using `sed` to modify structured data is nearly always a bad idea. Use a dedicated JSON tool like `jq`. This is a very common FAQ. – tripleee Jan 17 '22 at 12:34

1 Answers1

0

If you only want to change what comes after "value", you can do the following using sed:

's/\(\"value\".*\).png\"/\1"/'

\(\"value\".*\).png\" matches anything starting with "value" and ends with .png".

Then \1" substitutes it for the first match group (what was between the parentheses) and a double quote.

You can see the result here: https://sed.js.org/?gist=9f1a0ce59fd9a7b46277498cc106444e

Nicolas Kagami
  • 130
  • 1
  • 5
  • It look good but How can I pass a file in argument ? like sed -i 's/\(\"value\".*\).png\"/\1"/' *json doesn't work – xif Jan 17 '22 at 12:37
  • Have you tried using only the file instead of "*.json"? sed -i 's/\(\"value\".*\).png\"/\1"/' a.json worked for me... – Nicolas Kagami Jan 17 '22 at 12:43
  • Yes I get : sed -i 's/\(\"value\".*\).png\"/\1"/' 0.json sed: 1: "0.json": invalid command code . – xif Jan 17 '22 at 12:43
  • That worked for me as well. Are you using bash? – Nicolas Kagami Jan 17 '22 at 12:46
  • If you're on a Mac maybe you have this problem with the "-i" option: https://stackoverflow.com/questions/19456518/error-when-using-sed-with-find-command-on-os-x-invalid-command-code – Nicolas Kagami Jan 17 '22 at 12:48
  • I kinda find a workaround but I have to launch the command as many time as I have attributs in my file, so after spamming 100 times sed -i '' -e 's/\(\"value\".*\).png\"/\1"/' *.json. My files are good ! thanks a lot – xif Jan 17 '22 at 13:08