Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
105 lines (68 sloc) 3.88 KB

Add a Nuget to a Workbook

This workbook shows how to include and use a Nuget package from a workbook. The demo uses Json.NET to serialize and de-serialize a C# class.

  • Add the Json.NET Nuget

  • Create the C# object

  • Serialize & Deserialize using Json.NET

...borrowing some JSON from James Montemagno’s sample.

Add Json.NET

  1. Start by adding a Nugets — choose File > Add Package... to choose Nuget packages to reference.

⚠️ No need to follow these instructions - the package is already referenced by this workbook

and then

For this workbook, Json.NET was chosen, which results in the #r clause being added automatically:

#r "Newtonsoft.Json"

Check the workbook’s source to see how this is persisted (if you’re curious):


  1. To write some code using the Nuget package, first add a using statement:
using Newtonsoft.Json;

Create C# object

  1. Now setup a C# class and some test data...
public class Monkey
	public string Name { get; set; }
	public string Location { get; set; }
	public string Details { get; set; }
	public string Image { get; set; }
	public int Population { get; set; }
var monkey1 = new Monkey{Name="Blue Monkey", Location="Central and East Africa"};
var monkey2 = new Monkey{Name="Golden Lion Tamarin", Location="Brazil"};
var monkeyList = new List<Monkey> {monkey1, monkey2};

Serialize to JSON

  1. Using the JsonConvert class from the Nuget package, the C# data can be converted to JSON with a single line of code!
var toJson = JsonConvert.SerializeObject(monkeyList);

Deserialize from JSON

  1. To test deserialization, here is a more complete JSON string (thanks to James Montemagno’s sample here):
var fromJson = @"[{""Name"":""Baboon"",""Location"":""Africa & Asia"",""Details"":""Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae."",""Image"":"""",""Population"":10000},
{""Name"":""Capuchin Monkey"",""Location"":""Central & South America"",""Details"":""The capuchin monkeys are New World monkeys of the subfamily Cebinae. Prior to 2011, the subfamily contained only a single genus, Cebus."",""Image"":"""",""Population"":23000}]";
  1. Converting a JSON string back to C# objects is also a one-liner - and results in a C# collection that can be utilized in app code:
var monkeys = JsonConvert.DeserializeObject<List<Monkey>>(fromJson);

The example above uses a JSON string, but the data could also be loaded from a local file or an internet download (either a file reference or REST web service endpoint).

One More Thing...

To load the JSON string from a remote website, just add System.Net to the workbook

using System.Net;

and then use WebClient to download it before serialization

WebClient client = new WebClient();
var response = client.DownloadData (""); // GET
var json = System.Text.Encoding.UTF8.GetString(response);
var monkeys = JsonConvert.DeserializeObject<List<Monkey>>(json);