How to convert a JSON string to regular string? - powershell

I have the following connection string
{"Authentication Kind":"UsernamePassword","Username":"someID1","Password":"Yu#gh456!ts","EncryptConnection":true}
I am trying to show the password with ******* , so I need to convert it to SqlConnectionStringBuilder type as its easy to replace properties with that. I am using it just fine for non-json structured strings
[System.Data.SqlClient.SqlConnectionStringBuilder]::New('{"Authentication Kind":"UsernamePassword","Username":"someID1","Password":"Yu#gh456!ts","EncryptConnection":true}')
i am getting this error:
Cannot convert value to type
System.Data.SqlClient.SqlConnectionStringBuilder
How do I convert it to a connection string thats acceptable by the SqlConnectionStringBuilder type?

You could convert it to a PSObject first.
$Json = '{"Authentication Kind":"UsernamePassword","Username":"someID1","Password":"Yu#gh456!ts","EncryptConnection":true}'
$Sql = $Json | ConvertFrom-Json
$Sql.Password
From there you can use the properties to create a new string, or convert it back to JSON.

Related

Why does casting this string in this way work but not the other way?

I am reading data from Firestore. I was getting an error that said type 'Timestamp' is not a subtype of type 'String' in type cast, so I changed the createDate line below to use .toString() instead of doing as String. This solved the problem, but why did this work?
Notification.fromJson(Map<dynamic, dynamic>? json): //Transform JSON into Notification
createDate = (json?['createDate']).toString(), //This works
modifiedDate = json?['modifiedDate'] as String; //This gives the error: 'type 'Timestamp' is not a subtype of type 'String' in type cast'
The format of both of these fields is October 5, 2022 at 10:49 PM UTC-5.
Like it is said in error message, you are getting this error, because the value's type, which is coming from json is Timestamp, not String. It is better to make createDate and modifiedDate fields in Timestamp type instead of String, because it provides it's own methods, that make your work easier. Converting to String (and probably, you are parsing this String in somewhere) is redundant.
In Flutter every object class have toString() method, which is return a value with String type
(json?['createDate']).toString();
So if you write that code above, it will return a value with String type and then assign that value to createDate variable. It means you not assign json?['createDate'].
modifiedDate = json?['modifiedDate'] as String;
That line above will error because you assign that json to modifiedDate which is have different type. Cast (as) only work if the object/variable have same hierarchy, like
Child child = Child();
Parent parent = child as Parent();
json?['createDate'] is a Timestamp type object that just contains numbers describing a point in time. As you can see from the API documentation, it has a method called toString that knows how to convert that to formatted date string. Since a Timestamp it is not a subclass of String, Dart does not allow it to be cast to one. Perhaps you want to cast it to Timestamp instead, since that's what it is?

encoding and decoding base64.in aws appsync resolver

I have a resolver that receives an argument "nextToken" ($ctx.args.nextToken) which will be in base64 string. In the request mapping template, I need to convert nextToken from base64 string into ascii string. I know that Appsync has $util.base64Decode(String) : byte[] but this function gives back byte[] but I want back an ascii string.
In addition, I will need to create base64 string from ascii string in the response mapping template. Again Appsync provides a function $util.base64Encode( byte[] ) : String but I don't know how to change my ascii string into byte[].
Anybody has any idea how to handle both situations. Thanks in advance.
VTL template $util functions for base64 accept a string and return a string.
Example VTL template:
#set($base64 = $util.base64Encode("Hello World"))
{
"label": "$util.base64Decode($base64)"
}
Returns "Hello World"

Creating a Custom Sorter for CollectionView.CustomSort in Powershell

I am kinda' new to Powershell and have searched the ends of the earth for a solution, but I can't seem to find anything. I hope it's still doable.
I am trying to sort a CollectionView by date that's in string format. In my custom sorter, I want to convert the date strings (M/dd/yyyy) into Date objects and have the Comparer sort it by date. My CollectionView is filled like this:
$stuff = [System.Collections.ObjectModel.ObservableCollection[System.Object]](ImportClixml -Path $path)
$view = [System.Windows.Data.CollectionView]([System.Windows.Data.CollectionViewSource]::GetDefaultView($stuff))
The XML data is filled with Properties that are String and Boolean values. the Date value I am trying to sort is a String. The CollectionView is the bound to a DataGrid like so: $myDataGrid.ItemsSource = $myCollectionView
Can I just make a function that sorts and just point to it somehow?
function customSorter{ #Do Stuff }
$myCollectionView.CustomSort = customSorter
The CustomSort property takes in a System.Collections.IComparer object. I'm not sure how to approach this. I would prefer to just keep it all in powershell 4 and not import C# code or something like that. Any help is appreciated.
You can format your string dates into a datetime object
For example, yyyy-MM-dd format will be easy to sort.
If you want to convert your string dates you can use :
$timeinfo = '6/06/2017'
$template = 'M/dd/yyyy'
$newDate = Get-Date([DateTime]::ParseExact($timeinfo, $template, $null)) -Format "yyyy-MM-dd"
Result is : 2017-06-06

How to convert data in unityscript

Now I have data like this "1234567890", first it is System.Object
I want convert it to long.
I tried like this
(long) data
data as long
data as System.Int64
It seems they all not work,anyone can help me?
First got your object as string if needed:
string mystringnumber = mysystemobject as String;
You can use this:
long.parse(mystringnumber);
or:
if (long.TryParse(mystringnumber,null))
{
dosomething();
}

convert a string collection into a string array in powershell

as the title says how can I convert a string collection into a string array in powershell don't know where to start with this as I am new to powershell. Would it be anything like String[])$viewFields.toArray(new String[0])
You should try type conversion ([object[]]). Here is an example:
$x = New-Object System.Collections.ArrayList # Definitely not an object array
([object[]]$x).GetType() # Object[]
To create an array out of anything surround with #(...). To create an array of a specific type, System.Array has some factory methods and use [type] to get a Type object. Thus
$theArray = #($viewFields.toArray([array]::CreateInstance([string], 0)))