Skip to content

Instantly share code, notes, and snippets.

@ideaflare
Created October 29, 2016 12:13
Show Gist options
  • Select an option

  • Save ideaflare/7d76d302c996cb530f99530fd73118e3 to your computer and use it in GitHub Desktop.

Select an option

Save ideaflare/7d76d302c996cb530f99530fd73118e3 to your computer and use it in GitHub Desktop.
// Add NuGet package FSharp.Data
#r "../packages/FSharp.Data.2.3.2/lib/net40/FSharp.Data.dll" //#r is needed in .fsx file. You can omit this line in .fs file.
open FSharp.Data
let apiUrl = "http://swapi.co/api/people/?format=json"
type People = JsonProvider<"http://swapi.co/api/people/?format=json">
type Planet = JsonProvider<"http://swapi.co/api/planets/1/"> // * planet was found by inspecting....
// assumption made, all planets have the same json structure/properties as planet #1
let luke =
People.Load(apiUrl).Results //Results is property of first page of people in this case.
|> Seq.tryFind (fun guy -> guy.Name.StartsWith("Luke"))
let lukePlanet = luke |> Option.map (fun guy -> guy.Homeworld) // * <--- that string
let lukeWorldInfo =
match lukePlanet with
| None -> "Couldn't find the guy, no info about his planet."
| Some(planet) -> Planet.Load(planet).Name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment