Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell Script: replace mutiple strings in a json file. Foreach methood

New guy in the world of power shell scripts.
I need to replace 100+ variables with new values in a BIG .json file

Following the JSON example

[
  {
    "_id": "631b3375030cafa9b47ebf06",
    "index": 0,
    "guid": "cf39106c-7afa-497a-a0bb-c4ac925f1a2a",
    "isActive": true,
    "balance": "$2,446.24",
    "picture": "http://placehold.it/32x32",
    "age": 37,
    "eyeColor": "green",
    "name": "Angel Jenkins",
    "gender": "female",
    "company": "EQUITAX",
    "email": "angeljenkins@equitax.com",
    "phone": "%%PHONE_NUMBER_REPLACE%%",
    "address": "809 Junius Street, Bayview, Federated States Of Micronesia, 1208",
    "about": "Nostrud eu enim amet irure ad deserunt aute laborum exercitation. Incididunt enim velit eiusmod quis elit deserunt ex officia irure est. Eu excepteur laboris eu nostrud officia. Cupidatat aute nulla qui ullamco eu pariatur. Culpa occaecat elit amet sit occaecat eiusmod ut ea consectetur. Sunt excepteur laboris cillum laboris. Non cillum nisi est anim ex id reprehenderit.\r\n",
    "registered": "2022-01-20T10:47:40 -02:00",
    "latitude": 71.06607,
    "longitude": 128.932965,
    "tags": [
      "dolore",
      "reprehenderit",
      "veniam",
      "in",
      "do",
      "reprehenderit",
      "pariatur"
    ],
    "friends": [
      {
        "id": 0,
        "name": "%%NAME_FRIEND_TO_REPLACE%%"
      },
      {
        "id": 1,
        "name": "Angelia Holder"
      },
      {
        "id": 2,
        "name": "Dale Shelton"
      }
    ],
    "greeting": "Hello, Angel Jenkins! You have 6 unread messages.",
    "favoriteFruit": "banana"
  },
  {
    "_id": "631b33756e8ab97465edeb62",
    "index": 1,
    "guid": "020e9d50-6d96-485f-b019-467684f0cacb",
    "isActive": true,
    "balance": "$1,788.39",
    "picture": "%%URL_PICTURE_REPLACE%%",
    "age": 26,
    "eyeColor": "brown",
    "name": "Anderson Jimenez",
    "gender": "male",
    "company": "BRISTO",
    "email": "%%%EMAIL_REPLACE%",
    "phone": "+1 (866) 448-2626",
    "address": "225 Euclid Avenue, Barstow, Colorado, 5335",
    "about": "Aliqua exercitation sit duis qui est consequat cupidatat ea dolor aliqua laboris. Ex consectetur incididunt ea non voluptate velit. Non in deserunt commodo aute ex. Officia ex ullamco laboris labore.\r\n",
    "registered": "2017-03-30T10:34:17 -03:00",
    "latitude": 80.533482,
    "longitude": 100.437459,
    "tags": [
      "qui",
      "commodo",
      "%%REPLACE_TAG%%",
      "aute",
      "et",
      "duis",
      "sit"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Bowman Stephens"
      },
      {
        "id": 1,
        "name": "Tamika Phelps"
      },
      {
        "id": 2,
        "name": "Mcneil Ross"
      }
    ],
    "greeting": "Hello, Anderson Jimenez! You have 1 unread messages.",
    "favoriteFruit": "banana"
  },
  {
    "_id": "631b3375f319ce2391fdefff",
    "index": 2,
    "guid": "cf08598e-378f-4318-a2fa-c111ee842434",
    "isActive": true,
    "balance": "$1,247.75",
    "picture": "http://placehold.it/32x32",
    "age": 31,
    "eyeColor": "blue",
    "name": "Amy Murphy",
    "gender": "female",
    "company": "COMTENT",
    "email": "amymurphy@comtent.com",
    "phone": "+1 (805) 436-3728",
    "address": "%%ADDRESS_REPLACE%%",
    "about": "%%ABOUT_MESSAGE_TO_REPLACE%%",
    "registered": "2015-02-28T01:09:54 -02:00",
    "latitude": 7.595347,
    "longitude": -151.936382,
    "tags": [
      "quis",
      "anim",
      "esse",
      "ea",
      "adipisicing",
      "ea",
      "est"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Teresa Rose"
      },
      {
        "id": 1,
        "name": "Joyner Ray"
      },
      {
        "id": 2,
        "name": "Simpson Rivas"
      }
    ],
    "greeting": "Hello, Amy Murphy! You have 7 unread messages.",
    "favoriteFruit": "strawberry"
  },
]

I can try to do:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$filePath = ".\project\data.json"

$findString1 = "%%ABOUT_MESSAGE_TO_REPLACE%%"
$replaceString1 = "My name is Andrew"

$findString2 = "%%PHONE_NUMBER_REPLACE%%"
$replaceString2 = "0045864188"

(Get-Content $filePath) -replace $findString1, $replaceString1 -replace $findString2, $replaceString2 | Set-Content $filePath

But imagine to create 100+ variables for all the strings and replaces.

I’ve tried this thread, but no luck at all because I don’t need to select a specific object in my json (also tried to replace, not to select). Another idea was to create a json local with key: value, iterate my json data and compare (something like this). But no luck

Any thoughts?

>Solution :

Put the replacements in a hashtable and repeat the -replace operation for each entry in a loop:

# read file into memory
$filePath = ".\project\data.json"
$json = Get-Content $filePath

# define replacements to be carried out
$replacements = [ordered]@{
  ABOUT_MESSAGE_TO_REPLACE = "My name is Andrew"
  PHONE_NUMBER_REPLACE     = "0045864188"
  # ... and so on
}

# replace!
foreach($label in $replacements.psbase.Keys){
  $pattern = [regex]::Escape("%%${label}%%")
  $substitute = $replacements[$label]
  $json = $json -replace $pattern,$substitute
}

# write to disk
$json |Set-Content .\project\output.json
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading