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.

3
  • 1
    Are you trying to do a transformation or are you trying to wait until somebody gives you the code that does a transformation? Commented Mar 23, 2018 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 Commented Mar 23, 2018 at 14:02
  • Hmm... I see. have look at stackoverflow.com/questions/33520699/…, 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) Commented Mar 23, 2018 at 14:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.