Craig Liston
Craig Liston

Reputation: 93

Having trouble getting json data from file

I'm reading in a json file but I can't seem to be able to find a way to iterate or loop over the data and get the sub items.

I have tried to for loops, .keys(), foreach, and other examples I've found on how to loop over the data.

Here is an example of the json. I modified some of the data for privacy

{
    "day_ordered_data": {
        "1": [
            {
                "study": "1234567890",
                "barcode_uploaded": "1234567890",
                "barcode_scanned": "1234567890",
                "receipt_date": "1234567890",
                "day_num": 1,
                "sample_type": "Blood"
            },
            {
                "study": "0987654321",
                "barcode_uploaded": "0987654321",
                "barcode_scanned": "0987654321",
                "receipt_date": "0987654321",
                "day_num": 1,
                "sample_type": "Blood"
            },
            {
                "study": "6789012345",
                "barcode_uploaded": "6789012345",
                "barcode_scanned": "6789012345",
                "receipt_date": "6789012345",
                "day_num": 1,
                "sample_type": "WB for DNA"
            }
        ]
    }
}

I've manage to figure out how to get "day" data by creating an array of expected days to loop over. Everything I tried to do looping over the days otherwise failed. I got the error the data was not an array or a string.

The console.log prints out the correct data now but I can not figure out how to get the sub fields it always tells me data[day].day_num is undefined.

import { Injectable } from '@angular/core';
import accessioningAnalyticsDataNC from '../../assets/data/Accessioning-Analytics-Data.json';

@Injectable({
    providedIn: 'root'
})

export class AnalyticsAccessioningDataService {
    private dataMap = new Map();
    days30: string[];

    constructor() {
        this.days30 = ['29', '28', '27', '26', '25', '24', '23', '22',
            '21', '20', '19', '18', '17', '16', '15', '14', '13', '12', '11',
            '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0'];
    }

    public getData() {
        console.log('AnalyticsAccessioningDataService: getData');

        const data = accessioningAnalyticsDataNC.day_ordered_data;

        for (const day of this.days30) {
            console.log(data[day]);
        }
    }

    public getDataByType(type: string) {
        let data: any;

        if (this.dataMap.has(type)) {
            data = this.dataMap.get(type);
        }

        return data;
    }
}

I need to find a way to be able to loop over the data so I don't need to know the days, which sample_type, and which study is present.

Update to first answer:

const data = accessioningAnalyticsDataNC.day_ordered_data;

for(let i of data){
}

This returns:

ERROR in src/app/services/analytics-accessioning-data.service.ts(25,22): error TS2495: Type '{ "1": { "study": string; "barcode_uploaded": string; "barcode_scanned": string; "receipt_date": string;
"day_num": number; "sample_type": string; }[]; ...' is not an array type or a string type.```

Upvotes: 0

Views: 38

Answers (2)

Craig Liston
Craig Liston

Reputation: 93

Thanks to @Pipimi for pointing me to the right direction. Changed

const data = accessioningAnalyticsDataNC.day_ordered_data;

to

const data = accessioningAnalyticsDataNC['day_ordered_data'];

Add inside the for loop

if (data[day] !== undefined) {
    console.log(data[day]);
    for (const i of data[day]) {
        this.dayItem = i;
        console.log(day + ' ' + this.dayItem.barcode_scanned + ' ' + this.dayItem.day_num + ' ' + this.dayItem.sample_type);
    }
}

Working code.

const data = accessioningAnalyticsDataNC['day_ordered_data'];
console.log(data);

for (const day of this.days30) {
    console.log(day);
    if (data[day] !== undefined) {
        console.log(data[day]);
        for (const i of data[day]) {
            this.dayItem = i;
            console.log(day + ' ' + this.dayItem.barcode_scanned + ' ' + this.dayItem.day_num + ' ' + this.dayItem.sample_type);
        }
    }
}

Upvotes: 1

Pipimi
Pipimi

Reputation: 143

As far as I am concerned what it could be happening is that you are storing arrays of days and those are what you are referencing.

This is what you are trying to get: day_ordered_data > day > day_num

But this is the structure of your JSON: day_ordered_data > array of days "1" > day > day_num

You could try something like this: console.log(data[1][day]).

I hope I could be of any help.

Upvotes: 1

Related Questions