string formating and using curley brace in the text

Welcome Forums General PowerShell Q&A string formating and using curley brace in the text

Viewing 3 reply threads
  • Author
    Posts
    • #215529
      Participant
      Topics: 1
      Replies: 0
      Points: 11
      Rank: Member

      i have tried using :

      for($i=0; $i -lt 3; $i++){
      $NewPath = “k:\xTools\text_copy\Output\td_block_{0:D3}.json” -f $i

      $val = @”
      {
      “variants”: {
      “”: {
      “model”: “yabm5:block/td_block_{1:D3}”
                            }
                      }
            }
      “@ -f $i

       

      set-content -Path $NewPath -Value $Val

      }

       

      I get an error ” Error formatting a string: Input string was not in a correct format”

      How do i use {} in my string without messing up the formatting?

      Ive tried releasing with \ with no luck

       

    • #215532
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Welcome to Powershell.org.
      Please (re-)read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!
      Next you should do is go back and fix your post by formatting your code as code using the code tags “PRE“, please. Thanks.

      You could create your json file like this:

      for ($i = 0; $i -lt 3; $i++) {
          $NewPath = "k:\xTools\text_copy\Output\td_block_{0:D3}.json" -f $i
          $InsertString = "{0:D3}" -f $i
          $val = @"
      {
      "variants": {
      "": {
      "model": "yabm5:block/td_block_$InsertString"
                            }
                      }
            }
      "@
          set-content -Path $NewPath -Value $Val
      }

      But there are probably much more sophisticated ways to achieve something like this. Did you know, there ist a cmdlet ConvertTo-Json?? 😉

      Last but not least I’d like to ask you a personal favor: Please try to avoid posting that much unnecessary white space. It makes your post harder read. Thanks in advance.

    • #215535
      Participant
      Topics: 12
      Replies: 523
      Points: 1,214
      Helping Hand
      Rank: Community Hero

      Well, you have a couple of errors in your code:
      1. In the line (“model”: “yabm5:block/td_block_{1:D3}”) you’re skipping the first index #0. You have 1 variable after the -f. This line needs to be changed to (“model”: “yabm5:block/td_block_{0:D3}”) so that the value of $i would replace index #0 inside the curly braces.
      2. Your here string is improperly formatted, which is the subject of the error you’re getting (Error formatting a string: Input string was not in a correct format).
      For example this code:

      for ($i=0; $i -lt 3; $i++){
          $NewPath = "k:\xTools\text_copy\Output\td_block_{0:D3}.json" -f $i
          $Val = 
      @"
      {
      "variants”: {
          "":     { "model": $("yabm5:block/td_block_{0:D3}" -f $i) }
          }
      }
      "@ 
          # Set-Content -Path $NewPath -Value $Val
          "Setting content of ($NewPath) to"
          $Val
      }
      

      produces this output:

      Setting content of (k:\xTools\text_copy\Output\td_block_000.json) to
      {
      "variants”: {
          "":     { "model": yabm5:block/td_block_000 }
          }
      }
      Setting content of (k:\xTools\text_copy\Output\td_block_001.json) to
      {
      "variants”: {
          "":     { "model": yabm5:block/td_block_001 }
          }
      }
      Setting content of (k:\xTools\text_copy\Output\td_block_002.json) to
      {
      "variants”: {
          "":     { "model": yabm5:block/td_block_002 }
          }
      }
      

      As a side issue, I’m not sure that this line (“”: {) is valid JSON. You’re giving a value to an unnamed variable!?
      I suggest this code instead:

      for ($i=0; $i -lt 3; $i++){
          $NewPath = "k:\xTools\text_copy\Output\td_block_{0:D3}.json" -f $i
          $Val = 
      @"
      {
      "variants”: {
          "MissingVariantName":     { "model": $("yabm5:block/td_block_{0:D3}" -f $i) }
          }
      }
      "@ 
          # Set-Content -Path $NewPath -Value $Val
          "Setting content of ($NewPath) to"
          $Val
      }
      

      Finally, consider building your JSON using PS objects as in:

      for ($i=0; $i -lt 3; $i++){
          $NewPath = "k:\xTools\text_copy\Output\td_block_{0:D3}.json" -f $i
          $Val = [PSCustomObject]@{
              Variants = [PSCustomObject]@{
                  "MissingVariantName" = [PSCustomObject]@{
                      model = "yabm5:block/td_block_{0:D3}" -f $i
                  }
              }
          } | ConvertTo-Json 
          # Set-Content -Path $NewPath -Value $Val
          "Setting content of ($NewPath) to"
          $Val
      }
      
      • This reply was modified 1 month, 3 weeks ago by Sam Boutros.
    • #215541
      Participant
      Topics: 12
      Replies: 523
      Points: 1,214
      Helping Hand
      Rank: Community Hero

      @Blinky00082 you got 3 errors in your code. Can you spot them compared to the following code?

      for ($i=0; $i -lt 3; $i++){
          $NewPath = "k:\xTools\text_copy\Output\td_block_{0:D3}.json" -f $i
          $Val = 
      @"
      {
      "variants”: {
          "MissingVariantName":     { "model": $("yabm5:block/td_block_{0:D3}" -f $i) }
          }
      }
      "@ 
          # Set-Content -Path $NewPath -Value $Val
          "Setting content of ($NewPath) to"
          $Val
      }
      

      Also consider using PS objects to build you JSON as in:

      for ($i=0; $i -lt 3; $i++){
          $NewPath = "k:\xTools\text_copy\Output\td_block_{0:D3}.json" -f $i
          $Val = [PSCustomObject]@{
              Variants = [PSCustomObject]@{
                  "MissingVariantName" = [PSCustomObject]@{
                      model = "yabm5:block/td_block_{0:D3}" -f $i
                  }
              }
          } | ConvertTo-Json 
          # Set-Content -Path $NewPath -Value $Val
          "Setting content of ($NewPath) to"
          $Val
      }
      
Viewing 3 reply threads
  • You must be logged in to reply to this topic.