How do I Convert CSV to JSON Correctly

1 favourites
  • 7 posts
  • 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

  • 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.

  • 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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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.

    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.

  • 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.

  • It's regex, there are many pages that list the basics

    http://www.w3schools.com/jsref/jsref_obj_regexp.asp

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