Tips for transforming JSO into JSON data format

Here is an example:

var info = [{"name":"john","age":"30"},{"name":"smith","age":"28"}]

I am looking to convert the above json object to a format like this result:

[{name:"john",age:30},{name:"smith",age:28}]

Any suggestions on how to achieve this?

Answer №1

To properly extract information from a JSON string, it is recommended to use JSON.parse along with a reviver function:

var jsonString = '[{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]';

// The reviver function checks if the value can be converted to a number
// If so, it returns the number; otherwise, it returns the original value
var reviver = function (key, value) {
    var number = Number(value);

    return number === number ? number : value;
};

// By providing the reviver function as a parameter,
// each key-value pair will be processed to determine the final value
var data = JSON.parse(jsonString, reviver);

When the reviver function is called with reviver("name", "eric"), it returns "eric" since strings cannot be converted to numbers. However, calling it with reviver("age", "24") will result in returning the number 24.

It's important to note that although

[{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]
is an array and not directly JSON, the string version
'[{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]'
represents valid JSON formatted data.

Answer №2

let info = [{"name":"eric","age":"24"},{"name":"goulding","age":"23"}] 

This data is in JSON format. To convert it into a JavaScript object (JSO), we can use the method parse. By using JSON.parse, we can manipulate this JSON data as a JSO.

let convertedToJSO = JSON.parse(info)
let info = [{name:"eric", age:24},{name:"goulding", age:23}]

The above is the corresponding JavaScript object (JSO) representation of the data. If you need to print or save this JSO, you can accomplish that by converting it back to JSON using JSON.stringify.

let convertedToJson = JSON.stringify(info)

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

Using ng-style to apply a background image with a dynamic id

Hey there, I'm facing an issue here. The link provided below seems to not be working properly. Even though the id is set correctly within the scope, it seems like there might be a parsing error occurring. Any thoughts on what might be causing this pro ...

Currently, only the initial button is functional. I am uncertain if it is due to a script issue or if there is a rule that I inadvertently

As a beginner, I am eager to grasp the fundamentals and rules of Javascript. My goal is to create a basic example that involves a box with three buttons. However, I have encountered an issue where only one button seems to be functional despite having dis ...

Output PHP variable in JavaScript (and show the value within an iframe)

I'm trying to retrieve a value from a MySQL database using PHP, store it in a variable, output the PHP variable in JavaScript, and then display the value in an iframe. However, I'm having some trouble getting it to work... Here is my PHP code: ...

I am having trouble with cookies, as I am unable to retrieve them from my localhost server

Currently, I am in the process of developing a user validation system for my application. However, I have encountered an issue with validating a token as it appears that the necessary cookie is not being retrieved from my browser's storage. Strangely, ...

Utilizing jQuery's Chained Selectors: Implementing Selectors on Previously Filtered Elements

DO NOT MISS: http://jsfiddle.net/gulcoza/9cVFT/1/ LATEST FIDDLE: http://jsfiddle.net/gulcoza/9cVFT/4/ All the code can be found in the above fiddle, but I will also explain it here: HTML <ul> <li id="e1">1</li> <li id="e2" ...

Using the ng-repeat directive in an angular-nvd3 tooltip

Looking to dynamically repeat values in the tooltip content of an nvd3 graph? Not sure how to achieve this using ng-repeat with JSON data? Seeking guidance on alternative methods? I can help! $scope.options = { chart: { type: ...

Deactivate alignment for core/columns block in the theme.json file for Wordpress

Is it possible to completely disable the align (wide and full) options as well as the anchor from columns core block in theme.json? Here is my current code snippet: { "version": 1, "settings": { "blocks": { ...

Display header right button based on a condition in React Native using React Navigation

I am looking to conditionally display the Entypo new-message icon in the top right corner of the header based on a boolean variable (if true, then show the icon in the header). Here is a snippet of code where this functionality can be replicated. Thank y ...

Guide on how to automatically direct users to a page upon successful login in ReactJS

How can I redirect to the homepage after a successful login in ReactJS? Also, how can I display an error message when a user enters incorrect credentials? I have attempted the following code, but it does not successfully redirect to the homepage or show ...

How can Observables be designed to exhibit both synchronous and asynchronous behavior?

From: Understanding the Contrasts Between Promises and Observables In contrast, a Promise consistently operates asynchronously, while an Observable can function in synchronous or asynchronous manners. This presents the opportunity to manipulate code in ...

What is the best way to incorporate both images and text in PHP code?

I'm currently working on creating a large image call-to-action (CTA) for my new WordPress website. I've set up a new field group with the type "group" in ACF, and added some functions in PHPStorm. However, none of my images, text, or links are ap ...

Understanding how to sum the values of two separate dropdown selections using jQuery

Is it possible to combine and total up two different selections to display on the "Total" button below? I have added calculations to each selection. When a user selects a quantity, it should automatically sum up and display on the "Total" button, but I am ...

Deactivate a form on the page while another form is being used

My issue involves having two forms on the same web page with identical IDs, and I'm unable to easily change them. Both forms are necessary on the page. When I submit one form, it also submits the other form as blank, resulting in duplicate submission ...

Angular movie database using an API from an apiary

I'm struggling to create a link on movie posters that will lead to detailed movie information based on the movie ID. I have been going through their documentation, but I can't seem to get the api configuration to function properly. Every time I t ...

Is there a way for me to verify my Json file?

My current project involves using Titanium Appcelerator to create an Android application. However, I've encountered an issue while working with 'ACS JSON'. Specifically, when trying to utilize the posts query, I noticed that I'm receivi ...

Click here to get the button click link

Is there a method to obtain the link of a button click on a website? This is the Website and it features a play button. I am looking for a way to capture the link triggered by clicking the play button so that the audio starts playing automatically each t ...

What causes Vue to drop nested component data within a v-for loop?

Witness the mysterious behavior through my fiddles: Anticipated outcome: https://jsfiddle.net/o7c9mwzf/27/ By clicking "unshift," I add an element to the beginning of my items array. After setting its data, clicking "unshift" again should maintain the el ...

Identifying geometric coordinates within Canvas

Currently, I am adding drag and drop functionality to my HTML5 Canvas application. I have encountered a challenge in determining how to detect if the shape being clicked on is not a rectangle or square. For instance, within my 'mousedown' event h ...

Experiencing a Node.js application issue with the error message "ERR

I'm encountering some serious challenges with a Node.js app that I am developing using Express, MongoDB, and Mongoose. Everything seemed to be functioning correctly last night when I used nodemon server.js to start the server. However, I'm now fa ...

Create a query string using JavaScript and combine multiple parameters into a single param

I am facing a challenge where I need to construct a query string in JavaScript and nest various parameters within one of the parameters. In PHP, I can achieve this using the http_build_query function. However, when attempting to do the same in JavaScript, ...