Featured

Convert JSON to csv in C#

Convert JSON to csv in C#

  • Given users.json as the json file
using (var csv = new ChoCSVWriter("users.csv").WithFirstLineHeader())
{
   using (var json = new ChoJSONReader("users.json") 
      .WithField("Username")
      .WithField("Sub", jsonPath: "$.Attributes[?(@.Name == 'sub')].Value", isArray: true)
      .WithField("EmailVerified", jsonPath: "$.Attributes[?(@.Name == 'email_verified')].Value", isArray: true)
      .WithField("GivenName", jsonPath: "$.Attributes[?(@.Name == 'given_name')].Value", isArray: true)
      .WithField("FamilyName", jsonPath: "$.Attributes[?(@.Name == 'family_name')].Value", isArray: true)
      .WithField("Email", jsonPath: "$.Attributes[?(@.Name == 'email')].Value", isArray: true)
      .WithField("UserCreateDate")
      .WithField("UserLastModifiedDate")
      .WithField("Enabled")
      .WithField("UserStatus")
   )
{
   csv.Write(json);
}
}
Advertisement
Featured

Check if the string is valid JSON in C#

C# validate json string

public static bool IsJsonString(string str)
{
     if (string.IsNullOrWhiteSpace(str)) { return false; }
     str = str.Trim();
     if ((str.StartsWith("{") && str.EndsWith("}")) || 
         (str.StartsWith("[") && str.EndsWith("]")))
      {
         try
         {
            var obj = JToken.Parse(str);
            return true;
          }
          catch (JsonReaderException jex)
          {
             return false;
          }
          catch (Exception ex) //some other exception
          {
             return false;
          }
      }
      else
      {
         return false;
      }
 }
Featured

Export Users from Cognito in AWS

Export Cognito Users in AWS

As of today, there is no way to directly export users from Cognito in AWS.

But we can use AWS CLI or AWS SDK to get the list of users.

  • First step is to install AWS CLI on your machine

Click here to download and install AWS CLI

  • Next, step is to configure AWS on your machine. To do this, open cmd (command prompt) and do the following –
$ aws configure
AWS Access Key ID [None]: YourAccessKeyId
AWS Secret Access Key [None]: YourSecretAccessKey
Default region name [None]: YourRegion
Default output format [None]: json

Replace the above with your values. For more info, click here.

  • To get the list of all users in Cognito, run the following command
aws cognito-idp list-users --region <region> --user-pool-id <userPoolId> 
--output json > users.json

The above will return the list of users in a json format. If you want to get the result in a table format, run the following command

aws cognito-idp list-users --region <region> --user-pool-id <userPoolId>  --output table > users.txt
  • Now if you want to convert the result json to csv. Use the following code snippet.
private static void ConvertJsonToCsv()
{
 using (var csv = new ChoCSVWriter("users.csv").WithFirstLineHeader())
 {
   using (var json = new ChoJSONReader("CognitoUsers.json")
             .WithField("Username")
             .WithField("Email", jsonPath: "$.Attributes[?(@.Name == 
              'email')].Value", isArray: true)
             .WithField("UserStatus")
         )
  {
     csv.Write(json);
  }
 }
}
Featured

How to replace space with underscore in an xml file using Notepad++

It is very easy to achieve this using a regex expression. Suppose we have below in an xml file and we want to replace the space inside DisplayName node with underscore.

Sample xml –

<User id="11068577">
	<UserId>11068577</UserId>
	<DisplayName>Dolcese Vita</DisplayName>
	<Address>Texas, US</Address>
</User>
  1. Open Notepad++
  2. Click Ctrl+H to open replace dialog box
  3. Add below in Find What:
(?i)(<DisplayName>.*?)[\s](?=.*</DisplayName>)

4. Add below in Replace with:

\1_\2

5. Result –

<User id="11068577">
	<UserId>11068577</UserId>
	<DisplayName>Dolcese_Vita</DisplayName>
	<Address>Texas, US</Address>
</User>


Interested in Cryptocurrency. Register and start investing here

Earn a side income by affiliate marketing. Learn here how to do it.

Featured

Restore Azure Database (.bacpac) file to SQL Server (.bak) file

Azure Sql Database is a fully managed relational database that provisions quickly, scales on the fly and includes built-in intelligence and security as well.

Azure Sql Database is a fully managed relational database that provisions quickly, scales on the fly and includes built-in intelligence and security as well.

Below are the steps to restore the database from .bacpac (Azure DB backup) file to .bak (SQL DB backup) file

  1. The first step is to export the Azure DB. For this, you need to login to your Azure portal and go to SQL database in your resource group. Click Export as shown below.






  2. After clicking on Export, you have to select the storage location and add the credentials as shown below.
    This process will take few minutes to finish depending on your database size.


    Note: It is good to select the storage location (blob storage) in the same resource group, if you have multiple resource groups



  3. After the export is finished, you will get the exported file as a .bacpac file in your selected storage. (In my case, it is blob storage container)




  4. Right-click on the .bacpac file you just created and download it locally





  5. The next step is to create .bak file from the .bacpac file you just downloaded. For this, you need to open SQL Server Management Studio (I am using SQL Server Management Studio v17.9.1).
    Right-click on Databases and select Import Data-tier Application.





    You will see the below screen. Now, click Next.







  6. Click on Browse and select the .bacpac file you downloaded from Azure in the previous step and click Next as shown below –





  7. Here you can change the database name or can keep the same name as your .bacpac file.
    You can leave the other settings as it is and just click Next again.





  8. Now, you can verify all the settings below and click Finish or you can click Previous and go back to the previous settings if you want to change anything.





  9. Now you will see the progress and once it is finished, you will see the below Operation Complete screen. If there is any error, you can click on that and see what is wrong, else you will get all Success





  10. You can see the newly restored database under the Databases folder.





  11. The next step is to create the .bak file.
    For this, right-click on the new DB and select Tasks -> Back Up… as shown below –





  12. Now, you will see the below screen.
    Remove the destination path that is pre-selected by clicking Remove as shown below.
    And then click on Add to select the path where you want to store your .bak file.





  13. After clicking on Add, you will see the screen below.
    Select the destination path/folder and add the desired File name. I have added TestDB12072019.





  14. Click OK and you will see it executing. Once 100% completed, you will see the following screen –





    Thats’s it! You have created SQL Server .bak file from Azure database .bacpac file.

    Now you can see the .bak file in the folder path you selected.




    Please leave a comment, if you have any questions.

Interested in Cryptocurrency. Register and start investing here

Earn a side income by affiliate marketing. Learn here how to do it.

How to Add a Container in Google Tag Manager

Add a Container in Google Tag Manager

  1. Open Google Tag Manager and login. You will see the the Accounts screen with a list of all existing GTM containers.

2. To create/add a new container, click the three dots on the right – next to the settings icon as below

3. Click on Create Container and you will see the below screen

Note: In order to Create a Container, you need to have permission for it. Please ask the account admin, if you don’t have it

4. Add the container name, e.g. techiespice.com and select the Target Platform, e.g. Web. Click Create and your container is ready.

5. Once you click Create, you will see the below screen with all the container settings (Tags, Variables, Triggers, etc.) along with the GoogleTagManagerID (marked in red) on the right.

6. Click the GoogleTagManagerID and you will see the popup with GTM script that you need to add to your site.

Interested in Cryptocurrency. Register and start investing here

Earn a side income by affiliate marketing. Learn here how to do it.

%d bloggers like this: