3

I'm trying to perform some config transformations on JSON files using PowerShell. For this there's several input json files and a transform one (one for each environment).

Input sample (AzureStorage.json):

{
"$schema": "http://datafactories.schema.management.azure.com/schemas/2015-09-01/Microsoft.DataFactory.LinkedService.json",
"name": "AzureStorage",
"properties": {
"type": "AzureStorage",
  "typeProperties": {
    "connectionString": "My Connection String here"
  } } }

Transform:

{
  "$schema": "http://datafactories.schema.management.azure.com/vsschemas/V1/Microsoft.DataFactory.Config.json",
  "AzureStorage": [
    {
      "name": "$.properties.typeProperties.connectionString",
      "value": "DefaultEndpointsProtocol=https;AccountName=mytestaccount;AccountKey=d;lasfjdalfdjfldjfdsfds;EndpointSuffix=core.windows.net"
    }
  ],
  "DataLakeStore": [
    {
      "name": "$.properties.typeProperties.dataLakeStoreUri",
      "value": "https://mydatalake.azuredatalakestore.net/webhdfs/v1"
    }
  ]
}

What I need to do, is to load the transform file, then traverse it, finding the names of the input files I need to transform (in this example AzureStorage.json and DataLakeStore.json).

Next, I need to replace the properties accordingly. I'm trying to do it by loading the transform file into a variable using ConvertFrom-Json, but I not sure how to traverse it afterwards.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Daniel Perez
  • 4,292
  • 4
  • 29
  • 54
  • 1
    Are you trying to do a transformation or are you trying to wait until somebody gives you the code that does a transformation? – Tomalak Mar 23 '18 at 10:52
  • I just don't know how to dynamically traverse the json, I need to know how to do get all of the items containing "AzureStorage", "DataLakestore" (dynamically of course, there will be more). the rest i can figure out myself. try to be helpful please, thanks – Daniel Perez Mar 23 '18 at 14:02
  • Hmm... I see. have look at https://stackoverflow.com/questions/33520699/iterating-through-json-file-powershell/33521853, where I show two ways of iterating through unknown keys in a Powershell file. (I try to be helpful. On the other hand, searching for "iterating json powershell" brings up more than one hit, and you show no own attempt in your question, so it seems a bit like you could have put in more effort) – Tomalak Mar 23 '18 at 14:06

1 Answers1

3

I don't know hat exactly you need. I'm guessing access to the information within the JSON file.

What about this approach?

$json_object = Get-Content -Raw -Path '<your_path>\transform.json' | ConvertFrom-Json
$azure_storage = @('AzureStorage'; 'DataLakeStore')

ForEach ($azure in $json_object) {
    ForEach ($storage in $azure_storage) {
        Write-Output $azure.$storage.name
        Write-Output $azure.$storage.value
    }
}

Edit Due to edit I got it. You need a generic access.

Here you go:

$json_object = (Get-Content -Path '<your_path>\transform.json') -join "`n"  | ConvertFrom-Json

ForEach ($object in $json_object.PsObject.Properties) {
    Write-Output $object.name
    Write-Output $object.value
}

Explanation: (Get-Content -Path '<your_path>\transform.json') -join "n"` is a Microsoft's MSDN recommended way to read json files.

You need to find out where the values are. The object you are using is a "Windows PowerShell custom object" - PsObject. To access the you need to use .Properties.value. Then you have the Strings you want and you can access them using .name and .value.

Helge Klein
  • 8,829
  • 8
  • 51
  • 71
tukan
  • 17,050
  • 1
  • 20
  • 48
  • hi @tukan I need to access the information dynamically, I cannot assume that it will always be called "AzureStorage" or "DataLakeStore", those were just examples, but the transforms file can contain more items for different config files – Daniel Perez Mar 23 '18 at 13:59