1

I have 2 files text.json that contains

{
    "Files": [
        {
            "pattern": "/Something/Something/*"
        },
        {
            "pattern": "/Something/Something/*"
        },
        {
            "pattern": "/Something/Something/*"
        },
        {
            "pattern": "/Something/Something/*"
        },
        {
            "pattern": "/Something/Something/*"
        },
        {
            "pattern": "/Something/Something/*"
        }
    ]
}

and dlls.txt

1.dll
2.dll
..
6.dll

I want to replace the symbol * with the necessary dll like this :

"Files": [
        {
            "pattern": "/Something/Something/1.dll"
        },
        {
            "pattern": "/Something/Something/2.dll"
        },
       .
       .
       .
        {
            "pattern": "/Something/Something/6.dll"
        }
    ]
}

So far my code replaces the symbol but only with the last array element.

0

2 Answers 2

2

Since you're dealing with a structured data format - JSON - using a dedicated parser is always preferable to performing purely textual processing based on regexes.

While using the dedicated ConvertFrom-Json and ConvertTo-Json cmdlets to parse from and serialize back to JSON is slower than textual processing, it is much more robust.

# Read the DLL names from the text file into an array of strings.
$dlls = Get-Content dlls.txt

# Read the JSON file and parse it into an object.
$objFromJson = Get-Content -Raw text.json | ConvertFrom-Json

# Loop over all elements of the array in the .Files property and
# update their .pattern property based on the corresponding DLL names.
$i = 0
$objFromJson.Files.ForEach({ 
  $_.pattern = $_.pattern -replace '(?<=/)\*$', $dlls[$i++] 
})

# Convert the updated object back to JSON; save to a file as needed.
$objFromJson | ConvertTo-Json
Sign up to request clarification or add additional context in comments.

2 Comments

Previously I tried to convert from json, but I couldn't get the raw data because of wrong syntax. Thank you, that solves everything :)
Glad to hear the answer helped, @ИванЦанев; my pleasure.
1

Why not skip the 'C:\Users\itsan\Desktop\text.json' file alltogether and simply create a new JSON from the dll filenames you have in 'C:\Users\itsan\Desktop\dlls.txt' ?

$dlls = Get-Content -Path 'C:\Users\itsan\Desktop\dlls.txt'

$result = [PsCustomObject]@{
    Files = foreach($file in $dlls) {
        "" | Select-Object @{Name = 'pattern'; Expression = {"/Something/Something/$file"}}
    }
}
$result | ConvertTo-Json

If you want that as new file, simply change the last line into

$result | ConvertTo-Json | Set-Content -Path 'C:\Users\itsan\Desktop\dll.json'

Output wil be like this:

{
    "Files":  [
                  {
                      "pattern":  "/Something/Something/1.dll"
                  },
                  {
                      "pattern":  "/Something/Something/2.dll"
                  },
                  {
                      "pattern":  "/Something/Something/3.dll"
                  },
                  {
                      "pattern":  "/Something/Something/4.dll"
                  },
                  {
                      "pattern":  "/Something/Something/5.dll"
                  },
                  {
                      "pattern":  "/Something/Something/6.dll"
                  }
              ]
}

2 Comments

The problem is that I have 80+ .dlls and the pattern is different on every line.
@ИванЦанев Aha, you didn't make that clear in the question. In that case, accept mklement0's answer

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.