0

I am html scraping a string that is formatted as follows (shortened for brevity):

"[{'name': 'Roddy Ricch', 
'type': 'event', 
'id': 'Z7r9jZ1Ae-Fuw', 
'url': 'http://www.ticketsnow.com/InventoryBrowse/TicketList.aspx?PID=2848372', 
'images': [
    {'ratio': '3_2',
     'source': '6.0', 
........."

with multiple levels of arrays and objects. I would like to operate on the actual objects the string is meant to represent rather than 1 String Object.

How to I turn this string into the actual objects it is meant to represent in TypeScript?

Gwynn
  • 553
  • 2
  • 5
  • 10

1 Answers1

0

This will need a code generation step because when the compiler type checks your code, it doesn't have information about the structure of the object which is being obtained at runtime.

You can extract some sample json, and use a code generator like json-to-ts to infer the types of this json. You can save the inferred types in a ts file and cast the return value of JSON.parse to that type.

lorefnon
  • 12,875
  • 6
  • 61
  • 93