Add JSON file content to Invoke-WebRequest POST comment

Welcome Forums General PowerShell Q&A Add JSON file content to Invoke-WebRequest POST comment

Viewing 12 reply threads
  • Author
    Posts
    • #247390
      Participant
      Topics: 1
      Replies: 6
      Points: 6
      Rank: Member

      How do I get a JSON file content to be passed in as the content in the below script?
      <br />$bodyFromJson = '{"comments":[{"parentCommentId":0,"content":"&lt;&lt;JSON file content as comment&gt;&gt;","commentType":1}],"properties":{"Microsoft.TeamFoundation.Discussion.SupportsMarkdown":{"type":"System.Int32","value":1}},"status":1}'<br />$body = $bodyFromJson | ConvertTo-Json<br />$uri = "https://dev.azure.com/{organization}/{project}/_apis/wit/workItems/{workItemId}/comments?api-version=5.1-preview.3"<br />$headers = @{<br />"Content-Type" = "application/json"<br />"Authorization" = "Bearer $env:SYSTEM_ACCESSTOKEN"<br />}<br />Invoke-WebRequest -Uri $Uri -Method Post -Headers $headers -Body ($body | ConvertTo-Json)<br />

    • #247408
      Senior Moderator
      Topics: 9
      Replies: 1309
      Points: 4,781
      Helping Hand
      Rank: Community Hero

      You can read the json file and put it inside.

      <br />$bodyFromJson = &#039;{&quot;comments&quot;:[{&quot;parentCommentId&quot;:0,&quot;content&quot;:&#039; + $JsonContent + &#039;&quot;,&quot;commentType&quot;:1}],&quot;properties&quot;:{&quot;Microsoft.TeamFoundation.Discussion.SupportsMarkdown&quot;:{&quot;type&quot;:&quot;System.Int32&quot;,&quot;value&quot;:1}},&quot;status&quot;:1}&#039;<br />

      • This reply was modified 1 month, 2 weeks ago by kvprasoon. Reason: Code formatting
      • This reply was modified 1 month, 2 weeks ago by kvprasoon.
      • This reply was modified 1 month, 2 weeks ago by grokkit.
    • #247450
      Participant
      Topics: 1
      Replies: 6
      Points: 6
      Rank: Member

      Thanks. But by doing this way the json format is fully messed up though. How can I keep the same source format in the body as well?

    • #247522
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      Typically recommend using Powershell objects vs text as it gives you full flexibility:

      temp.json

      Code:

      Body Output:

      {
      “comments”: [
      {
      “parentCommentId”: 0,
      “commentType”: 1,
      “content”: “@{description=My awesome description; comment=This is what I\u0027m thinking}”
      }
      ],
      “properties”: {
      “Microsoft.TeamFoundation.Discussion.SupportsMarkdown”: {
      “value”: 1,
      “type”: “System.Int32”
      }
      },
      “status”: 1
      }

    • #247553
      Participant
      Topics: 1
      Replies: 6
      Points: 6
      Rank: Member

      Thanks. Yes, it would have been easy if it was in the actual object instead of text. Unfortunately my json file is an output of a different task and is in text. Are there other options to maintain the formatting in the body? Below is the example of my json file content.

      An execution plan has been generated and is shown below.
      Resource actions are indicated with the following symbols:
      ~ update in-place

      Update will perform the following actions:

      # Resource ABC will be updated in-place
      ~ resource “resource_name” {
      endpoint_name = “ABC”
      endpoint_type = “xyz”
      priority = 2
      ~ weight = 1 -> 10

      custom_header {
      name = “host”
      value = “host.test.com”
      }
      }

      Plan: 0 to add, 1 to change, 0 to destroy.

    • #247607
      Participant
      Topics: 0
      Replies: 77
      Points: 343
      Helping Hand
      Rank: Contributor

      If you want to preserve formatting in your json file, you should read the json as a single string and do not perform any serialization. If you want to append a json string to an existing one with minimal spacing, you should compress the json.
      $jsonBody = '{"comments":[{"parentCommentId":0,"content":"&lt;&lt;JSON file content as comment&gt;&gt;","commentType":1}],"properties":{"Microsoft.TeamFoundation.Discussion.SupportsMarkdown":{"type":"System.Int32","value":1}},"status":1}'<br />$myJson = @'<br />{<br />  "Block1": "value1",<br />  "Block2": "value2"<br />}<br />'@</p><p># Inserting myJson as is into jsonBody<br />$jsonBody = $jsonBody -replace '&lt;&lt;JSON file content as comment&gt;&gt;',$myJson</p><p># Inserting a compressed myJson into jsonBody<br />$myJsonCompressed = $myJson | ConvertFrom-Json | ConvertTo-Json -Depth 100 -Compress<br />$jsonBody = $jsonBody -replace '&lt;&lt;JSON file content as comment&gt;&gt;',$myJsonCompressed</p><p># Reading Json as Single String from File to Preserve formatting<br />$jsonString = Get-Content file.json -Raw

    • #247677
      Participant
      Topics: 1
      Replies: 6
      Points: 6
      Rank: Member

      Thanks guys. Getting closer…now i got the json format preserved by introducing the hash table instead but its failing when posting the values.

      Line |
      14 | Invoke-WebRequest -Uri $uri -Method Post -Body $body -Hea
      | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      | {“$id”:”1″,”innerException”:null,”message”:”Value cannot be
      | null.\r\nParameter name:
      | comments”,”typeName”:”System.ArgumentNullException,
      | mscorlib”,”typeKey”:”ArgumentNullException”,”errorCode”:0,”eventId”:0}
    • #247789
      Participant
      Topics: 0
      Replies: 77
      Points: 343
      Helping Hand
      Rank: Contributor

      When using ConvertTo-Json the default depth is 2. You need to add the -Depth parameter. I would just pick a high number like 10, something that will definitely be at or exceed your hierarchy level count.

    • #247891
      Participant
      Topics: 1
      Replies: 6
      Points: 6
      Rank: Member

      I tried with -Depth 100 and still the same error.

    • #247897
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      Try this:

    • #247906
      Participant
      Topics: 1
      Replies: 6
      Points: 6
      Rank: Member

      Rob, yes it would have been much more easier if the source json was well formatted. It is not and so convertfrom-json will fail. I pasted the json content above in my replies.

    • #247924
      Participant
      Topics: 0
      Replies: 77
      Points: 343
      Helping Hand
      Rank: Contributor

      # Resource ABC will be updated in-place

      ~ resource “resource_name” {

      endpoint_name = “ABC”

      endpoint_type = “xyz”

      priority = 2

      ~ weight = 1 -> 10

      custom_header {

      name = “host”

      value = “host.test.com”

      }

      }

      Nothing about the above is JSON. What part of this should be converted to JSON?

    • #247927
      Participant
      Topics: 1
      Replies: 6
      Points: 6
      Rank: Member

      I basically prefer to have the content I pasted as an output of the body it the same format. Thats why I created a hash table and put all the above content as one string.

Viewing 12 reply threads
  • You must be logged in to reply to this topic.