Creating a Javascript file of JSON objects - javascript

I would like to create a Javascript file of JSON objects. I have a front-end application, which will take no more than 600 requested JSON objects from an API, and will perform a visualization on them based on some characteristics.
I am able to load in all the JSON objects into a list of JSN objects, but that is not what I had in mind.
Ideally, I would like to be able to load them all into a .txt or .js file, where, if someone wanted to, could look into this file and see all the JSON objects. Additionally, I could directly perform manipulations on this data.
I would like to stay away from using any database for this since the records, memory, and manipulations I'm performing do not warrant it.
Is there a specific subroutine or library that can assist me with this? I apologize if this question seems naive, I am new to JavaScript.

Related

how can i get data from .js file and update this file

I don't know if this is the best way but I would like to have a .js file with an object that I will update once a day. I would not like to make a database for this because my code already works for the object. Via API I will get the data for the day and I would like to update the .js file. I would like to keep the historic data in this file and use it to feed the website, the API would only be used at the end of the day. It is a website with data from covid-19, I am doing it just for learning, so I am open to new approaches. I try to keep this file in github, but for edit this i need to put my user and pass in code, i dont know how turn around this issue.
Storing JavaScript objects to files is almost always done in JSON format.
Converting an object to a JSON string is done with the JSON.stringify() function.
When you read the JSON string back from the file, you covert it back to a JavaScript object with the JSON.parse() function.

Find the structure of very long JSON format

I am fairly new to using JSON form, but need a specific part of the parsed JSON for my project. I was going to iterate through the proper object using dot notation. However the JSON I am getting from the API is EXTREMELY long (almost 60 pages) and I do not want to have to go through and trace the structure of it.
Is there anyway to compress the structure or figure this out programmatically?
I am using Node js if that makes any difference in this instance.

Merging millions of data using nodejs

i need help/tips
i have a huge amount of json data that needs to be merged, sorted and filtered. right now, they're separated into different folders. almost 2GB of json files.
what i'm doing right now is:
reading all files inside each folders
appending JSON parsed data to an Array variable inside my script.
sorting the Array variable
filtering.
save it to one file
i'm rethinking that instead of appending parsed data to a variable, maybe i should store it inside a file ?.. what do you guys think ?
what approach is better when dealing with this kind of situation ?
By the way, i'm experiencing a
Javascript Heap out of memory
you could use some kind of database, e.g. MySQL with table's engine "memory" so it would be saved in ram only and would be blazing quick and would be erased after reboot but you should truncate it anyways after the operation while it's all temp. When you will have data in the table, it will be easy to filter/sort required bits and grab data incrementally by let's say 1000 rows and parse it as needed. You will not have to hold 2gigs of data inside js.
2gigs of data will probably block your js thread during loops and you will get frozen app anyways.
If you will use some file to save temporary data to avoid database, i recommend using some temporary disk which would be mounted on RAM, so you will have much better i/o speed.

Object storage for HTML5 game - what to use?

I'm a fairly well versed programmer, so learning new technologies shouldn't be that big of an issue. That being said I'm currently attempting to make a card game in HTML5 using canvas/javascript etc.
The current question that I have is what to use to store instances of the cards. I was thinking about using XML to store the card data, but I'd like to limit the amount of work the browser has to do so the game runs more smoothly, I've heard JSON is a good alternative, but I'm just looking for suggestions. Thanks!
JSON is better in my opinion.
You can serialize objects to JSON at server side and send JSON string to client (browser), then your client will be able to parse JSON string into regular JavaScript object using JSON.parse.
In this way you'll not need to walk through XML to find particular nodes, but will just work with data in more convenient way using native JavaScript objects/arrays.
Also in most cases JSON will be more compact than XML so this can save bandwidth and speed-up data loading.
Also the data types stuff may be important here - JSON represents datatypes correctly (integers, booleans, floats, strings) and XML is storing them as strings so you'll need some additional attributes to set datatype during serialization and determine it during deserialization.
I am not sure how to do this without a framework, but what I would do is use Backbone.JS and create a model of what an instance would look like. Eg:{CardNumber:'2', CardColor: 'red', CardClass: 'hearts'}. Now I would create a collection to hold all these models, see backbone collections.
So I would store all this data client side, and possibly provide the user with an option to save the game, to persist this data to a database. This stores it as JSON and then when you persist it to the database, you can serialize it to get the individual components.
If you dont want to save to the db and do not want to use a framework. Try stack/queue implementations in Javascript. See:How do you implement a Stack and a Queue in JavaScript?
I hope that answers your question.
Stick to JSON because JSON is just a string representation of plain JS objects, and browsers are very comfortable with it. JS have no good XML handling and that will be too expensive.
Use HTML5 localStorage for keeping data until you really need to sync with the server. Frequent server operations will cause your game to suffer. Use bulk data transfers instead of many small server connections (for example at the start and the end).
Consider using a game library if the canvas graphics are intense. I have used http://jawsjs.com sometime back, but there should be better libs available out there. Selectively render only the dynamic objects, not everything on canvas.
JSON in conjunction with localStorage is a great way to go.
There are libraries available to serialize and deserialize Javascript objects and allow you tp store and retrieve it from localStorage. Simple Github search is a good way to start

How to do Javascript access a local database in txt format

I am newbie working on a 100% js prototype. It consist of 3 docs: an html page full of xml tags, a small dictionary in a text file format, and a js file with jquery.
The js needs to parse the xml tags (no problem here) and look into the mini-dictionary list for available translations.
Which is the best way to implement the mini-dictionary list. (No more than 50.000 records). Is there a way to load the list into a memory database and access it from js? Which is the usual path to take in this case? What is the simplest and machine-independent way to do this?
Any directions as to where should I research are greatly appreciated.
I would suggest encoding mini-dictionary with JSON data format, and then using AJAX to get that file and parse it. But then you are risking someone will just copy whole dictionary and steal your work.
That is, if you are not using server side language, like PHP. If you are using it, then just store everything into database and request just specific words with AJAX.

Categories

Resources