How do I Convert CSV to JSON Correctly

Get help using Construct 2

Post » Thu Aug 13, 2015 3:47 am

Hi, I just discovered, with help online, that when I use the CSVtoArray plugin, and then download it into a .json file, it is putting all my numbers into quotation, which means they're been converted to strings. I have a large excel file with the first row being used for column titles and also some rows being used for dialog. But mostly, there is a lot of number data. Is this something that shouldn't be done? Even when I use an online converter, from .csv to .json, it wants to do this. There are no quotation marks in the .csv file. Am I supposed to put quotation marks around any word or phrase in any box of the spreadsheet?

Anyone have a better, easier method for getting this data into an array? I update the data quite a lot, so it needs to something fairly quick and easy. Any ideas are appreciated.

Thanks
B
4
Posts: 29
Reputation: 264

Post » Thu Aug 13, 2015 9:12 am

As I understand it, I think, your problem is that your JSON file has "992.34" as data rather than the constant 992.34 ? Is this right ?

You can fix this with an editor that does regex search and replace like Sublime Text, but probably the simplest way is to write a script in (say) Python which parses the JSON file looking for numbers in quotes and replaces them. Using re module it would probably be half a dozen lines tops if the JSON was formatted sensibly.
My tutorial through development blog is at http://myconstruct2dev.blogspot.co.uk/
B
12
S
3
Posts: 221
Reputation: 1,328

Post » Thu Aug 13, 2015 5:02 pm

Hi, thanks for your reply, though it requires a bit more programming knowledge than I possess. How do most people input large files of data into the arrays in their games? Like I mentioned, I'm using Rex's CSVtoArray plugin to put the csv into an array, then saving it as a .json file, which I can import into the game's files. I like using Excel to edit/update the data, and even though it is all turned into strings, I've recently learned that I can use "str" and "int" functions in events and actions to correctly interpret the data. But maybe someone has a better, more efficient method starting with Excel.

Thanks
B
4
Posts: 29
Reputation: 264

Post » Thu Aug 13, 2015 6:40 pm

CSV has no data type distinction, everything is basically a string. It's in your interpretation of the data that you can decide if it's an int or a string.
B
67
S
28
G
24
Posts: 970
Reputation: 18,675

Post » Thu Aug 13, 2015 7:28 pm

One way would be going through the array after import with the csv plugin and then convert all numbers to their appropriate type (int, float). After that save the array as json.
Image
Like this.

If you only want to allow each column to contain one type, you have to do type checking for the whole column up front and then convert everything to the type. Though probably that's not necessary.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Thu Aug 13, 2015 11:01 pm

Thanks for the tips and the example. I'll give it a try.

Edit: It works perfectly, actually. THANKS. If you could mention a website where the type of symbols you used in the expressions are explained/defined that would be helpful. I'd like to know what the expression said, and how to create these kind of expressions myself.
B
4
Posts: 29
Reputation: 264

Post » Fri Aug 14, 2015 5:16 am

It's regex, there are many pages that list the basics
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786


Return to How do I....?

Who is online

Users browsing this forum: el3um4s, Ryan Kocourek, TylerJS and 9 guests