A JavaScript function for parsing a JSON string using the `new Function`

As I develop a script that parses the "rel" attribute of an anchor tag for a JSON String of settings, I am implementing code similar to this to convert the string into a JavaScript Object:

var settings = new Function("return " + relAttribute);

Would you consider this method effective for parsing a JSON String?

Answer №1

If you're looking for a solution, I suggest using json2.js along with the JSON.parse function. Here's an example:

var data = JSON.parse('{ "item1": "value1", "item2": "value2" }');
alert(data.item2);

Answer №2

Initially, consider making the var settings a function instead of an object. It's important to assess that.

Additionally, be aware that even if you do this, malicious code could potentially escape your function by attaching itself to the global scope, like window.

Lastly, dealing with line breaks might prove challenging.

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

Is ASP.NET capable of displaying an expandable grid view?

After searching online, I have yet to find a solution that meets my requirements. Currently, my DB view generates the following data: --------------------------------------------------- Company | Code | Total | Available | Used | Needed ---------------- ...

Using JQuery to delete an item by clicking on the delete button and then clicking on the item to remove it

I am currently using jQuery to dynamically create items on an HTML canvas for users to drag around and create drawings in a style similar to Microsoft Visio. However, I am struggling with how to remove these items once they have been created. While I know ...

Using Sweetalert2 to send data via AJAX POST request

Recently, I've been incorporating SweetAlert2 into my project and I want to create an "Add Note" feature. The process involves the user clicking a button, being directed to a page, and then the following script is executed: <script>swal({ ...

Why is my UITableView appearing empty after parsing JSON in Swift 4?

I'm facing an issue where the cells are not displaying any data. Despite successfully parsing using Decodable, I've tried numerous methods without any luck. struct MuscleGroup: Decodable { let ExcerciseID: String let description: String ...

The significance of package-lock.json: Understanding demands vs dependencies

Within the dependency object of package-lock.json, there exist both requires and dependencies fields. For example: "requires": { "@angular-devkit/core": "0.8.5", "rxjs": "6.2.2", "tree-kill": "1.2.0", "webpack-sources": "1.3.0" }, "d ...

Converting any given String into proper JSON format using JAVA

I am encountering an issue with parsing a String in a specific format using the Jackson ObjectMapper readTree API. Here is the code snippet I am using for parsing: ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(JsonParser.Feature.A ...

What is the best way to implement validation for a textfield to prevent submission if a negative value is entered?

I am working with a text field of type number and I have successfully set a minimum value of 0 to ensure that negative values are not accepted. However, I have encountered an issue where I am unable to delete the 0 once it is entered. Is there a way to fix ...

Navigating a JSON array using the Handlebars template engine

I have a JSON file and I am looking for guidance on how to display the information from it using the handlebars template engine: This is the template code: <script id="template-app" type="text/x-handlebars-template"> {{#each data}} Emai ...

Unable to cycle through an array of objects in JavaScript. Only receiving output for the initial element

var people = new Array(); var individual = { first_name: "Padma", identification_number: 1, region: "India" }; people.push(individual); people.push([individual = { first_name: "Balaji", identification_number: 3, region: "India" }]); people. ...

Changing dot notation to bracket notation in Angular

Having trouble using dynamic columns in a Primeng table? The column fields need to be in bracket notation for this setup. What if you have a column with nested JSON structure and you're not sure how to write that in bracket notation? Don't worry, ...

Encountering an XMLHttpRequest issue when making an HTTP GET request for improperly formatted JSON data

I am attempting to retrieve JSON data through HTTP in my Dart/Flutter function: Future<List<News>?> getNews() async { var client = http.Client(); var uri = Uri.parse('http://localhost:3000/news'); var response = await clie ...

A single snippet of JavaScript blocking the loading of another script

I've encountered an issue where the code seems to be conflicting with itself. The top part works fine, but the bottom part doesn't. However, when I remove the top portion, everything works as intended! You can see a working example of both compo ...

How to assign various column values to a PHP array in JSON format from a MySQL database

I am struggling with making a JSON Ajax request and receiving an array in return. After browsing through multiple questions, I came across this code snippet that I edited to fit my problem: var hej[]; function ajax_search(){ $.getJSON('test.php ...

Having difficulty creating a custom user registration field in Services 3.0 for Drupal

On attempting to submit data to my custom zip code field on the user registration form, I encounter the following issue: Sending post data to endpoint/user/register.json { "name":"testuser123", "pass":"testuser123", "mail":"<a href="/cdn-cgi/l/email-p ...

Issue: Unable to use the b.replace function as it is not recognized (first time encountering this error)

I can't seem to figure out what I'm doing wrong. It feels like such a silly mistake, and I was hoping someone could lend a hand in solving it. Here is my controller - I'm utilizing ng-file-upload, where Upload is sourced from: .controller( ...

Manually assigning a value to a model in Angular for data-binding

Currently utilizing angular.js 1.4 and I have a data-binding input setup as follows: <input ng-model="name"> Is there a way to manually change the value without physically entering text into the input field? Perhaps by accessing the angular object, ...

Switching buttons with AngularJS

I am currently working on a Github search app using the Github API in Angular. My goal is to make it so that when the user clicks the "Add to Favorite" button, the button disappears and the "Remove Favorite" button is displayed instead. I attempted to achi ...

Leveraging the power of AJAX to dynamically update information on a modal form using PHP's

After successfully preventing my modal form from closing upon submitting a value using ajax jQuery without refreshing the page, I encountered a new issue. Whenever I fill in my modal form and click submit to update the data inside my database, it does not ...

Prevent JSON-Pretty from being cached for JavaScript files that rely on it

I recently created a Squarespace website using a template that features a unique masonry type gallery layout, which can be viewed at the following link: . Although I was drawn to this template specifically for its gallery design, I've encountered seve ...

Include a new key value in JSON string created through the use of GSON.toJSON

String jsonResponse=Utils.getGsonInstance().toJson(Object); jsonResponse returns : [ { "Key":"1", "Code": "11", }, { "key":"2", "code": "22", } ] I want to modify the JSON-String by wrapping it in another Key like this: ...