Im passing my python list to a html element and reading it with JS, but i get this error below.
var test = document.querySelector('#my_variable').value
document.write(test)
var options = {
series: [{
name: "Desktops",
data: test
When you move data between different programming languages, you use some kind of intermediary. When working with JavaScript, it's almost impossible to avoid using JSON, as that's it's native intermediary. You can use it in many more places, but that's beside the point.
Python also natively supports JSON with it's json module. You should convert any data you have to a Python dictionary or list, which you can then turn into a JSON string using the module.
You can then pass that string to the frontend, from where you can parse it with JavaScript's JSON, which should solve your problem, or at the very least get you closer to a solution.
Related
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 encountered a problem with JSONs in web client-server application.
Context: scala (could be any typed language with inheritance), typescript+angularjs, json representation in NoSql postgresql, same json sent to web client.
How can I add type to json representation to get features such as:
describes generics like List
enables easy usage of inheritance in javascript/typescript world
can be deserialized line by line (like SAX) by json deserializer to get fast transformation with minimum memory used
Adding attribute to object like {myList: (...) , (...), type: ?? } interferes with point 3 due to no guarantee of attributes order.
Adding type as attribute name List#Integer#: {myList: (...) , (...)} makes the code ugly on client side due to additional wrapper/prefix everywhere.
How to solve this problem? Maybe somebody knows of Scala json library that already supports types?
Many libraries just assume that you know what type you are loading...
Workaround: Optimised deserializing is needed only on server side. For web client I transform JSON from List#Integer#: {myList: (...) , (...)} to {myList: (...) , (...), type: "List#Integer#" }
Adding type node to every object inferfers with frameworks, components i.e.: angular tree accepts only one children container at each node. Drag&drop doesn't work then. Working with flat type attribute is more predictable.
I'm trying to send an object graph from Python to JavaScript running in a browser, and I was wondering whether there is a pair of ready-to-use libraries for handling serialization on the Python side and deserialization on the JavaScript side. JSON does not support object references out of the box, the docs for JS-YAML say it's not production-ready in a browser environment, and I didn't find anything for XML. Any suggestions?
edit: Here's an example for what I mean by "JSON does not support object references out of the box": I have a shop database with products and orders and a many-to-many relationship between them. If I put a bunch of orders into the Python JSON serializer, the result will contain multiple serializations (copies) of each product, because the JSON serializer has no way of saying, "I've serialized this product already, so I'll just insert a reference to it". So I put the result on the wire and deserialize it on the client, and now I have multiple JavaScript objects representing the same product, which is bad.
how about jsonpickle? JS is awesome with json out of box, adding json pickling in python is the missing link:
jsonpickle
New to JSON, just trying to get my feet wet.
I know how to do this with XML via javascript, but am trying to learn how to handle JSON objects so I can switch over.
Basically I want to search through all "permalink" tags in the following JSON object and, when I find the right one, save its corresponding "title" and "id" tags to javascript variables:
http://api.soundcloud.com/users/goldenstatewarriors/tracks.json?client_id=02db8e29aa2fb5bf590f478b73137c67
Can this be done with only javascript (no PHP)? The main issue I'm facing is simply grabbing the text from the page and converting it to a json object.
You need to use a JSON parser in order to transform the JSON string into an object you can handle natively in JavaScript. Recent browsers have this functionality built in as JSON.parse(), but obviously this will not work in older browsers (we're talking very old browsers here).
A solution to that problem is to use the JSON parsing library available here. If native browser support is detected, it simply uses that, otherwise it has a JavaScript implementation to achieve the same result. The file you'll need is json2.js - simply include that as you would any other library and away you go!
An example of the code would be:
var dataObject = JSON.parse(jsonData);
As a side note, XMLHttpRequest is somewhat of a misnomer these days. It is simply a mechanism for making HTTP requests and retrieving the data returned, it doesn't have to be XML. It can be plain text, (non X)HTML, JSON, anything. In fact, I don't think I've seen anything in the wild return actual XML data for an XMLHttpRequest in a very long time.
how to convert this json to database:
var obj = {
'one' : ['qq','rr'],
'pizda' : { }
}
and then retrive it and use it?
You could store it in mongodb without too much effort.
db.foo.save(obj); // Save obj to a database
db.foo.find(); // find it again.
You might want to give a bit more details.
Assuming that the database runs on the webserver and you thus need to pass the JSON as a string around between webserver and webbrowser, then the answer depends on the server side language you're using.
If you're using PHP, then you can convert between a JSON string and a PHP array using json_decode() and json_encode()
If you're using JSP/Java, then there are several libraries available to convert between a JSON string and a Java object, the popular ones being Google Gson, JSON.org and Jackson.
If you're using ASP/.NET, then there are several libraries available as well, under each Google JSONSharp.
Also see the bottom of this page for an overview of several JSON parsers/formatters in various languages.