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

2
  • You described needing "parsing"; is the JSON a string, or did you already apply JSON.parse to it? Do you simply want to cast the result to your OpenApi interface, or do you actively want to validate the JSON to ensure it conforms to your expected interface? Commented Nov 7, 2022 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. Commented Nov 7, 2022 at 19:17

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.