Defining a JSON file interface in Angular to populate a dropdown box with dependencies

I've embarked on an exciting project to develop a cascading dropdown box filter, and it's proving to be quite challenging.

I'm taking it step by step to ensure clarity. I have obtained a JSON file containing the data required to populate dependent dropdown boxes.

{"data":
{"events":
{"type":"cart_update",
    "properties":
        [{"property":"product_id","type":"string"},
        {"property":"variant_id","type":"string"},
        {"property":"category_level_1","type":"string"},
        {"property":"category_level_2","type":"string"},
        {"property":"category_level_3","type":"string"},
        {"property":"product_list","type":"string"},
        {"property":"action","type":"string"}]},
{"type":"view_item",
    "properties":
        [{"property":"product_id","type":"string"},
        {"property":"variant_id","type":"string"},
        {"property":"category_level_1","type":"string"},
        {"property":"category_level_2","type":"string"},
        {"property":"category_level_3","type":"string"}]}]},"success":true}

To aid in navigating my JSON file effectively, I've created interfaces. Let me know if this is correct:

export interface DataInterface {
  events: Array<EventInterface>;
  success: boolean;
}
export interface EventInterface {
  type: string;
  properties: Array<PropsInterface>;
}

export interface PropsInterface{
  property: string;
  type:string;
}

@Injectable({
  providedIn: 'root'
})

export class DataService {

  constructor(private http: HttpClient) { }
     
  getData() {
    return this.http.get('./assets/data.json')   
  }
  }

The next challenge I face is how to populate dropdown boxes with the various event[type] and properties[property], as well as creating dependencies between them. Any suggestions?

Answer №1

take a look at the edits made below

**export interface ResponseInterface {
  information: DataInterface;
}**
export interface DataInterface {
  eventsList: EventInterface[];
  successStatus: boolean;
}
export interface EventInterface {
  categoryType: string;
  details: PropsInterface[];
}

export interface PropsInterface{
  attribute: string;
  dataType:string;
}

@Injectable({
  providedIn: 'root'
})

export class DataService {
  // extracting relevant data from http request
  attributes: PropsInterface[] = this.getData().pipe(pluck("information", "eventsList", "details"));
  constructor(private http: HttpClient) { }
     
  getData() {
    return this.http.get***<ResponseInterface>***('./assets/data.json')   
  }
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

It seems like the recent upgrade to yarn 2 has caused issues with typescript types, whereas the installation of the same project with yarn 1 was

Recently, I've been attempting to update a typescript monorepo to utilize yarn 2, but I've encountered an issue where typescript is struggling to recognize certain react props. This functionality was working fine in yarn 1.x, leading me to believ ...

Break free/Reenter a function within another function

Is there a way to handle validation errors in multiple task functions using TypeScript or JavaScript, and escape the main function if an error occurs? I am working in a node environment. const validate = () => { // Perform validation checks... // ...

The standard build.gradle settings for Ionic projects on Android

By default, in the platforms/android/build.gradle file, I have the following configuration: allprojects { repositories { google() jcenter() } //This replaces project.properties w.r.t. build settings project.ext { defa ...

How to access the result without using subscribe in Angular?

I am facing unexpected behavior with a method in my component: private fetchExternalStyleSheet(outerHTML: string): string[] { let externalStyleSheetText: string; let match: RegExpExecArray; const matchedHrefs = []; while (match = this.hrefReg.exe ...

Heroku local is designed to support only NodeJS applications, without any Angular framework or database connectivity

I'm currently facing two separate issues. When I run my app locally, it works fine, but it doesn't function properly on Heroku. Running "heroku local" opens the NodeJS app on localhost:5000 and connects to the local database. However, when attemp ...

The combination of Object.keys() and the find function

Having trouble figuring out why I'm getting an error when attempting to use ES6 .find on the following data in order to retrieve the record with id number 3. { {id:10,title:'Dairy & Eggs'} {id:7,title:'Laundry & Household'} {id ...

How can we efficiently determine if a background image is broken in Angular 5 and substitute it with a default image?

When working with Angular 2+, we often use the <img> tag to display images using a syntax like <img [src]="myDp" (error)="myDp='assets/media/user/default_dp.jpg'">, where myDp contains the relative path to the image on the server. Is ...

Creating a function in Typescript to extend a generic builder type with new methods

Looking to address the warnings associated with buildChainableHTML. Check out this TS Playground Link Is there a way to both: a) Address the language server's concerns without resorting to workarounds (such as !,as, ?)? b) Dodge using type HTMLChain ...

Is there a way to troubleshoot the issue pertaining to using routerLink in Angular version 17.2?

Currently working on a small app, but encountering an error with routerLink in the console. I am new to angular and seeking help. ✘ [ERROR] NG8002: Can't bind to 'routerLink' since it isn't a known property of 'a'. Here is ...

Tips for setting up chrome-app typings in Typescript 2

I am looking to eliminate the typings in our Typescript project. After successfully removing most typings dependencies with Typescript 2, I'm left with just one for chrome-app: https://github.com/uProxy/uproxy/compare/master...fortuna:master When usi ...

Modifying the dimensions of mat-card in Angular Material

https://i.stack.imgur.com/CP16N.png I am struggling to make both components the same height, similar to the left form. I have tried adjusting margins and padding but nothing seems to work. Below is the HTML code for the parent element: <mat-tab label= ...

Ionic - Deleting an item from local storage

Currently, I am implementing local storage for my Ionic application. While I can successfully add and retrieve data from local storage, I encounter an issue when trying to delete a specific object - it ends up removing everything in the database. Moreover, ...

Evaluating Angular2 components that have indirect dependencies

Starting with Angular2, I make sure to test each component I create from scratch. When it comes to writing tests for components, I must initialize TestBed to ensure that the component under scrutiny has all its necessary dependencies resolved. Currently, ...

How can the file system module (fs) be utilized in Angular 7?

Hello, I am currently working with Angular 7. I recently attempted to utilize the fs module in Typescript to open a directory. However, I encountered an error message stating: "Module not found: Error: Can't resolve fs". Despite having types@node and ...

Searching function in material-table does not work properly with pipe symbol

Within my data table, I utilize the @pipe to display name instead of position in the position row... The name is sourced from a separate JSON file... <ng-container matColumnDef="position"> <mat-header-cell *matHeaderCellDef> No. </ma ...

Tips on implementing conditional validators in Angular's formBuilder

Is it possible in Angular 7.x to use formBuilder and ReactiveForms to apply a validator to a form based on the user's role? For instance, if the user has a specific role, they would be required to input certain fields. The user information is stored i ...

Guide to setting up an interface-only project (along with its dependent projects) on NPM

I'm encountering two problems with my TypeScript project. Imagine I have a interface-only TypeScript project called myproject-api. My plan is to implement the interfaces in two separate projects named myproject-impl1 and myroject-impl2. I am utilizin ...

Angular Form Required not functioning as expected

I have encountered an issue with my form where the required attribute does not seem to work properly. Even when I leave the input field empty, the form still gets submitted. Here is a snippet of my code: <div class="form-group"> <div class="c ...

Dealing with 'TypeError X is Not a Function' Error in Angular (TypeScript): Occurrences in Certain Scenarios and Absence in Others

Recently, I came across an odd issue in Angular 14 where a type error kept popping up. Although I managed to refactor the code and find a workaround, I'm quite intrigued as to why this issue is happening so that I can prevent it from occurring again i ...

The integration of cypress-cucumber-preprocessor with multiple testing frameworks appears to be experiencing compatibility issues

I am trying to set up a connection between Cypress and Cucumber, and I came across this plugin: https://www.npmjs.com/package/cypress-cucumber-preprocessor However, I am having trouble with my implementation as it seems to be missing. I have also added th ...