0

I know that there are already multiple questions about parsing JSON files in Typescript, but non of these helped me.

I'm trying to parse the result of the license-checker output which is a json:

{
  "@angular/[email protected]": {
    "licenses": "MIT",
    "repository": "https://github.com/angular/angular",
    "publisher": "angular",
    "name": "@angular/core",
    "version": "10.1.4",
    "description": "Angular - the core framework",
    "licenseFile": "C:\\Users\\UC190\\workspace\\core-search-app\\node_modules\\@angular\\core\\README.md",
    "licenseText": "Angular\n=======\n\nThe sources for this package are in the main [Angular](https://github.com/angular/angular) repo. Please file issues and pull requests against that repo.\n\nUsage information and reference details can be found in [Angular documentation](https://angular.io/docs).\n\nLicense: MIT",
    "path": "C:\\Users\\UC190\\workspace\\core-search-app\\node_modules\\@angular\\core"
  },
  "@angular/[email protected]": {
    "licenses": "MIT",
    "repository": "https://github.com/angular/angular",
    "publisher": "angular",
    "name": "@angular/forms",
    "version": "10.1.4",
    "description": "Angular - directives and services for creating forms",
    "licenseFile": "C:\\Users\\UC190\\workspace\\core-search-app\\node_modules\\@angular\\forms\\README.md",
    "licenseText": "Angular\n=======\n\nThe sources for this package are in the main [Angular](https://github.com/angular/angular) repo. Please file issues and pull requests against that repo.\n\nUsage information and reference details can be found in [Angular documentation](https://angular.io/docs).\n\nLicense: MIT",
    "path": "C:\\Users\\UC190\\workspace\\core-search-app\\node_modules\\@angular\\forms"
  },
  ...
}

Therefore I created an interface to parse the elements:

export interface JsonData {
  licenses: string;
  repository: string;
  publisher: string;
  name: string;
  version: string;
  description: string;
  licenseFile: string;
  licenseText: string;
  path: string;
}

But I am struggling on how to iterate over the elements and parse them to a list of JsonData.

0

2 Answers 2

2

Interfaces in TypeScript allow you to describe the shape of your data structure (though, that's not their only purpose). In your case, the data structure is a map, keyed on the package name, with a corresponding value which is package/license details.

Consider the following interfaces...

interface Package {
  licenses: string;
  repository: string;
  publisher: string;
  name: string;
  version: string;
  description: string;
  licenseFile: string;
  licenseText: string;
  path: string;
}

interface PackageMap {
  [key:string]: Package
}

You can test that you can obtain keys, and their corresponding values with the following...

const data: PackageMap = JSON.parse(json); // Assume json refers to your data structure
const keys: string[] = Object.keys(data);

keys.forEach(key => console.log(key, data[key]));
Sign up to request clarification or add additional context in comments.

Comments

0

Okay I found out how to handle this.

I can iterate over the Object, that contains values by

for (const value of Object.values(data)) {
  this.dataSource.data.push(this.encodeLicenseData(value));
}

And parse the value to what I need in the encodeLicenseData() function.

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.