How do I easily convert from CSV to C2-friendly JSON?

  • Several students in my introductory game programming class are working on final projects that rely heavily on two-dimensional arrays. Arrays are one of the few things that seem slightly more complicated in C2 than they would be in a different programming environment.

    Ideally, it would wonderful if my students could create their arrays in a program like Excel and then convert the data to a properly formatted JSON file. There doesn't seem to be any way of doing this that is both free and easy. The most straight-forward solution is reverse engineering an existing JSON file in a text editor, copying and pasting to insert new rows. But this approach is a pain in the butt.

    It occurred to me that it might be possible to create a Word mail-merge that will pull fields from an Excel file and then convert them to JSON formatted output. Assuming we turn off smart quotes, that could work. But....

    There *has* to be a better way of doing this. I would be grateful for any advice from the C2 community.

    Thanks!

    Aaron

  • Arrays aren't that hard to work with. However, as soon as you want to externalize your data, things get complicated a little bit. Your only recourse is the strict JSON format that you get when you use the "Array.AsJson" expression or when you invoke the "Download as JSON" action.

    Knowing that, it should be possible to create a script that can switch between plain CSV to C2/3 JSON array format. If you are using Excel, I guess a VBA script could do the trick.

  • Thanks! As you note, the problem is when people want to externalize their data. Using the event sheet to manually add items to the array one index at a time is a bit of a chore.

    Considering the size and energy of the Construct community, it seems weird that a tool hasn't been developed that would make this easier. Before I go to the hassle of writing a script that will handle this, I figured that it was worth touching base with the community to see if someone has already invented this particular wheel.

  • adelwich Is there a reason why you need JSON?

    You can install Rex's CSV plugin:

    It's great and I'm using it heavily in my games.

    Another option would be parsing CSV into 2D-arrays with events.

    This is quite easy -

    First loop breaks the file into lines using tokencount/tokenat and line break as separator.

    Second loop breaks each line into values with tokencount/tokenat and comma as separator.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Another option would be parsing CSV into 2D-arrays with events.

    This is quite easy -

    First loop breaks the file into lines using tokencount/tokenat and line break as separator.

    Second loop breaks each line into values with tokencount/tokenat and comma as separator.

    This is fine when you can be 100% sure that your values won't contain linebreaks nor commas. Otherwise, parsing CSV requires a bit more work than that.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)