JSON Attributes with numbers as name

Hi there,
i try to write a small little reporting app for our projectmanagment tool at work using haxe.

Now I’ve got a problem:
The REST API of the pm tool returns some json data. Everything worked fine.
In a special case I get some dynamical Json like:

{ "attributes_values": { "1": "240 min" },
    "user_story": 1,
    "version": 2


{ "attributes_values": {"122": "tgu", "97": "1,5", "98": "2017-11-10"},
 "version": 5,
 "user_story": 756

Anyone an idea how I can get the values of the “attribute_values”? They are dynamically. In the first example I have to read the value of “1”(=“240 min”) and in the second example I have to read the value of “122”(=tgu)

I tried a lot: Using maps, parsing the value of “attribute_values” into a new json-object and read it with object.122 and many more… but nothing worked for me.

I hope someone can help me.

Thanks a lot.

import haxe.DynamicAccess;
import haxe.Json;

class Test {
    static function main() {
        var obj:DynamicAccess<String> = Json.parse('{ "1": "240 min" }');
1 Like

Or you can use the json2object library if you want to avoid Dynamic. It supports deserialization into a Haxe Map, among other things.

class Main {
	public static function main() {
		var parser = new json2object.JsonParser<Data>();
		var obj = parser.fromJson('{ "1": "240 min" }', "data.json");

typedef Data = Map<Int, String>;

Thank you for the fast help!

I took the json2object library! :slight_smile:

No matter what programming language you’re talking about, every language has one-or-more “JSON magic encoder/decoder rings” that are known to work correctly, and you should always use them blindly.

  • Treat the JSON string as a “black box” and hand it to the magic decoder-ring to be turned into a data structure. Be prepared that the decoder-ring might throw an exception if it is handed ill-formed JSON … never assume that “the other guy” knew what he was doing, and recover graciously if he didn’t. (Or, if he’s trying to cause trouble…)

  • When ready to transmit, use the magic encoder-ring and send the exact string that it gives you.

  • Exactly the same principles apply, of course, to XML and SOAP. There are many subtle nuances to be dealt with, and somebody else has already figured them out for you.

Actum Ne Agas: “Do Not Do A Thing Already Done.”