0

I have a text file containing a an array of objects in the following format:

[{
            "symbol": "MSFT",
            "side": "BUY",
            "price": 120,
            "quantity": 100
        },
        {
            "symbol": "KPMG",
            "side": "SELL",
            "price": 88,
            "quantity": 10
        }]

However, when I upload the file into my application, and store it as a variable, it gets stored as a string. Does anyone know how I can avoid doing this?

I want to be able to read the file in (which I can already do) and store that as an array. So I can go like data[1] and get access to

  {
        "symbol": "KPMG",
        "side": "SELL",
        "price": 88,
        "quantity": 10
    }

I also want to be able to go like:

data[1].side

and retrieve the field

blazerix
  • 770
  • 4
  • 8
  • 24
  • 1
    You're looking for a JSON parser. – SLaks Dec 08 '17 at 16:15
  • Hi Vipin, I read that link but JSON.parse doesn't seem to work on my file – blazerix Dec 08 '17 at 16:16
  • 1
    Why not? What did you do? What happened? Note that `JSON.parse()` does not take a path. – SLaks Dec 08 '17 at 16:17
  • I made an input button that reads my file in using FileReader. I used reader.readAsText and store the result into a variable. However, that variable stored my entire array as a string. I'm not able to access any of the objects inside the array because the entire file is treated as a giant string. – blazerix Dec 08 '17 at 16:18
  • 1
    You can use JSON.parse after reading the content – Vipin Kumar Dec 08 '17 at 16:23
  • 1
    The variable that stores the string is what you pass: JSON.parse(jsonStringVariable) – Aaron Dec 08 '17 at 16:26
  • it worked! thank you guys. Sorry for the duplicate question – blazerix Dec 08 '17 at 16:30

0 Answers0