What is the best way to dynamically add data to a JSON file?

image of JSON file

Just a heads up: I'm looking to add data directly without the need to write it to a .json file, perhaps by using Angularfire2 database.

 user = {
 name: 'Arthur',
 age: 21
};

const options = {Headers, responseType: 'json' as 'blob'};
    this.httpService.put('assets/data/ex.json', this.user, options).subscribe(
      data => {
         console.log(data);
      },
      (err: HttpErrorResponse) => {
        console.log (err.message);
      }
    );

Answer №1

One way to access the object directly is by using the dot operator

person = {
 name: 'Emily',
 age: 25
};

const options = { Headers, responseType: 'json' as 'blob'};
    this.httpService.put('assets/data/info.json', this.person, options).subscribe(
      data => {
         console.log(data);

data.user.lastName = 'programming';

      },
      (err: HttpErrorResponse) => {
        console.log(err.message);
      }
    );

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

How to retrieve the HTTPClient value in Angular?

APIservice.ts public fetchData(owner: any) { return this.http.get(`${this.url}/${owner}`, this.httpOptions).pipe( catchError(e => { throw new Error(e); }) ); } public fetchDataById(id: number, byId:string, owner: any) { ...

Troubleshooting issues with parsing JSON responses in AngularJS

Being new to using AngularJS, I am encountering an issue with parsing a JSON response. I have user login credentials such as Username and password, and I am attempting to parse them when the user clicks on the login button(). If the username and password m ...

"Can you provide guidance on binding data in vue.js when there is a dash (-) in the key of the JSON

My JSON data is structured as follows: { "color-1": "#8888", "color-2": "#000" } I am attempting to bind this variable with a style tag for a Vue component. However, my current approach seems to not be functioning as expected. <div v-bind:sty ...

Having trouble reading the JSON file content?

www.youtubeinmp3.com/fetch/?format=JSON&video=http://www.youtube.com/watch?v=i62Zjga8JOM here is an example of the output you can expect: { "title":"Happy Forever Alone Day (Forever Alone Song)", "length":"125", "link":"http:\/\ ...

Is there a way for me to modify a value in Mongoose?

I have been attempting to locate clients by their ID and update their name, but so far I haven't been successful. Despite trying numerous solutions from various sources online. Specifically, when using the findOneAndUpdate() function, I am able to id ...

How can I extract a value from an object that is readonly, using a formatted string as the key?

I encountered a situation where I have code resembling the following snippet. It involves an object called errorMessages and multiple fields. Each field corresponds to various error messages in the errorMessages object, but using a formatted string to retr ...

When using JSON stringify, double quotes are automatically added around any float type data

When passing a float data from my controller to a JavaScript function using JSON, I encountered an issue with quotes appearing around the figure in the output. Here is the JS function: function fetchbal(){ $.ajax({ url: "/count/ew", dataType: "jso ...

Struggling to access a remote URL through jQuery's getJSON function with jsonp, but encountering difficulties

Currently, I am attempting to utilize the NPPES API. All I need to do is send it a link like this and retrieve the results using jQuery. After my research, it seems that because it is cross-domain, I should use jsonp. However, I am facing difficulties m ...

Leveraging AWS CDK to seamlessly integrate an established data pipeline into your infrastructure

I currently have a data pipeline set up manually, but now I want to transition to using CDK code for management. How can I achieve this using the AWS CDK TypeScript library to locate and manage this data pipeline? For example, with AWS SNS, we can utilize ...

C# Web API that provides a JSON result containing nested arrays

Hello, I am looking for assistance with the following scenario. I have a requirement to return a JSON response from a DotNet Web API in the format shown below. { "Status":"OK", "Series":[["GREEN",40.5],["RED",12.8],["YELLOW",12.8]] } Below are examples o ...

What is the best way to handle alias components in next.js when using ts-jest?

When working with TypeScript, Next.js, and Jest, I wanted to simplify my imports by using aliases in my tsconfig file instead of long relative paths like "../../..". It worked fine until I introduced Jest, which caused configuration issues. This is a snip ...

How can one pass a generic tuple as an argument and then return a generic that holds the specific types within the tuple?

With typescript 4 now released, I was hoping things would be easier but I still haven't figured out how to achieve this. My goal is to create a function that accepts a tuple containing a specific Generic and returns a Generic containing the values. i ...

Can a reducer be molded in ngrx without utilizing the createReducer function?

While analyzing an existing codebase, I came across a reducer function called reviewReducer that was created without using the syntax of the createReducer function. The reviewReducer function in the code snippet below behaves like a typical reducer - it t ...

Utilize Javascript to extract and showcase JSON data fetched from a RESTful API

I'm attempting to use JavaScript to pull JSON data from a REST API and display it on a webpage. The REST call is functioning correctly in the Firefox console. function gethosts() { var xhttp = new XMLHttpRequest(); xhttp.open("GET", "https://10 ...

Guide on retrieving simplejson in Python

As a beginner, I am currently delving into a Python project that involves working with json data. My aim is to retrieve information by calling an object ("object.json()"). The catch is that I am restricted to importing simplejson and not allowed to impor ...

Fetching data in VueJs before redirecting to a new page

Within the mounted function, I am creating an action that fetches data from a Rest API and populates my table in a Vue.js component mounted() { UserService.getProjects().then( (response) => { this.isProject = true; this.project ...

Python dictionary does not display non-English characters correctly

Hey there, so I've read through However, I'm still struggling to grasp why this code is behaving the way it is: # -*- coding: utf-8 -*- import json a='ööö' b='ääß' print a+' '+b >>>ööö ääß pr ...

Ways to implement debounce in handling onChange events for input fields in React

I've been attempting to implement debounce functionality in my React app without relying on external libraries like lodash or third-party node modules. I've tried various solutions found online, but none have worked for me. Essentially, in the h ...

Issue encountered in Vite Preview: Uncaught (in promise) SyntaxError: JSON.parse found an unexpected character at the beginning of the JSON data, line 1 column 1

Encountering the error message Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data when running vite preview after running vite build. https://i.sstatic.net/61Y9t.png Here is my vite.config.js: import { ...

What are some strategies to expand JSON code length within a grid in SQL Server?

When working in SQL Server, I am faced with an issue regarding a select * query that returns 2 columns - one containing numbers and the other JSON code generated by a subquery using FOR JSON PATH. The problem arises when the subquery generates a large nu ...