0

I want to create model file for Nested JSON in angular 8 . As I'm new to Angular Development had no idea how to do it .

my Api response looks like :

{
   "data": [{
      "nationalCustomerId": 31,
      "nationalCustomerName": "Family Dollar",
      "stores": [{
            "categories": [{
               "category": "Dairy",
               "categoryId": 1
            }],
            "storeId": 18627,
            "storeNumber": 3367
         },
         {
            "categories": [{
               "category": "Dairy",
               "categoryId": 1
            }],
            "storeId": 25540,
            "storeNumber": 10164
         },
         {
            "categories": [{
               "category": "Dairy",
               "categoryId": 1
            }],
            "storeId": 25735,
            "storeNumber": 10783
         },
         {
            "categories": [{
               "category": "Dairy",
               "categoryId": 1
            }],
            "storeId": 26971,
            "storeNumber": 11374
         }
      ]
   }],
   "status": "success"
}

Any help is appreciated.. I want to create a model file for above API response in angular 8.

0

2 Answers 2

1

The model classes could look like this:

export class Response {
    data: Customer[];
    status: string;
}

export class Customer {
    nationalCustomerId: number;
    nationalCustomerName: string;
    stores: Store[];
}

export class Store {
    storeId: number;
    storeNumber: number;
    categories: Category[];
}

export class Category {
    category: string;
    categoryId: number;
}

To keep them organised, have them as separate files and import as needed to reference them, e.g:

In a file called store.ts (that has the Store class definition):

//if your Category model class is in same folder and called category.ts
import { Category } from './category';

Then you can import the outer 'data' class to the file where you need to map the response with e.g:

import { Response } from '<some path to your model classes folder>/response';
Sign up to request clarification or add additional context in comments.

1 Comment

Super !!!! I did the same and Tq so much for the response. cheers mate..
0

you can create a typescript file(let say response.ts) and keep you data there like

export const response = {
  "data": [
    {
      "nationalCustomerId": 31,
      "nationalCustomerName": "Family Dollar",
      "stores": [
        {
          "categories": [
            {
              "category": "Dairy",
              "categoryId": 1
            }
          ],
          "storeId": 18627,
          "storeNumber": 3367
        },

        {
          "categories": [
            {
              "category": "Dairy",
              "categoryId": 1
            }
          ],
          "storeId": 25540,
          "storeNumber": 10164
        },


        {
          "categories": [
            {
              "category": "Dairy",
              "categoryId": 1
            }
          ],
          "storeId": 25735,
          "storeNumber": 10783
        },



        {
          "categories": [
            {
              "category": "Dairy",
              "categoryId": 1
            }
          ],
          "storeId": 26971,
          "storeNumber": 11374
        }
      ]
    }
  ],
  "status": "success"
}

After this you can import this data in any file using the below import statement.

import {response} from 'path of file where it is kept'

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.