1

I want to add an indent to a string in the text file I am writing .

Below is the output I am getting.

    {
      "Value":  [             
      {
        "UserPrincipalName": "abc.onmicrosoft.com",
        "MobilePhone": "12121212"
      },
      {
        "UserPrincipalName": "abcde.onmicrosoft.com",
        "MobilePhone": " "
      },
      {
        "UserPrincipalName": "abc.onmicrosoft.com",
        "MobilePhone": "4545457815"
      }
      ]
    }

Below is the output I want.

   {
      "Value":  [             
        {
          "UserPrincipalName": "abc.onmicrosoft.com",
          "MobilePhone": "12121212"
        },
        {
          "UserPrincipalName": "abcde.onmicrosoft.com",
          "MobilePhone": " "
        },
        {
          "UserPrincipalName": "abc.onmicrosoft.com",
          "MobilePhone": "4545457815"
        }
      ]
    }

I tried by adding \t but still, the last three brackets are not getting indented.

string.Format("{0}\n{1}{2}\n{3}\n{4}", "{", "  \"Value\":  [", "  " + json, "  ]", "}");

How can I achieve the above?

Thanks in Advance.

dhruvil29
  • 211
  • 1
  • 10

1 Answers1

0

Have you tried loading your json into something like newtonsoft and write it out again allowing the library to handle the formatting? You might try one of the answers in this question:

How do I get formatted JSON in .NET using C#?

JustAnotherDev
  • 546
  • 2
  • 8