2

I am wanting to know if it is possible to pickup JSON text from a server using a GET request, and then output that data into a local file in JSON format.

The only thing I can seem to find is this call:

File.WriteAllText(AgentWorkingDirectory & "\json.txt", JsonConvert.SerializeObject(return_message))`

Which only seems to give me a continous line of unformatted text.

So instead of this:

{
    "AlertingRules":[
        {  
            "RuleId":1,
            "Name":"Unprocessed Directory",
            "RuleConditions":[
                {  
                    "Name":"FileCount",
                    "FileName":"",
                    ...

I get this:

{"AlertingRules":[{"RuleId":1,"Name":"Unprocessed Directory","RuleConditions":[{"Name":"FileCount","FileName":null,...
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Riples
  • 1,167
  • 2
  • 21
  • 54
  • That compact format makes a lot of sense, as it saves disk space. You can still open the file with any editor that understands JSON and it will show it pretty-printed. – Thilo Aug 07 '17 at 03:09
  • Okay, well when I open the file in a JSON editor it still remains unformatted. – Riples Aug 07 '17 at 03:14
  • 1
    @Riples If your content is already in JSON format and known to be well-formed, why not just write it to disk directly as you received it? – Brad Aug 07 '17 at 03:22
  • 1
    @Riples This answer about reformatting the JSON from notepad++ may help: https://stackoverflow.com/a/5083037/6638533... – samAlvin Aug 07 '17 at 04:28

1 Answers1

5

Add the the formating parameter to your serialze call

File.WriteAllText(AgentWorkingDirectory & "\json.txt",JsonConvert.SerializeObject(return_message, Newtonsoft.Json.Formatting.Indented))

See Json.NET Documentation

David Sdot
  • 2,343
  • 1
  • 20
  • 26