Sabrina Reyes
Sabrina Reyes

Reputation: 139

Why is JSON being detected as empty?

So I have a JSON file I got from Postman which is returning as an empty object. This is how I'm reading it.

import regscooter from './json_files/reginald_griffin_scooter.json'

const scoot = regscooter;

const CustomerPage = () => {...}

reginald_griffin_scooter.json

{
    "success": true,
    "result": {
      "id": "hhhhhhhhhh",
      "model": "V1 Scooter",
      "name": "hhhhhhhhhh",
      "status": "active",
      "availabilityStatus": "not-available",
      "availabilityTrackingOn": true,
      "serial": "hhhhhhhhhhhh",
      "createdByUser": "hhhhhhhhK",
      "createdByUsername": "hhhhhhhh",
      "subAssets": [
        "F0lOjWBAnG"
      ],
      "parts": [
        "hhhhhhhh"
      ],
      "assignedCustomers": [
        "hhhhhhhhh"
      ],
      "createdAt": "2019-12-03T21:47:26.218Z",
      "updatedAt": "2020-06-26T22:05:54.526Z",
      "customFieldsAsset": [
        {
          "id": "hhhhhhh",
          "name": "MAC",
          "value": "hhhhhhhh",
          "asset": "hhhhhhhhhh",
          "user": "hhhhhhhhh",
          "createdAt": "2019-12-03T21:47:26.342Z",
          "updatedAt": "2019-12-11T16:29:24.732Z"
        },
        {
          "id": "hhhhhhhh",
          "name": "IMEI",
          "value": "hhhhhhh",
          "asset": "hhhhhhh",
          "user": "hhhhhhhhhh",
          "createdAt": "2019-12-03T21:47:26.342Z",
          "updatedAt": "2019-12-11T16:29:24.834Z"
        },
        {
          "id": "hhhhhhhhh",
          "name": "Key Number",
          "value": "NA",
          "asset": "hhhhhhhhh",
          "user": "hhhhhhhhhhh",
          "createdAt": "2019-12-03T21:47:26.342Z",
          "updatedAt": "2019-12-11T16:29:24.911Z"
        }
      ]
    }
}

The error is that "const scoot" is being shown as an empty object {}. I made sure to save a ton of times everywhere. I am able to read through the imported JSON file in other variables in similar ways, so I don't know why I can't parse this one. I just want to access the JSON object inside this. Also I omitted some information with hhhhh because of confidentiality.

EDIT: The code works, but it still has a red line beneath result when I do:

const scoot = regscooter.result.id;

Upvotes: 1

Views: 201

Answers (1)

Yaroslav
Yaroslav

Reputation: 110

It would be much more effective if you will provide an example in codesandbox or so.
However at first look it might be a parser issue ( maybe you are using Webpack with missing configuration for parsing files with json extension ), meaning we need more information to provide you with a full answer ( maybe solution ? ).

Have you tried to do the next:

const scoot = require('./json_files/reginald_griffin_scooter.json');

Upvotes: 1

Related Questions