Saving a collection of items to an external JSON file

Looking to save an array of objects from your javascript code into an external JSON file? Specifically, you want to store it in Webstorm's package.json. Most resources focus on retrieving data from an external JSON file, but how can you accomplish the opposite?

Take a look at this example javascript array:

var questions = [{
        question: "Question1",
        choices: ["Choice1", "Choice2", "Choice3", "Choice4"],
        corAnswer: 0
    }, {
        question: "Questions2",
        choices: ["Choice1", "Choice2", "Choice3", "Choice4"],
        corAnswer: 1
    }, {
        question: "Question3",
        choices: ["Choice1", "Choice2", "Choice3", "Choice4"],
        corAnswer: 3
    }];

You may be thinking of using JSON.stringify, but then how do you go about storing it in package.json?

Answer №1

It's not possible to directly write to files using JavaScript, as mentioned by others. However, if your goal is simply to store JSON objects in a separate file without modifying them, you can place them in a .js file and reference that file within script tags before your main JS file. By placing it in the global scope, the questions object will be accessible to functions in other files.

Answer №2

After applying JSON.stringify to the result, I utilized console.log, followed by copying and pasting the data into an external JSON file.

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

Enhance Your HTML Skills: Amplifying Table Display with Images

Recently, I utilized HTML to design a table. The Table Facts : In the first row, I included an appealing image. The second row contains several pieces of content. In continuation, I added a third row. The contents in this row are extensive, resulting i ...

Navigating JSON data with unexpected fields in Typescript and React.js

Looking to parse a JSON string with random fields in Typescript, without prior knowledge of the field types. I want to convert the JSON string into an object with default field types, such as strings. The current parsing method is: let values = JSON.parse ...

Sort ActiveRecord objects based on JSON type data structure

Recently delving into the world of JSON datatypes and curious if Rails has any clever shortcuts for sorting. Let's say I have the following: object_a.payload = { "category_1" => 2, "category_2" => 1 } object_b.payload = { "category_1" => 1 } ...

Incorporate a loader when making an AJAX request and then conceal it after it has successfully completed

I find myself a bit puzzled here. Currently, I'm developing a JavaScript form that sends values to a PHP page (submit.php). If the PHP page returns a success message, I plan to redirect the user to another page (success.php). var url = 'submit. ...

Discover real-time results of accordions and their information as you search

I am looking to add a search functionality to filter through a list of accordions and their contents on my HTML page. I want the search box to be able to search both the accordion titles and the content within each accordion. This is my first time posting ...

Steps to enable navigation to external pages from a ReactJS app

I am working on a simple ReactJS application: [Demo] [Source] I am trying to implement navigation within the app from external sources without refreshing the web page. This functionality should be similar to using this.props.history.push(...). /public/i ...

Utilize JSON to Extract Information and Present it in an Expandable List Interface

Can you explain the distinction between activity and fragment? I have been tasked with parsing JSON data and displaying it in an expandable list view where NAME and Class will be the list headers and other information will be displayed as children. Can th ...

Utilizing a particular iteration of npm shrinkwrap for project dependencies

When deploying my node.js app to Appfog, I encountered a problem with their install script failing to parse npm-shrinkwrap.json. The current format of a dependency in shrinkwrap.json is as follows: "async": { "version": "0.2.10", "from": " ...

Setting up isotope using dynamic JSON data

I've done some searching, but without any luck. My current dilemma involves utilizing the isotope jQuery library to display data in a dynamic manner using a JSON dataset. I've stored the data in a .json file and am reading it in, parsing the info ...

Printing using *ngFor will display items in an ascending order

When attempting to display an object in markup, I am running into the issue of *ng printing it in ascending order instead of maintaining the original order. Ideally, I would like the elements to be printed as they are. You can view my code on StackBlitz ...

What is the process for submitting a record to a table following the activation of a JavaScript link or button

I am working on creating a like-unlike button below each post for registered users to interact with. I have successfully created the button itself, but now I need to figure out how to store records when a user clicks the button. My idea is to utilize a tab ...

Disabling the capability to duplicate content in the iPad Safari browser

While working with Safari on the iPad running iOS6, I am facing an issue where a "Copy" option automatically pops up when I try to select and highlight text using JavaScript. This behavior is not seen in other browsers except for Safari on the iPad. Is t ...

Dynamic rule addition with JQuery Validation

I am searching for a method to dynamically incorporate the jQuery validation rules, as they are needed in various locations. For instance, a user can edit news in two different ways. The first method involves using a standard form with jQuery validation, ...

Remove the 5th element from the given array, starting with the element at index 2

My array consists of 3 groups, with each group containing 5 elements. var tempArray1 = ['prodn', 'PP1', 'UK1', 'Exp', 'India2', 'prodn', 'PP2', 'france1', 'Imp', &ap ...

Transforming JSON format to another JSON format using AngularJS

I am in the process of setting up a questionnaire using angularjs. I have an array of responses that looks like the following, and I need to convert this object array into JSON format. How do I go about converting an object array into JSON format? ...

Can you explain the significance of "mongodb is not a function"?

As a novice in the world of programming, I am currently diving into a web development course. However, I encountered an error while trying to connect to a database I created. The error message states that "mongodb.connect is not a function." Below is the ...

The primefaces codeMirror is failing to properly load its own CSS and JavaScript files

I am experiencing an issue with codeMirror from primeface-extension when trying to use it with SQL syntax. Upon loading the page that contains this component, I encounter a 404 error where the Css and javascript components cannot be found. Despite having ...

Sum the MongoDB aggregation until a certain condition is met (using if/else) or solve the problem by counting the price and quantity

THE ISSUE I am currently developing a function that calculates the average price and total value based on the requested quantity. For instance: async calculateValues (first_100) { let market_data = await auctions_db.aggregate([ { ...

Running code step by step in VSCode debugging

After finding answers and solutions partially related to this specific question, I have now established the following launch configurations for debugging my react-redux + electron application. { "version": "0.2.0", "configurations": [ { "typ ...

Darken the entire webpage and gradually fade in a specific div element using Jquery

Currently, I am working on implementing the following functionality: - When a link is clicked, it should trigger a function to show a DIV (#page-cover) that will dim down the entire background. This div has a z-index of 999. - Following the dimming effect, ...