How to Convert JSON/Anonymous Structure Into Nested Map?

Xinbei

  • *
  • Posts: 8
Hello,

I want to convert JSON strings/files into map attributes in my game. I have only used drag and drop thus far but I have figured out how to use the code block to parse a JSON string. I set the parsed JSON result as an anything attribute. My problem is that I can't convert the anything attribute into a map nor can I use map blocks on it despite it looking exactly like a map when I print it.

 How can I either directly convert the JSON into a map with nested arrays/maps or convert the Anonymous Structure from the parse function into a map?

An example of a JSON I want to convert into a map is:

Code: [Select]
{
  "apple": {
    "value": 10,
    "health": 15,
    "tags": [
      "fruit",
      "fiber",
      "sweet"
    ]
  },
  "lemon": {
    "value": 5,
    "health": 10,
    "tags": [
      "fruit",
      "citrus",
      "sour"
    ]
  },
  "ham": {
    "value": 50,
    "health": 50,
    "tags": [
      "salty",
      "meat"
    ]
  }
}

Attached is the block I've been trying and the results of printing the anything attribute.

merrak

  • *
  • Posts: 2199
You might end up just having to loop through all elements in the entire structure jsonout and build the map.

I haven't used JSON though, so I can't say that's the only way. I looked at the Haxe documentation and it looks like it's doing something similar to what the FAST routines give you when you parse an XML document. I built a FAST extension if you want to look at it as an example. Not sure how relevant it'll be.

One thing I'll note though is that I never bothered converting my XML documents to maps. Instead, I built that extension so that it provides blocks that access the returned structure directly. That might be the intention here as well. Is there a reason you have to build a map?

squeeb

  • Posts: 1419
Are you trying to parse the json?  And set it as an attribute?  I havent done maps before.. i have set parsed json to number and text attributes  though.. i can post code in the morning

Edit
Sorry i think i misread that :p

Xinbei

  • *
  • Posts: 8
Thanks for the responses,

Seems like there's no easy way to convert JSON to a map attribute. Look like my options at this point are:

1. Convert the JSON to a CSV and use the text split function to convert CSV into a map somehow (not ideal since I'll have to create a different split event for each type of JSON)

2. Convert the JSON to XML and use the XML API in haxe to iterate over every single node and somehow setup  a map during the iterations

3. Hire a coder who knows how to use haxe to write me a JSON to Map attribute converter (any suggestion to where I can find  and hire a coder for this?)

None of these options are ideal and hopefully in the future Stencyl will have a direct converter for JSON to map attribute like in Game Maker Studio 2. I think JSON is incredibly useful and convenient and if I could convert one into a global map attribute it would allow me to easily create a complex yet accessible database for my games.

If anyone has any other work around method, please let me know I would greatly appreciate any suggestions that would allow me to convert JSON or even XML into a map attribute

merrak

  • *
  • Posts: 2199
I think the trick will be to use the Reflect class. (Example). Reflect provides a fields function that gives you an array, which can then be iterated over. If I had a few hours to spare I could probably write an extension that does this. I don't see that happening until at least this weekend.

The FAST extension I put on the extensions forum doesn't have an XML -> Map converter, but it does have an iterator that would make it a lot easier to write one.

Xinbei

  • *
  • Posts: 8
Thanks Merrak, if you have the spare time  I would greatly appreciate the extension and I'll help you in any way I can. In the meantime, I'm going to look into the reflect class and your XML iterator to see if there's any option there.