JSON in Different Languages

jessekphillips

Jesse Phillips

Posted on October 19, 2022

JSON in Different Languages

JSON is a data format that uses Javascript notation to layout the structure of the data. But Javascript isn't the only language that can represent these structures in code.

// Javascript
{
  "firstitem" : "first data", 
  "list" : [
    "bold",
    "italics"
  ]
}
Enter fullscreen mode Exit fullscreen mode

I'm utilizing the two main structures, generally referred to as 'object' {} and 'list' [] because that is how Javascript talks of them. Most languages can represent these in one or more ways. These structures don't produce JSON, but libraries (beyond this article) can make the translation.


// Lua
{
  firstitem = "first data", 
  list = {
    "bold",
    "italics"
 } 
}
Enter fullscreen mode Exit fullscreen mode

Lua is almost the same with just object {} notation even for lists. The equal rather than colon is used for association. We also don't quote the field names, though Lua does allow spaces like JSON, ["first item"] = "first data".

// Python
{
  'firstitem' : 'first data', 
  'list' : [
    'bold',
    'italics'
  ]
}
Enter fullscreen mode Exit fullscreen mode

Python looks really close, but string literals are single not double quote. We can also use tuples to build your list.

// Python
{
  'firstitem' : 'first data', 
  'list' : (
    'bold',
    'italics'
   )
}
Enter fullscreen mode Exit fullscreen mode

This makes the list syntax different, but you'll get the needed output.


// c#
public class Data
{
  public string firstitem { get; set; }
  public List<string> list { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

C# is a statically typed language so we define a type that matches the structure. However C# has many different features and we can get away with much more.

// c#
new {
  firstitem = "first data", 
  list = new List<string> {
    "bold",
    "italics",
 } 
}
Enter fullscreen mode Exit fullscreen mode
// c#
Dictionary<string, object> data = new {
  { "firstitem", "first data" },
  { "list", new List<string> {
    "bold", "italics",
  }},
};
Enter fullscreen mode Exit fullscreen mode

This one is a cheat. The specification of object just places the data in to the libraries "general" representation, for example Newtonsoft's Json library would use a JObject, JArray for the type you could cast to.


// D
public class Data
{
  public string firstitem;
  public string[] list;
}
Enter fullscreen mode Exit fullscreen mode
// D
new class {
  auto firstitem = "first data";
  auto list = [
    "bold",
    "italics"
  ];
}
Enter fullscreen mode Exit fullscreen mode
// D
Variant[string] data = [
  "firstitem" : Variant("first data"),
  "list" : Variant([
    "bold",
    "italics",
 ])
];
Enter fullscreen mode Exit fullscreen mode

Unlike in C# where all types have an object representation, there is not parent for all types with Auto-Boxing. D does however provide a type, Variant, which can hold other types and that needs to be explicitly created.

💖 💪 🙅 🚩
jessekphillips
Jesse Phillips

Posted on October 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related