Ways to verify if information is being added to a JSON file or not

JSON

Here is the JSON structure where I am adding data with a button click event.

var myData = {
    "info" : []
};

JavaScript Function

I have written this function to add data to the JSON object:

myData.info.push({ 
            "Name" :name,
            "Number" : num
        });

Function to Display JSON Data

function displayJson(){ 
    var obj = eval ("(" + myData + ")");
    document.getElementById("newme").innerHTML=obj.info.Name;

}

This code doesn't seem to work as expected, any suggestions would be appreciated.

Thank you for taking time to assist me

Answer №1

Give this a shot

function showJson(){ 

    document.getElementById("newme").innerHTML= arrtopic.info[0].Name;

}

Answer №2

You are on the right track. Be sure to utilize the following code:

 console.log(arrtopic.info[0].Name);

rather than using arrtopic.info.Name.

Answer №3

function showJsonData(){ 
    var data = JSON.parse(topicArray);
    document.getElementById("myData").innerHTML = data.details.Title;
}

You could also try using alert(data.numItems); to check if the items are being added successfully.

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

The method of inserting a JSON dates object to deactivate specific days

I am currently utilizing a date picker component that can be found at the following link: While attempting to use the disabledDays section below, I have encountered an issue where I am unable to apply all three options. The blockedDatesData option works o ...

Divide the array of words into segments based on the maximum character limit

I am looking for a way to divide an array of words into chunks based on a maximum number of characters: const maxChar = 50 const arrOfWords =['Emma','Woodhouse,','handsome,','clever,','and','rich,&apo ...

Changing nested JSON into a flat dataframe in R

Here is the structure of my JSON: I am looking to convert it into a flat dataframe. res <- jsonlite::fromJSON(data, simplifyDataFrame=TRUE) or res <- jsonlite::fromJSON(data, flatten=TRUE) However, these methods give strange results. I have t ...

Run a function once the ajax request has been completed

Despite seeing similar queries on SO multiple times (such as here), I couldn't find a solution that worked for me. I am using ajax to import JSON data for a slick slider. The slick slider needs to be initialized after the completion of the ajax impor ...

Is there a way to create multiple POJOs from a JSON string?

Here is the JSON data I'm working with: {"event_type": "[new,update,delete,close]","event_payload": [{"comment_id": 123,"comment_text": "","comment_type": "DIDWELL"}],"event_retrospective_id": 500,"event_error": ""} The Pojo class that was generate ...

Exploring the Integration of Angular 5 with Firestore for Working with Nested

I have a firestorm collection that follows this specific structure: USERID { email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="354150464175415046411b565a58">[email protected]</a>" name: "John Doe" ...

Refreshing the Dropdown Menu with Jquery

Looking for a way to create a reusable dropdown menu using css/jquery? Check out this codepen: https://codepen.io/anon/pen/NxXXPg Is there a method to reset the 'active' status when clicking on a blank space or another html element? Here's ...

Switching from PHP to JavaScript before returning to PHP to establish and manage sessions

Currently, I am in the process of resolving an issue I am facing. The problem arises when utilizing the following code for someone trying to sign into the admin panel: <script> function myFunction() { //alert('you can type now, end ...

Sharing information among v-for divisions - Vue.js

I am currently delving into the world of VueJS. While my code may not be the most elegant, it does the job almost as intended. The issue I am facing is that the API provides the title and href separately in different v-for loops, even though each loop only ...

Is Jquery Steps causing interference with the datepicker functionality?

I am currently using the jquery steps plugin for creating a wizard on my website. However, I am experiencing some issues with the functionality of the datepicker and qtip inside the steps. Even after switching the .js references, the problem still persists ...

Having trouble fetching JSON data using AJAX

Having trouble retrieving Json text from servlet response. The servlet code is functioning properly. It seems there may be an issue with my Ajax code. It doesn't seem to return anything. Do I need a specific jar file for this task? Below you can find ...

Discovering the most recently selected element in jQuery

Currently reading a jQuery tutorial from the Head First series and practicing with an example where I need to identify the last selected element. In Chapter 2, there is a task involving four images on a page. When a user clicks on any of these images, the ...

Attempting to retrieve information from PHP using JSON for communication with Android

Struggling to fetch information from PHP using JSON in an Android app. Here is the PHP script I am using: <?php # print(json_encode("[name=john]")); # ?> However, when trying to receive this data in Java, I encounter the following error: ERROR/log ...

Execute JavaScript/AJAX solely upon the selection of a button

Currently, my script is being executed immediately after the page loads and then again after a button click. Is there any way to modify it so that the script waits until I click the button before running? As far as I understand, this code runs asynchronous ...

How can I rectify the varying vulnerabilities that arise from npm installation?

After running npm audit, I encountered an error related to Uncontrolled Resource Consumption in firebase. Is there a solution available? The issue can be fixed using `npm audit fix --force`. This will install <a href="/cdn-cgi/l/email-protection" clas ...

issue with integrating promise in angular 4

I'm currently learning about promises and how to implement them in Angular. I have written the following code in StackBlitz. My goal is to display the array whenever it contains a value by using promises in Angular. This is my HTML code: <h2>A ...

Tips for expanding a fabric canvas to match the entire width of its parent division

specific scenario I have a cloth canvas placed inside a main section. How can I expand the canvas to cover the entire width and height of its container? Here is what my current code looks like: <div class="design_editor_div"> &l ...

Comparing the length of an array to whether the length of the array is greater than

What distinguishes checking an array's length as a truthy value from verifying that it is greater than zero? In simple terms, is there any advantage in utilizing one of these conditions over the other: var arr = [1,2,3]; if (arr.length) { } if (arr ...

Could you explain the distinction between these two arrow functions?

I'm puzzled about why the second arrow function is effective while the first one isn't. //the first one doesn't function properly this.setState = () => { text: e.target.value, }; //in contrast, this one ...

What is the best way to turn each function within the module pattern into a promise?

Utilizing Angular 1.5, I have developed a factory function that returns a literal object structured as follows: return { item: null, get: function() { return item; }, create: function() { if (this.get()){ this.remove(); ...