Cerulean
Cerulean

Reputation: 6033

Trying to parse 'JSON' in TypeScript: but is this JSON?

First, I am duly embarrassed. I should know this.

I'm learning TypeScript and when I try to parse the data below (which I was given, told it was JSON) using JSON.parse() TypeScript complains that the data is not a string. I get that -- it isn't. If I wrap the entire thing in quotes, however, It doesn't work either.

I find myself lost and rather embarrassed as I'm a mid-level programmer. What am I missing here?

For what it's worth, I'm importing a large data array (the array is huge, I've only given the first two entries below) in my React app using

import data from './data/data.json'

and then attempting to convert it to a JS array using

const parsedArray:Array<any> = JSON.parse(data).

But of course, as per above, it fails.

I want to end up with a JS data structure where the numbers are numbers again, not strings.

The data is as follows:

 [{"_id":"5bb238cbf839d08d65633450","index":0,"guid":"390322bb-3e93-4196-853a-e6e7a6f455bb","isActive":true,"balance":"$2,193.03","picture":"http://placehold.it/32x32","age":62,"eyeColor":"brown","name":"Holder Joyce","gender":"male","company":"RAMEON","email":"[email protected]","phone":"+1 (942) 548-3425","address":"168 Ovington Court, Eagleville, Oregon, 1098","about":"Voluptate eiusmod exercitation ad aliqua ullamco ex officia. Fugiat deserunt anim commodo anim reprehenderit cillum cupidatat magna anim. Do in sint elit deserunt sint amet veniam culpa et veniam.\r\n","registered":"2017-09-21T02:55:55 -02:00","latitude":53.144539,"longitude":111.973422,"favorites":{"cavatappi":3.1604049168546533,"fusilli":47.41214262743145,"spaghetti":36.351431171952584,"tagliatelle ":16.540786406823592,"farfalle":29.91825944401951}},{"_id":"5bb238cbf839d08d65633450","index":0,"guid":"390322bb-3e93-4196-853a-e6e7a6f455bb","isActive":true,"balance":"$2,193.03","picture":"http://placehold.it/32x32","age":62,"eyeColor":"brown","name":"Holder Joyce","gender":"male","company":"RAMEON","email":"[email protected]","phone":"+1 (942) 548-3425","address":"168 Ovington Court, Eagleville, Oregon, 1098","about":"Voluptate eiusmod exercitation ad aliqua ullamco ex officia. Fugiat deserunt anim commodo anim reprehenderit cillum cupidatat magna anim. Do in sint elit deserunt sint amet veniam culpa et veniam.\r\n","registered":"2017-09-21T02:55:55 -02:00","latitude":53.144539,"longitude":111.973422,"favorites":{"cavatappi":3.1604049168546533,"fusilli":47.41214262743145,"spaghetti":36.351431171952584,"tagliatelle ":16.540786406823592,"farfalle":29.91825944401951}}]

Any help as to what I'm missing would be greatly appreciated. I've spent 45 minutes trying to figure this out and I'm missing something crucial. As I said, I was given the file of data, with the name data.json, so I thought, naively, that I could just read it in and parse it.

Upvotes: 0

Views: 75

Answers (1)

Evert
Evert

Reputation: 99816

When you import a file, or use require() typescript / node.js will parse the file for you and return the result.

So instead of:

const parsedArray:Array<any> = JSON.parse(data).

You use:

const parsedArray:Array<any> = data;

Upvotes: 1

Related Questions