Processing.js a way to create JSON - javascript

Is there some kind of constructor or some way to create Json object in processing.js? I know that in the Processing library have JSONObject but in the processing.js there is nothing like that everything that I found for processing.js is how to load JSON but I need some kind of way to create it. If there is way can someone show me how to do it.

JSONObject is used by Java (or Processing in Java mode) to create JSON Strings that can be used by JavaScript.
Processing.js is already JavaScript, so you don't need to go through all that trouble.
If you have an object in JavaScript (and Processing.js is already JavaScript), then you can just use the JSON.stringify() function that's built in to JavaScript (and Processing.js).
But again, you might not even need to do this. Processing.js is already JavaScript, so you can probably just use the objects directly in JavaScript code without converting them to JSON first.

Related

Can I use SQL to store my Javascript objects?

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

Handling JSON data safely in JS and HTML context

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.

Can Kotlin or Swift parse JSON just like Javascript?

If I'm using javascript (or TypeScript), I can do following (just idea);
object = JSON.parse(jsonString)
And I can just use it like this,
alert(object.property);
Super Simple.
If I'm using Java, I need to create classes and parse it to use it. I understand.
How about Kotlin and Swift. They have optional types, so why single line, Javascript-like simple parsing doesn't exist for them, or does it? (Without even data class or going through JSON's properties)
If you look up what JSON stands for it's no wonder why JavaScript has "native support" for it: JavaScript Object Notation
In Kotlin you'll need to use libraries for parsing JSON, I'd recommend Jackson for that, a library widely used with Java already.

Where can I find JS JSON info on raw JSON manipulation?

I can do it via jQuery and/or Underscore JS as arrays of data, but I want to learn the API for just pure RAW JSON. For example, I know there is:
delete json.item
Which just simply deletes the item, but is there a set place I can go learn the full API?
--UPDATE--
I guess this came out wrong. I want to learn about Douglas Crockfords library, all of it. The JSON.func() library, not just "JSON" which is a string. For example he has a function called stringify etc. His site is a total mess, so I was wondering if there was a better place to learn about that JSON library (which has become the standard.)
There is no such thing. Raw JSON is a string, and attempting to manipulate it will only serve to damage it. Always manipulate it after decoding it.
delete will modify the property of an object, it won't let you manipulate JSON (unless that JSON is stored as the property of an object, in which case it will delete it entirely).
You appear to be asking about how to modify a JavaScript object, in which case, the "API" can be found at https://developer.mozilla.org/en/JavaScript/Reference

Javascript and C# COM interaction

I am using C# COM Interop techniques to interact with some device from website using ActiveX. Right now I can call C# codes from javascript and pass only string values. But I intend to pass javascript structure into C# method and the reverse. How can I do that.
Right now as an alternative I pass JSON formatted string from C# code and generate javascript object runtime using eval. But I want more control on that.
Thanks
Maksud
I ended up using NewtonSoft's JSON Library as I could not find native solution.

Categories