What is the method for converting a JSON file into an array list?

I am looking to convert my JSON file into an arraylist that can be utilized to showcase the information on a webpage.

Here is my HTML file snippet:

var xmlhttp = new XMLHttpRequest();
 xmlhttp.onreadystatechange = function() {
 if(this.readyState == 4 && this.status == 200) {
    console.log(JSON.parse(this.responseText))
}
};
 xmlhttp.open("GET", "PATIENT51.txt", true);
 xmlhttp.send(); 

This is how my JSON file looks like:

{
“resourceType”:”RiskAssessment”,
“Notes”: [
{“date”: “05/13/2019”, “note”: “my notes”},
{“date”: “02/22/2018”, “note”: “cool”}
]
}

I am struggling to access the 05/13/2019, I have attempted using

myObj.Notes[0].note;

Unfortunately, it returns as "undefined". My end goal is to create a table where each date will have an associated note.

Answer №1

Avoid using balanced quotes or similar characters in JSON. “ and ” are not considered valid JSON characters; use standard quotes instead. '"'

JSON.parse('{ \
“resourceType”:”RiskAssessment”, \
“Notes”: [ \
{“date”: “05/13/2019”, “note”: “my notes”}, \
{“date”: “02/22/2018”, “note”: “cool”} \
] \
}');

In my experience, Chrome throws the expected error:

VM18:1 Uncaught SyntaxError: Unexpected token “ in JSON at position 2
at JSON.parse ()
at js:13

Stick to using standard quotes "

Upon inspecting the outputted Object in console.log, you'll notice that the element where you want to retrieve values from is named 'Notes' and not note. Thus, you need to access the variable of interest using myObj.Notes[0].note.

myObj = JSON.parse('{ \
"resourceType":"RiskAssessment", \
"Notes": [ \
{"date": "05/13/2019", "note": "my notes"}, \
{"date": "02/22/2018", "note": "cool"} \
] \
}');
console.log(myObj);
console.log(myObj.Notes[0].note);

If you need to replace curly/balanced quotes in your code snippet with a file, you can follow the solution provided in this answer:

var xmlhttp = new XMLHttpRequest();
 xmlhttp.onreadystatechange = function() {
 if(this.readyState == 4 && this.status == 200) {
    console.log(JSON.parse(this.responseText.replace(/[\u2018\u2019\u201C\u201D]/g, '"')));
}
};
 xmlhttp.open("GET", "PATIENT51.txt", true);
 xmlhttp.send(); 

However, it's recommended that whatever generates PATIENTS1.txt uses standard quotes instead of balanced quotes.

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

No issues encountered during indexing with Elasticsearch Bulk

When I run the code snippet below on the cmd line in Windows to bulk index roughly 3 million documents, nothing seems to happen. The process completes in less than a second without any output, and the index is not created as expected. curl -H "Content-Ty ...

Obtain similar words and related terms

My goal is to extract information from www.thesaurus.com, specifically related to synonyms and antonyms of a word. In this case, let's consider the word angry. I am interested in the words displayed in these images (focusing only on the first 2 blocks ...

Can we utilize the elements in Array<keyof T> as keys in T?

Hello, I am trying to develop a function that accepts two parameters: an array of objects "T[]" and an array of fields of type T. However, I am encountering an issue when I reach the line where I invoke el[col] Argument of type 'T[keyof T]' i ...

Arranging information based on key values

I have a data file containing numerical values in JSON format: { "automotive_auto buying and selling_1_2":2.66883E-06, "automotive_auto infotainment technologies_1_9_1":8.67917E-06, "automotive_auto insurance_1_3":1.41038E-06, "automotive_auto ...

Ensure that the promise is fulfilled only if a specific condition is met

I have a complex if-else condition in my code that includes different promises. Once the logic determines which condition to enter and executes the corresponding promise, I need to ensure that a final promise is always executed. if (a < 5) { vm.pr ...

What is the best method for retrieving the character's ID from within an object?

Exploring my collection of movies, I discovered that each movie contains an array of characters. My goal is to display specific information about each character (such as their name and age) when clicked on, requiring me to obtain the id of each character. ...

Content that is set to a fixed position within a hidden element will remain at the top

translate3d(0%, 0px, 0px); is causing issues with my position fixed element. In my demo, you'll notice that clicking the button should open up the content just fine, but it is supposed to stay fixed at the top in a position fixed. Therefore, when scr ...

When attempting to call a class in Node.js with Express, the return value is coming back

I am relatively new to working with Node.js and the Express framework. I am attempting to pass a value to a JavaScript class in order to process a query and make a call to the database, expecting to receive a result array. Here is the code snippet I am cur ...

Using jQuery with multiple selectors can become tricky when dealing with elements that may or may not be present

I decided to be more efficient by using multiple selectors instead of rewriting the same code repeatedly. Typically, if one element exists then the others do not. $('form#post, form#edit, form#quickpostform').submit( function() { ...

The header of the bootstrap table will now feature a new button for toggling the visibility of the filter control

I'm currently utilizing a bootstrap table with filter control, and I am looking to incorporate a button (icon) on each of the table headers in order to toggle the visibility of the filter control. Is there a method to achieve this? An example of a b ...

Bringing in More Blog Posts with VueJS

Exploring the Wordpress API and devising a fresh blog system. As a newbie to VueJS, I'm intrigued by how this is handled. The initial blog posts load as follows: let blogApiURL = 'https://element5.wpengine.com/wp-json/wp/v2/posts?_embed&p ...

Setting up dynamic routes in a Vue.js Express application

I am currently working on a project that involves creating a basic Vue.js Express profile interface. This interface is responsible for retrieving profile information of a specific user based on a unique ID assigned to each user. The .get() request in Vue.j ...

Issues with Node.js and AJAX

I am having trouble with my ajax request to retrieve data from node.js. The interaction between the two seems off, but I can't pinpoint where the issue lies. Below is the snippet of my ajax get request: $.get('/notificationsNumber', ...

Does using .detach() eliminate any events?

I have a DIV that is filled with various content, and I am using the detach() and after() functions to move it around within the document. Before moving the DIV, I attach click events to the checkboxes inside it using the bind() function. Everything seems ...

Convert an object from java.lang.Object to an ArrayList

Is there a way to transform this type of java.lang.object into an array or List of java.lang.Object: [ {procedure_name_txt=nameText, procedure_name_ets_id=ID-1234, procedure_ets_desc=description_123}, {procedure_name_txt=deafness, procedure_name_ ...

Leverage Azure Data Factory for extracting JSON data from a specific column

Let me explain my current scenario I am working with an Azure Table as my source, and I need to populate an Azure SQL database as my target. The structure of the source table is as follows: id file_name metadata 1 file_1.txt {"Company": ...

Include a numerical suffix to the file name of an image if the corresponding value is already present within the database (using Codeigniter

When users upload images to the 'gambar' folder directory, I want to save the image path and data to a database table. The code below dynamically adds a counter to the file name if it already exists (e.g. my-path.jpg, my-path.png, my-path1.jpg) ...

Updating JavaScript alert title in Android webview: A guide for developers

I have integrated a web view in my Android application, which contains a button. When the button is clicked, it triggers a JavaScript function that displays an alert box with the title "The page at 'file://' says". I would like to customize this ...

Tips on sending template variables to JavaScript

Greetings, I am currently facing an issue with passing a template variable to javascript in order to create a chart. Unfortunately, Django treats all js files as static files which means that dynamic template variables are not accessible within javascript. ...

obtainServerSideProps query parameter

Hey there, I'm trying to use NextJS and its getServerSideProps function to send an API Request, but I'm having trouble passing my ID Query Parameter along. The URL for my API is: http://localhost:3001/product/${id} Below is my code: const rout ...