10.10.1 Parsing JSON

Use the haxe.Json.parse static method to parse JSON data and obtain a Haxe value from it:

class Main {
  static function main() {
    var s = '{"rating": 5}';
    var o = haxe.Json.parse(s);
    trace(o); // { rating: 5 }
  }
}

Note that the type of the object returned by haxe.Json.parse is Dynamic, so if the structure of our data is well-known, we may want to specify a type using anonymous structures. This way we provide compile-time checks for accessing our data and most likely more optimal code generation because the compiler knows about types in a structure:

typedef MyData = {
  var name:String;
  var tags:Array<String>;
}

class Main {
  static function main() {
    var s = '{
      "name": "Haxe",
      "tags": ["awesome"]
    }';
    var o:MyData = haxe.Json.parse(s);
    trace(o.name); // Haxe (a string)
    // awesome (a string in an array)
    trace(o.tags[0]);
  }
}