0

I am trying to map a large/complex/valid JSON string - I only need a set of nested keys as a string array, the rest can be ignored.

I am doing this in Typescript and would like to parse to a JSON Object such as

export interface OpenApi {
  paths:      string[];
}

Here is the [relevant part] json:

{
  "openapi": "3.0.1",
  "info": {
    "title": "title",
    "version": "2018-05-10"
  },
  "paths": {
    "/api/path1": {"ignore" ...},
    "/api/path2": {"ignore" ...},
    "/api/path3": {"ignore" ...}
  }
  ...
}

to map to the above object array of strings

OpenApi.paths = ["/api/path1", "/api/path2", "/api/path3"]

the rest of the json aside from the keys can be ignored.

Any help appreciated! Thank you

Andrew Eells
  • 735
  • 12
  • 30
  • You described needing "parsing"; is the JSON a string, or did you already apply [`JSON.parse`](https://stackoverflow.com/q/4935632/1426891) to it? Do you simply want to [cast the result to your OpenApi interface](https://stackoverflow.com/a/13320802/1426891), or do you actively want to [validate](https://stackoverflow.com/q/71996566/1426891) the JSON to ensure it conforms to your expected interface? – Jeff Bowman Nov 07 '22 at 17:57
  • Hi Jeff. I haven't applied JSON.parse yet. It's a large/complex/valid JSON string and I only need those /api paths as a string array. Just trying to figure out how to best resolve down to that nested 'paths' object... If there's a better way to solve it in Typescript that's cool. – Andrew Eells Nov 07 '22 at 19:17

1 Answers1

0

So I ignored resolving to an object in the end, as:

Object.entries(jsonObj.paths).forEach(([key]) => {
    console.info(key);
});

Prints

'/api/path1'
'/api/path2'
'/api/path3'
Andrew Eells
  • 735
  • 12
  • 30