1

So I'm currently working on a project that involves referencing the entire Pokemon pokedex, and as I was looking around for sources, I realized that Pokemon Showdown is open source and as a result, there's a full pokedex in the repository. However, when I opened up the file that contains the pokedex I found that it's not set up as a json file, but as something else entirely.

So, here are my questions:
1: What exactly is the data stored as? An array of objects?
2: How can I either convert the data to JSON or reference it as I would JSON(using key names)

glennsl
  • 28,186
  • 12
  • 57
  • 75
  • The entire JSON/ Object of Objects in this case is exported into a variable `BattlePokedex`. Maybe you should have a look at [this](https://www.sitepoint.com/understanding-module-exports-exports-node-js/) and [this](http://stackoverflow.com/questions/3922994/share-variables-between-files-in-node-js) to understand how exports work. – Sudheesh Singanamalla Feb 23 '17 at 02:00

4 Answers4

1

The data is a JavaScript object which is exported as a module for external files to use. What you can do is to use JSON.stringify to convert the object to JSON string.

const battlePokedex = require('data/pokedex.js');
const pokedexInJson = JSON.stringify(battlePokedex);

console.log(`Now it's the JSON representation of the JS object ${ pokedexInJson }`)
dawchihliou
  • 246
  • 1
  • 5
0
  1. Data is stored as JavaScript object the key is the name of each pokemon
  2. See below

Delete code below from the file and change the file name to .json and you get your JSON data.

'use strict';

exports.BattlePokedex = 
XY L
  • 25,431
  • 14
  • 84
  • 143
0

This is basically a module that exposes an object with all the pokedex data. You can import this module and it will return the data you want as an object. Here is a small example:

var pokedex = require('./pokedex.js'); //Path to file where is the module you want to import

For instances, imagine you want to get the data from Pikachu:

// pokedex is the module you imported, BattlePokedex is what they exposed 
// in the module, and the pikachu is the object containing the pikachu data
var pikachu = pokedex.BattlePokedex.pikachu;

I hope this helps but if you could give more context (pieces of code for instance) I can try and give a more complete answer.

Bruno Santos
  • 154
  • 1
  • 8
0

Figured it out, I ended up not converting it to json but just leaving it as an object in a separate file. For those with a similar problem in the future, just do the following:

If your object is in a separate file, add a script import to your html before your script import for the main javascript(it's the same way you would import jquery). Then change the .exports line to a variable declaration. For me it looked like this before:

exports.BattlePokedex = {

I then changed it to this:

var dex = {

To access the different variables, I used this line of code:

var pokemon = dex[name];