Replace & add specific parts/symbols from a specific lines only
-
Hey guys,
I’m having a HUGE .json file with 8,000+ lines but everything repeats with the following template."water": { "DisplayName": "Water", "Skin": 0, "Image": "water.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 5000, "SellPrice": 0, "Currency": "eco" }, "healingtea.advanced": { "DisplayName": "default", "Skin": 0, "Image": "healingtea.advanced.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 500, "SellPrice": 0, "Currency": "eco" },
What I’m trying to do is adding a the same link before the
"imagename.png"
on every Image line as it’s shown in the current example"Image": "https://www.example.com/example2/healingtea.advanced.png"
but also I have to replace the dot
.
with a dash-
for examplehealingtea-advanced.png
without changing the dot before the “png” or the added link will break.Any help? For the URL part I think I have to use
^"Image": "
in the find field with a regex value mode and then in the replace field to add thehttps://www.example.com/example2/
Or will this add it in front of^"Image": "
instead properly before theimagename.png
?
But first I have to somehow do the.
with-
replacing before I add the link and make it more hard for replacing, right?All of the lines I have to change starts with
^"Image": "
so they can be all searched properly and it’s maybe easy to exclude the.
before thepng
because it’s all the same again.I hope I explained everything properly, if you have ideas share them please. I’m completely new here and yesterday learned how to use
^
symbol andRegex
myself to add the same"
symbol in front of each Notepad++ line and",
at the end but this is more complex for my brain, haha. -
This is the kind of job that you don’t want to use regular expressions for; JSON can be very hard to parse with regex. Use a JSON parser instead.
If I understand your goal correctly, your JSON document is an object that has many instances of the following:
"this.is_the.key": { "BlockAmountChange": (true or false), "BuyPrice": (integer), "Currency": (text), "DefaultAmount": (integer), "DisplayName": (text), "Image": (text), "SellPrice": (integer), "Skin": (integer) }
and you want to modify every one of these objects to the following:
"this.is_the.key": { "BlockAmountChange": (true or false), "BuyPrice": (integer), "Currency": (text), "DefaultAmount": (integer), "DisplayName": (text), "Image": "https://www.example.com/example2/this-is_the-key.png", "SellPrice": (integer), "Skin": (integer) }
There are many ways to do this, including with scripting plugins like PythonScript . I’m going to demonstrate how to do this with the JsonTools plugin using RemesPath . If you need to automate this procedure, I recommend learning Python, and using the steps below as a starting point for your Python script that does this.
- Copy the text of your file into a new buffer so that you can compare the result to the original
- Open the tree view in JsonTools in your new buffer
- Paste the following query in the text box at the top of the tree view
for x = keys(@); at(@, x).Image = `https://www.example.com/example2/` + s_sub(x, `.`, `-`) + `.png`
- Click the
Submit query
button; this will change everyImage
field - You can use ComparePlus to compare the result with the original and make sure it’s to your liking.
I tested this query with the following START JSON:
{ "3kkQI.k67920": { "BlockAmountChange": false, "BuyPrice": -708951, "Currency": "DBg0", "DefaultAmount": -618002, "DisplayName": "default", "Image": "PAC", "SellPrice": -191929, "Skin": 439298 }, "ruHOa": { "BlockAmountChange": false, "BuyPrice": 800735, "Currency": "7", "DefaultAmount": 92053, "DisplayName": "8G", "Image": "cDf.Sdw", "SellPrice": 434145, "Skin": -127422 }, "Eup8FI8t.bvBR_JZcEv.YHBfpk": { "BlockAmountChange": true, "BuyPrice": 74371, "Currency": "KD", "DefaultAmount": 687624, "DisplayName": "I8v", "Image": "I3JGDMP.QUxCfddTP", "SellPrice": 94973, "Skin": -40263 }, "water": { "DisplayName": "Water", "Skin": 0, "Image": "water.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 5000, "SellPrice": 0, "Currency": "eco" }, "healingtea.advanced": { "DisplayName": "default", "Skin": 0, "Image": "healingtea.advanced.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 500, "SellPrice": 0, "Currency": "eco" } }
and got this END JSON:
{ "3kkQI.k67920": { "BlockAmountChange": false, "BuyPrice": -708951, "Currency": "DBg0", "DefaultAmount": -618002, "DisplayName": "default", "Image": "https://www.example.com/example2/3kkQI-k67920.png", "SellPrice": -191929, "Skin": 439298 }, "ruHOa": { "BlockAmountChange": false, "BuyPrice": 800735, "Currency": "7", "DefaultAmount": 92053, "DisplayName": "8G", "Image": "https://www.example.com/example2/ruHOa.png", "SellPrice": 434145, "Skin": -127422 }, "Eup8FI8t.bvBR_JZcEv.YHBfpk": { "BlockAmountChange": true, "BuyPrice": 74371, "Currency": "KD", "DefaultAmount": 687624, "DisplayName": "I8v", "Image": "https://www.example.com/example2/Eup8FI8t-bvBR_JZcEv-YHBfpk.png", "SellPrice": 94973, "Skin": -40263 }, "water": { "DisplayName": "Water", "Skin": 0, "Image": "https://www.example.com/example2/water.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 5000, "SellPrice": 0, "Currency": "eco" }, "healingtea.advanced": { "DisplayName": "default", "Skin": 0, "Image": "https://www.example.com/example2/healingtea-advanced.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 500, "SellPrice": 0, "Currency": "eco" } }
-
Hello, @flammable and All,
First, I would advice you to fully read this interesting FAQ post below, which explains to prefer the use of a
JSON parser
( instead of regexes ) for such problems :Oh,…, by moving to your post, I just see the @mark-olson’s reply which explains a straight solution, based on the
JsonTools
plugin ;-))
However, I also succeeded to find out a regex way to do it !
So, given your INPUT text, below, in a new tab :
"water": { "DisplayName": "Water", "Skin": 0, "Image": "water.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 5000, "SellPrice": 0, "Currency": "eco" }, "healingtea.advanced": { "DisplayName": "default", "Skin": 0, "Image": "healingtea.advanced.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 500, "SellPrice": 0, "Currency": "eco" },
-
Open the Replace dialog (
Ctrl + H
) -
Untick all box options
-
SEARCH
(?-is)^\x20+"Image":\x20"\K
-
REPLACE
https://www.example\.com/example2/
-
Check the
Wrap around
option -
Click, once, on the
Replace All
button ( NOT theReplace
button ! )
You should get this OUTPUT text :
"water": { "DisplayName": "Water", "Skin": 0, "Image": "https://www.example.com/example2/water.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 5000, "SellPrice": 0, "Currency": "eco" }, "healingtea.advanced": { "DisplayName": "default", "Skin": 0, "Image": "https://www.example.com/example2/healingtea.advanced.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 500, "SellPrice": 0, "Currency": "eco" },
Now with the following regex S/R, we’ll change any dot character, not followed by the string
png",
at end of line, with a dash char :SEARCH
(?-si)(?:^\x20+"Image":\x20"https://www.example.com/example2/|(?!\A)\G).*?\K\.(?!png",$)
REPLACE
-
Just follow, exactly, the same other points of the previous S/R !
You should get your expected OUTPUT text :
"water": { "DisplayName": "Water", "Skin": 0, "Image": "https://www.example.com/example2/water.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 5000, "SellPrice": 0, "Currency": "eco" }, "healingtea.advanced": { "DisplayName": "default", "Skin": 0, "Image": "https://www.example.com/example2/healingtea-advanced.png", "DefaultAmount": 1, "BlockAmountChange": false, "BuyPrice": 500, "SellPrice": 0, "Currency": "eco" },
BR
guy038
-