0

I am currently working on a use case for invoking ReST request in PowerShell. The body of POST request is created dynamically, reading data from a CSV file.

Here is how my final request body should be

{
            "@type": "mtTaskParameter",
            "name": "$src_sfdc$",
            "type": "EXTENDED_SOURCE",
            "sourceConnectionId":"00002E0B00000000000C"
        },
        {
            "@type": "mtTaskParameter",
            "name": "$tgt_db_del$",
            "type": "TARGET",
            "targetConnectionId":"00002E0B00000000000D"
        },
        {
            "@type": "mtTaskParameter",
            "name": "$tgt_db_ups$",
            "type": "TARGET",
            "targetConnectionId":"00002E0B00000000000D"
        },
        {
            "@type": "mtTaskParameter",
            "name": "$tgt_status$",
            "type": "TARGET",
            "targetConnectionId":"00002E0B00000000000D"
        }
}

Currently I have implemented like below

if($connectionParameterized -eq "true"){

        $str = @"
        "@type": "mtTaskParameter",
        "name": "$name",
        "type": "$type"
"@

        if($type -eq "SOURCE"){

        $sourceConnectionId = <get source id>

        $str = $str+
        @"
            ,"sourceConnectionId":"$sourceConnectionId"
"@
        }

        if($type -eq "TARGET"){

        $targetConnectionId = <get target id>

        $str = $str+
        @"
        ,"targetConnectionId":"$targetConnectionId"
"@
        }
$finalstr = $finalstr+@"
     {
     $str
     },
"@
}

This works fine, but the code becomes really messy and so difficult to scale. Also while printing, the format is not proper.

Is there a better way to handle this?

Note: As evident from the example, the request body contains several special characters like @,$ etc.

live2learn
  • 861
  • 2
  • 9
  • 16

1 Answers1

1

This would be easier if you included your CSV, but basically, you can import the CSV as an array of objects, then convert that to JSON.

You can customize the objects that are created from importing the CSV by adding custom members so that the translation to JSON gives you the output you want.

You can also group or filter the array of objects to make different ones depending on certain conditions.

Here's a some sample code that probably won't work directly but should somewhat demonstrate the concept:

$json = Import-Csv -Path C:\my\data.csv |
    ForEach-Object -Process {
        $row = $_
        $propName = $row.Type.ToLower() + 'ConnectionId'
        $row | Add-Member -NotePropertyName $propName -NotePropertyValue $out["$mapping_name"].$name -Force -PassThru
    } |
    ConvertTo-Json
briantist
  • 45,546
  • 6
  • 82
  • 127
  • Can you please give one example to demonstrate this? – live2learn Mar 16 '16 at 14:42
  • @AnsgarWiechers I would probably do that too if I knew what the names of the properties were supposed to be; if the CSV data is added to the question I may change it. But I dislike the messiness of the calculated item syntax so I tend to prefer `Add-Member` but it all depends on the situation for me. – briantist Mar 16 '16 at 14:42
  • @live2learn edit your question to include a sample of the CSV, and maybe add more of your code (we have no idea what `$out["$mapping_name"].$name` refers to for instance). From there, it's much easier to write an answer. – briantist Mar 16 '16 at 14:44
  • @briantist thanks a lot for your response. The csv file doesn't help much here as values are not directly fetched from csv. the csv values are compared against another ReST response and then the values are fetched. My query here is basically to understand how I can add a part of a body dynamically in a better way. – live2learn Mar 16 '16 at 14:57
  • @AnsgarWiechers I don't intend the generate the property names as such. the property value will be calculated and the name:property will be added. – live2learn Mar 16 '16 at 15:01
  • @live2learn I was talking about the names of the (PowerShell object) properties `sourceConnectionId` and `targetConnectionId`, not about the property `name`. – Ansgar Wiechers Mar 16 '16 at 15:56
  • @AnsgarWiechers right. sourceConnectionId & targetConnectionId are different properties and value against them needs to be added during runtime as sourceConnectionId:value1 or targetConnectionId:value2. Do you believe there is no better way to achieve this? – live2learn Mar 16 '16 at 16:01