Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store data in JSON object

Tags:

json

jquery

I have data with the following structure:

Unique ID (int) | First Name (string) | Last Name (string) | Pct (double)

A set of the data might look something like this:

1 | John | Hancock | 49.5
2 | John | Hancock | 95.0
3 | Jane | Hancock | 89.5
4 | Jane | Hancock | 73.5
5 | Jane | Hancock | 96.5
6 | Fred | Flintstone | 45.8

What I need is to store this data on the client-side so that it is accessible to the browser and can be manipulated using jQuery.

For example, I have a dropdown (select) on the page that has three values in it:

1) Low Percentile
2) Mid Percentile
3) High Percentile

When the user selects one of the values in the dropdown then I need to be able to select items from my data above that qualify for the selected value. For example, if they select the Low Percentile value then I need to retrieve all of the items from the data that have a percentage less than 60%.

I'm fairly versatile with jQuery, but I've never used it to store anything of this sort for client-side retrieval. Can I somehow store this data in JSON objects on the client-side, which in turn can be selected from and manipulated at will?

like image 914
Jagd Avatar asked Mar 12 '13 15:03

Jagd


People also ask

How can we store files in JSON object?

Method 1: Writing JSON to a file in Python using json.dumps() The JSON package in Python has a function called json.dumps() that helps in converting a dictionary to a JSON object. It takes two parameters: dictionary – the name of a dictionary which should be converted to a JSON object.

Can you store data in JSON?

JSON is perfect for storing temporary data. For example, temporary data can be user-generated data, such as a submitted form on a website. JSON can also be used as a data format for any programming language to provide a high level of interoperability.

How are values stored in JSON?

Data is stored in a set of key-value pairs. This data is human readable, which makes JSON perfect for manual editing. From this little snippet you can see that keys are wrapped in double quotes, a colon separates the key and the value, and the value can be of different types. Key-value sets are separated by a comma.

Which database is best for storing JSON data?

The best database for JSON A JSON database like MongoDB stores the data in a JSON-like format (binary JSON), which is the binary encoded version of JSON, and is optimized for performance and space. This makes the MongoDB database the best natural fit for storing JSON data.


1 Answers

So basically:

var javascriptObject = $.parseJSON(jsonString);

var jsonString = JSON.stringify(javascriptObject);

JavaScript is much like any other OOP.

var foo = {}; //NEW Object
foo.bar = 1;
foo.bars = 2;
foo.bar.date = 3;
var jsonString = JSON.stringify(foo);
//jsonString is now a "JSON Object" that holds the data found in object foo.
console.log(jsonString); //To see what it looks like.

However if all of this is for client side usage, just stick with JavaScript objects. No need to convert them to JSON. JSON objects are really for sending data to other sources as they are strings. Usually used in AJAX requests.

EDIT BASED ON COMMENT

$(document).ready(function(){
    var globalObject = {};
    globalObject.foo = 1;
    globalObject.bar = 2;

    $(el).change(function(){
        globalObject.foo = $(this).val();
    });
});
like image 91
Leeish Avatar answered Oct 20 '22 09:10

Leeish