Extracting decimal numbers from JSON data

I am currently facing an issue in my Angular project where I am attempting to read a JSON file. This file is stored in the assets folder and contains decimal values such as:

{
  "valueA": 0.40000000000002,
  "valueB": 23.99999999999999995
}

The problem arises when I import the file and the values are rounded to:

{
  "ValueA": 0.4
  "ValueB": 25
}

Is there a way to retrieve the JSON with the exact decimal digits from the source? Alternatively, is it possible to convert them into strings? Unfortunately, modifying the source to split the numbers at the dot or save them as strings is not an option for me. Although I could potentially edit it during the data seeding pipeline process, this approach seems messy.

Currently, my JSON file is imported and utilized in the following manner:

import MyJson from 'src/assets/MyJson.json'

export class MyService {
  private myJson = Object.assign(MyJson);

  public getFieldsIWant() {
    return this.myJson.theFields.iWant;
  }
}

It seems that the issue lies within the first line of import {.... When I print the imported file, the decimal places have already been "converted". Is there an alternative method to import JSON files in TypeScript to avoid this issue (I have already attempted importing via httpClient with the same outcome)?

Answer №1

If you need a good alternative to JSON.parse, consider using libraries such as https://github.com/josdejong/lossless-json

const fs = require('fs');
const LosslessJSON = require('lossless-json');

const fileContents = fs.readFileSync('./data.json', 'utf8');

let json = LosslessJSON.parse(fileContents);

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

Tips on implementing JSON data into select2 plugin

I have been trying to integrate the select2 plugin into my project. I followed a tutorial from this link, but unfortunately, it's not functioning properly for me. Here is the JSON output: [ {"ime":"BioPlex TM"}, {"ime":"Aegis sym agrilla"}, ...

A step-by-step guide for updating a minor version of Angular with Angular CLI

I've been searching online for the answer to this straightforward question, but can't seem to find it anywhere... In my angular 4 project (made with angular cli), I want to utilize the newly introduced http interceptors in version 4.3. Could so ...

"Creating a Typescript array by extracting items from a different const array (as seen in the official beginner tutorial

Currently, I am working through the Angular tutorial that can be found at https://angular.io/start. After successfully completing the tutorial, I decided to practice building for production locally. However, when attempting to build, I encountered this err ...

Employing [style.something.px]="2" in Angular to specify the thickness of the border

Presently, I am setting the width of the element using this code format: <div [style.width.px]="size" [style.height.px]="size"></div> What I am aiming for is to utilize a comparable format but to define the border-width css attribute, such as ...

Add an image to a directory with Angular 7

I am having trouble uploading an Image to the assets/ folder using Angular 7. Below is my attempted solution: HTML: <form [formGroup]="form" (ngSubmit)="postData()" class="intro-form-css"> <div class="form-row"> ...

Implementing an automatic logout feature based on the expiration timestamp of a JWT token

My goal is to have the application automatically log out based on an expiry token. Angular Client Code: login(credentials) { return this.http.post('http://something/api/login/', credentials) .map(response => { let res ...

Utilizing Dynamically Bound Properties for Angular Directive Inputs

Hey there, I'm currently working on a drag directive that is causing me some trouble when trying to apply it to dynamic objects. The issue lies in how it references the id within the input. @Input() set Drag(options: any) { this.stickerElement = do ...

Updating an object property within an array in Angular Typescript does not reflect changes in the view

I am currently delving into Typescript and Angular, and I have encountered an issue where my view does not update when I try to modify a value in an array that is assigned to an object I defined. I have a feeling that it might be related to the context b ...

Tips for resolving the error message "Uncaught (in promise) bad-precaching-response" in service workers when using Angular

Encountering issues with making my Spring + Angular app a progressive web app (PWA) in a jhipster-6.2-generated project. The service-worker registers successfully but offline functionality is missing in the production deployment. Getting a bad-precaching-r ...

Encountering challenges with periods in URL when utilizing a spring boot application alongside Angular in a live environment

Currently, I am in the process of developing a Spring boot + Angular application using JHipster and deploying it in a docker container with JIB. However, encountering an issue where URLs containing a dot are not functioning properly when accessed directly ...

Validation scheme for the <speak> element

When using validators in an angular formarray for input fields, I encountered a challenge with the regex to check the <speak> tag. https://i.sstatic.net/ogFA3.png The content provided was considered valid. https://i.sstatic.net/ar5FJ.png An error ...

Troublesome problem encountered when sending a JSON object to a C# method, specifically when the method requires a collection as a parameter and is being handled

Encountering a peculiar issue when passing the given data: queueNotificationData = { StartDate: that.batchData.StartDate.valueOf(), StartTime: that.batchData.StartTime.valueOf(), EndDate: that.batchData.EndDate.valueOf( ...

What are the limitations of using a JS file within an Angular application?

I am struggling to integrate some js methods from a file into an angular application. Unfortunately, the browser is unable to recognize the js file. I have tried following the guidelines in this SO post, but the browser still cannot locate the file in the ...

Is there a way to transform a local array into remote JSON data?

I am attempting to retrieve an array from a remote server that is connected to a dynamic database. From what I have gathered on Ionic forums, it seems that I need to utilize the $http function from AngularJS. However, since I am new to AngularJS, the curr ...

"Unindexing data in Angular: A step-by-step guide

Can someone help me figure out how to delete an item by index in Angular? I have a parameter and a remove button, but when I tried putting my parameter inside the remove button it didn't work. How can I fix this? deleteRowFiles(rowIndex: number){ th ...

The configuration file for Typescript and Typeorm, specifically the .ts file, is encountering an error

Hello, I'm encountering an issue with my app.ts. When trying to load my settings from ormconfig.ts for the typeorm function that creates the connection, I receive the following error: No overload matches this call. Overload 1 of 3, '(name: stri ...

Utilizing nested endpoints in Angular resource with a Node.js server

In my Angular application, I have a resource called /cars with the endpoint defined as $resource('/cars/:carsId'); This allows me to retrieve all cars or a specific car. On the server side, I've implemented middleware to ensure that carsI ...

"Upon subscribing, the array within the object is found to be

I am encountering a synchronization issue while trying to retrieve an element after making multiple HTTP requests. Below is the code I have posted: Get function in my service: get() { return new Observable(project => { this.channelsService.get().sub ...

Transform JSON-serialized string with HTML entities into an object

Looking for a solution to convert the following string into an object using Javascript: "[&quot;Software&quot;,&quot;3rd Party&quot;]" While I know how to convert HTML Entities to DOM Objects with this code: $("<div/>").html(encode ...

Splitting Angular Components with Angular as-split

Having trouble adding a "title" to my angular as-split-area. The titles are appearing together rather than above the as-split-area. <div style="width: 800px; height: 400px; background: yellow;"> <as-split direction="vertical&q ...