Paste JSON string into Visual Studio

14,327

Solution 1

Use verbatim string literals (MSDN) by affixing the string with @. Then replace quotes (") with double quotes ("")

This is much more readable than escaping the quotes using backslash.

JObject.Parse(
@"{ 
    ""key"" : ""Foo"", 
    ""format"" : ""Bar""
}");

Solution 2

ReSharper does it seamlessly but of course, it costs money.

There's also a free VS extension that does that, here: https://marketplace.visualstudio.com/items?itemName=martinw.SmartPaster2013

Solution 3

For my tests with lots of JSON I like to store them in ".json" files embedded into the project DLL.

Note you need a use System.Reflection for this to work.

public string GetFileFromManifest(string path){
    using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path))
    {
        stream.Position = 0;

        using (var streamReader = new StreamReader(stream))
        {
            return streamReader.ReadToEnd();
        }
    }

}

Call with the DLL path i.e:

GetFileFromManifest("The.Name.Of.The.Dll.FolderNameOfEmbeddedFiles.Filename.json");

Documentation for GetManifestResourceStream: https://msdn.microsoft.com/en-us/library/xc4235zt(v=vs.110).aspx

Also, you could add the JObject.Parse to this method or create a new method to get it as JSON.

Solution 4

Put the JSON in a dedicated .json file in some directory (e.g. TestData/data.json) and load the file contents as a string. Make sure to set the file property of data.json to Copy to output directory.

// File Loader

public static class ReadJsonFile
{
    public static string GetFileFromDisk(string path)
    {
        var absPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        return File.ReadAllText(absPath + path);
    }
}



// Call the loader
var resultString = ReadJsonFile.GetFileFromDisk("/TestData/data.json");
JObject.Parse(resultString);

This is cleaner and saves you from mingling with large JSON strings. Which can be a pain.

Solution 5

Paste, highlight the selection, Find and Replace " for \"

Share:
14,327
Philip Bergström
Author by

Philip Bergström

I am an Engineering Physicist based in Stockholm, with a Master's Degree in Machine Learning from KTH Royal Institute of Technology.

Updated on June 13, 2022

Comments

  • Philip Bergström
    Philip Bergström almost 2 years

    I am running some C# Unit Tests in Visual Studio using JSON strings I copied from my database such as:

    { 
        "key" : "Foo", 
        "format" : "Bar"
    }
    

    I want to parse the JSON string to a JObject in my code:

     JObject.Parse(
        "{ 
            "key" : "Foo", 
            "format" : "Bar"
        }");
    

    However, I can't do this because Visual Studio won't escape the quotes inside the string.

    I know I can add escape characters, however, the strings could be very long, which means manually adding the escape characters would take a very long time.

    What is the easiest way to parse my copied JSON string into a JObject?