The data I am sending my page is encoded in JSON, parsed using Javascript then displayed in an HTML SELECT element using a loop. The data arrives already sorted, but I am having issues keeping the correct order when decoding the JSON string, which nullifies the sorting applied on the data.
Sample data: {"test":{"4":"first","5":"second","3":"third"}}
Using jQuery's JSON parser and Javascript's eval() function, I am getting the following results:
{"test":{"3":"third","4":"first","5":"second"}}
It is not possible to modify the format of the data and the keys ("4", "5", "3") must remain in the same order. The real data is much more complex, but this sample illustrates very well my issue.
How can I maintain the order of the JSON data when parsing it from Javascript?
Use an array if you want to keep the order. That should be the only way to maintain the order in javascript.
Related
I'm trying to use AJAX to receive updates from a database without needing to reload the page. I've had very minimal problems thus far, however, I finally hit one. When retrieving information from a database, I have it convert all possible rows to an array. From there, I encode that array back to Javascript using $test1 = json_encode($array). In this same PHP function, I'm doing this to multiple pieces, converting those encoded arrays to a string by doing echo $test1."#".$test2."#".$test3. In Javascript, I split that string by the hashtag, and then grab the information of an array by doing something like test1 = array[1]. When doing that, I can retrieve that array entry as a string even though it's in the fashion of a Javascript array. Any ideas on how I can make it treat that string, which is setup like an array, as an array? Thanks in advance!
When retrieving the information from the AJAX response, you need to make it a Javascript Object. Convert the response to an object by doing JSON.parse(response) to get it to work
I'm new to programming, and I have been programming a small project with vanilla javascript, but I was using a lot of document.getElementById() tags, and I stored all of these in a javascript object, on a seperate file, but I was wondering If I could Just store that object on a SQL file, to make my project more organized.
I'm not sure if that's possible, I know that SQL stores data, so would I be able to store my JS object on a sql file, and import that object into my seperate Javascript files?
I'm trying to make sure if I can do what I want to do before I decide to start learning sql, but If it does do what I need, I was going to start incorporating it for organization, so I can learn it as I create projects.
You can use the JSON.stringify function to convert your javascript objects into strings. However, it is important to note that the only items within the javascript object that are converted into strings are: objects, arrays, strings, numbers, and values that are: null, true, or false. If you have references to functions or classes that have been instantiated, then these will be lost. You can convert the string back into a javascript object using JSON.parse.
One thing to consider before you do this is whether or not you need to perform database queries on the data that you are storing within the javascript object. If you need to search on the javascript object's data, then you should store the information directly within tables in the database. If you don't need to search on it, then converting the data to a string and saving it should be fine to do. Since it sounds as though you are using the data for your own purposes, doing this should be fine since extracting all of the data from the database shouldn't be an intensive task. Also, you can write your own scripts to parse the data.
Definitely, you can store as a JSON Blob
https://learn.microsoft.com/en-us/sql/relational-databases/json/store-json-documents-in-sql-tables?view=sql-server-ver15
I've read a lot about sanitizing and escaping of untrusted data, e.g. starting with this cheat sheet. But I didnt get the full picture yet. I am struggeling to understand, what principles I have to follow in order to parse JSON data safely.
Let's be more precise by means of an example. I retreive an Object in JSON format via an Ajax call from my server. The object shoudnt contain any malicious code, but.... you never now. So I parse the JSON data using JSON.parse in JS. The sesult is a multilevel JS object. I use the object in various way, e.g. creating table code via JS and writing it in the DOM.
Is this approach safe. Or do I have to somehow escape the invidual items of the object individually? Appreciate your help. Happy to concrete my example if necessary.
I'm ending up having to do hacks to convert 'true' to just true and it's creating code smell.
Is there a library like https://github.com/thephpleague/fractal that allows me to transform my response into the types I need?
In cases like this it's almost always better to fix the API to return data in a usable format rather than trying to post-process the result on the client.
In your case there are several routes you could take:
Store the list as a JSON string directly in the database.
This means you don't have to do any processing on the server and can just return it 'as is'. However you lose the ability to do queries on the data directly and need to resort to things like LIKE and string operations.
Store the data relationally, and process it on the server to turn it into JSON
Here you retain the ability to do queries on your data, but you may need to do several queries to get all the data you need and then connect it on the server. (eg. you would do one SELECT on the user table to get a user, and then you would need to do another SELECT on the friends table where the userid matches your first user. You would then need to merge these results to create your JSON.) This is usually the best way to do it.
You can also turn the result into JSON directly inside the database engine using a user defined function. For example using https://github.com/mysqludf/lib_mysqludf_json#readme
This is somewhat similar to 2, but it ties your stored procs to the JSON format.
I have a Extjs form which has around 50 fields to search. I am posting form data in JSON format to server. Before sending the data I am appying JSON.stringify method to convert the JavaScript object notation data into JSON form. but it seems after applying the function, the search operation has become slow. Is JSON.Stringify method costlier ? I want to know more details about the performance of the method, Please suggest alternatives for better performance.