cjerrington,
@cjerrington@mstdn.social avatar

I need some help with where we need to export some values all as strings. So the numbers we have in the object need to be strings, but only are exported as integers.

Example:
{
"SchemaVersion": 1
}

Needs to be

{
"SchemaVersion": "1"
}

jborean,

@cjerrington Make the type a string when you create the object

@{SchemaVersion=1} | ConvertTo-Json

{ "SchemaVersion": 1 }

@{SchemaVersion="1"} | ConvertTo-Json

{ "SchemaVersion": "1" }

cjerrington,
@cjerrington@mstdn.social avatar

@jborean I'm running a process that is exporting the data and don't have control of how the module exports its data.

falkheiland,

@cjerrington regex to the rescue, i guess '{"SchemaVersion": 1}' -replace '(?<=:\s*)(\d+)(?=\s*})', '"$1"' | ConvertFrom-Json

cjerrington,
@cjerrington@mstdn.social avatar

@falkheiland this might work! I’d probably have to save a temp file, then go line by line, and update if needed.

I’d have to play with this too if there is another key and the , comma is in the line too. Don’t want:

“SchemaVersion”: “1,”

cjerrington,
@cjerrington@mstdn.social avatar

@falkheiland could the regex be to the end of the line or , instead of the } ?

falkheiland,

@cjerrington you can include possible chars in a group like (,|}) if i understand it right

'{"SchemaVersion": 1}' -replace '(?<=:\s*)(\d+)(?=\s*(,|}))', '"$1"' | ConvertFrom-Json

cjerrington,
@cjerrington@mstdn.social avatar

@falkheiland This worked great! Some of the numbers have decimals in them, so they are not in the capture group.

For the Days it was working, but for TotalHours its not.

"SinceFull": {
"Days": "234",
"TotalDays": 234.92772020246295,
"TotalHours": 5638.2652848591106,
},

regex is fun... and appreciate the assistance!

cjerrington,
@cjerrington@mstdn.social avatar

@falkheiland I think I got it. The capture group is any number(s) dot any number(s)

((\d+|\d+.+\d+))

Full Regex: $data -replace '(?<=:\s*)((\d+|\d+.+\d+))(?=\s*(,|}))', '"$1"'

cjerrington,
@cjerrington@mstdn.social avatar

@falkheiland Another update. Had to get a little more aggressive with regex as there were also digit(s) dot digit(s) letter dash digit(s). Also saw I have some alpha characters.

...
"FileExists": true,
"Terabyte": 4.01325523853302E-05,
...

$data -replace '(?<=:\s)(([a-z]+|\d+|\d+.+\d+|\d.+))(?=\s*(,|}))', '"$1"'

Now I think we are complete. I am making sure I create a function out of this and saving it.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • Powershell
  • DreamBathrooms
  • ngwrru68w68
  • modclub
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • InstantRegret
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • mdbf
  • GTA5RPClips
  • JUstTest
  • tacticalgear
  • normalnudes
  • tester
  • osvaldo12
  • everett
  • cubers
  • ethstaker
  • anitta
  • provamag3
  • Leos
  • cisconetworking
  • megavids
  • lostlight
  • All magazines