What is the process for eliminating the perplexing "default" attribute that has been inserted into JSON?

I recently came across the jsondata.json file:

{
    "name": "John",
    "age": 30,
    "car": "ferrari"
}

Located in the same directory is a typescript file called main.ts where I simply output the json data using console.log

import * as j from './jsondata.json';

console.log(j);

The output includes an unexpected field named default. What is this field, why does it appear, and how can it be removed?


Just for your information, here is my setup in the tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./out",
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "forceConsistentCasingInFileNames": true
  }
}

Answer №1

After experimenting with different configurations, I discovered that the final two settings provide the desired outcome.

jsondata.json:

{
    "name": "John",
    "age": 30,
    "car": "ferrari2"
}

When resolveJsonModule is set to true without esModuleInterop and importing without *, it results in an error.

main.ts:

import j from './jsondata.json';
console.log(j);

tsconfig.ts:

"resolveJsonModule": true,
// "esModuleInterop": true

result:

>

{ name: 'John', age: 30, car: 'ferrari2' }

Module ... can only be default-imported using the 'esModuleInterop' flag

Setting both flags to true and using * import displays the default value

main.ts:

import * as j from './jsondata.json';
console.log(j);

tsconfig.ts

"resolveJsonModule": true,
"esModuleInterop": true

result:

>

{
  name: 'John',
  age: 30,
  car: 'ferrari2',
  default: { name: 'John', age: 30, car: 'ferrari2' }
}

When both flags are set to true without using *, the default value is not shown

main.ts:

import j from './jsondata.json';
console.log(j);

tsconfig.ts:

"resolveJsonModule": true,
"esModuleInterop": true

result:

>

{ name: 'John', age: 30, car: 'ferrari2' }

Using resolveJsonModule as true and no esModuleInterop with * import does not display a default value

main.ts:

import * as j from './jsondata.json';
console.log(j);

tsconfig.ts:

"resolveJsonModule": true,
// "esModuleInterop": true

result:

>

{ name: 'John', age: 30, car: 'ferrari2' }

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

A Guide to Iterating Through Arrays of Objects Using TypeScript

Currently, I am engrossed in an Angular project where I am fetching an object containing an array of objects from an API. The object being passed to the API as a parameter through my service is called "reportData". Here is an example of the data retrieve ...

HTML Elements for Displaying Undefined JSON Information

Hey there, I'm new to programming and I'm looking to display JSON data in an HTML table using jQuery. The issue I'm facing is that the output from the server shows up as 'undefined'. My goal is to have a constantly updated list of ...

The encoding function in xCode5 does not work properly with JSON

NSString *urlString=[NSString stringWithFormat:@"http://192.168.178.1/iBus/appService/_getMarker.php"]; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSError *error = [[NSError alloc] i ...

DxDataGrid: Implementing a comprehensive validation system for multiple edit fields

I'm currently working with a DxDataGrid within an Angular Application. Within this particular application, I have the need to input four dates. I've implemented validation rules that work well for each individual field. However, my challenge aris ...

Tips for printing a jq JSON format while properly escaping double quotes using backslashes

Is there a way to create a function that can properly echo a JSON with escaped values that contain backslashes, such as "System.Title": "The \"title\" with double quotes"? Whenever jq processes the echoed result, it ...

Change the input field to uppercase using JavaScript but do not use inline JavaScript

Hey there! I have a basic script set up (see below) with an input field allowing users to enter a query. This query is then sent to a socrata webservice to request specific data, which is displayed in an alert box. So far, everything works smoothly. var l ...

Pressing a button that appears multiple times and is also embedded within layers

I am facing an issue where I need to interact with a button that appears multiple times on the website within nested cards. Specifically, I am trying to locate the card containing a pet named Bala, as shown in the attachment below, and click on the Detail ...

What is the best way to showcase the outcomes of arithmetic calculations on my calculator?

In the midst of creating a calculator, I have encountered some issues in getting it to display the correct result. Despite successfully storing the numbers clicked into separate variables, I am struggling with showing the accurate calculation outcome. l ...

The ASP.net WebMethod seems to be providing a full page output instead of the expected JSON data

I am implementing an auto complete functionality for a textbox. However, when I send a GET request to a web method, instead of receiving the actual data, I get the entire page content. Here is the C# code snippet that I am using: [WebMethod] [Scr ...

What is the best way to integrate ag-grid with Observable in Angular 2?

After conducting extensive research on the Internet, I am still struggling to connect the pieces. My angular2 application utilizes an Observable data source from HTTP and I am attempting to integrate ag-grid. However, all I see is a loading screen instead ...

Converting JSON object to an array or list using deserialization

I'm not sure what to ask, so I apologize if this is poorly thought out. The other questions I have found are about people receiving object arrays in JSON. Instead of an array, my JSON string is returning as an object. This is new for me because I hav ...

Creating a Typescript interface where one property is dependent on another property

Let's look at an illustration: type Colors = { light: 'EC3333' | 'E91515' dark: '#100F0F' | '140F0F' } interface Palette { colorType: keyof Colors color: Colors[keyof Colors] } Is it possible for the ...

HTML code is not displayed within the ng-template

After passing JSON data in the correct format from my Laravel controller, I'm trying to display it recursively using Angular. However, the HTML inside ng-template is not being rendered. What could be the issue? Note: I've replaced the default An ...

Transforming AngularJS 2.0 code into ES6 syntax

Successfully implemented the AngularJS 2.0 5 Minute Quickstart in my IntelliJ IDEA 14.1.4 following a helpful Stackoverflow Answer on AngularJS 2.0 TypeScript Intellij idea (or webstorm) - ES6 import syntax. However, it seems to focus on compiling TypeScr ...

React's memo and/or useCallback functions are not functioning as anticipated

Within my Home Component, there is a state called records, which I utilize to execute a records.map() and display individual RecordItem components within a table. function Home() { const [records, setRecords] = useState<Array<RecordType>>(l ...

When using @testing-library/react (rtl), the 'waitFor' function achieves success even without the need for the 'await' keyword

waitFor() is causing my test to fail while waitFor() (without await) makes it pass. The official documentation states: Async methods return a Promise, so you must always use await or .then(done) when calling them. (https://testing-library.com/docs/guide ...

Converting Wordpress JSON data into a UITableView for iOS development

Having trouble parsing a JSON from this specific URL, which is an output in JSON format from Wordpress. Struggling to display the data in a UITableView. The code I currently have for parsing the JSON seems to be missing something, particularly when it com ...

Understanding the use of JSON and JavaScript is proving to be quite a challenge for me

Although I understand the "parse" and "stringify" methods for JSON, I am still struggling to use it effectively despite seeing many examples and questions. In a previous homework assignment, I created an image gallery with hard-coded links to images. Now, ...

Ways to initiate a fresh API request while utilizing httpClient and shareReplay

I have implemented a configuration to share the replay of my httpClient request among multiple components. Here is the setup: apicaller.service.ts import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http& ...

Ways to differentiate between the sources of two cold Observables (not just the possible data streams they may produce)

Situation: Within my Angular application, I am using publishReplay to store and replay specific Http requests. However, I encountered an issue where I need the cached observable source to update itself and create a new cached observable with publishReplay ...