Editing JSON file at multiple positions without rewriting complete file - javascript

I have a JSON file that contains lots of nodes. Each node has a unique id. The following is an example skeleton structure of the JSON file.
{
"node1": {
"id": "1",
"name": "student",
"properties": {
"name": "Ram",
"lastname": "Kumar",
"age": "20"
}
},
"node2": {
"id": "2",
"name": "Teacher",
"properties": {
"name": "Ram",
"subject": "Computers"
}
}
Now, this was just sample data. Suppose, I have to find node 2, provided I have the position of the node in the file. That is it's beginning and the end. I create a read stream from the start position and the end position of the file to get the object within the complete JSON object in the file. Now, using JSON.parse function I am able to create an object within the program after parsing it.
But, now I use a writestream to make the changes in the object at the required positions after computing them.
For example, I have to edit the subject property of the teacher, and the main rule is I cannot completely write the object again. Rather, create a writestream at the position where there is subject property and just edit that value.
So, the question is how to do so? And that to with multiple positions to edit, that is editing multiple properties, without rewriting them all.
I know I might face an issue if I use multi-threading for this to simultaneously edit the multiple properties at once, but I need a work around that too.

Related

Add element(Data) to a json file in javascript without node.js [duplicate]

This question already has answers here:
Local editors in HTML5
(5 answers)
Closed 12 months ago.
I have done lots of research before I decided to post this question but I still can find a way of adding an element/data to a json file which is stored locally. I have managed to open and read it but not add data.
Here is what I would like to happen:
before I run the script.
{"logins" : [
{
"name": "test",
"usernam": "t",
"passwor": "ber"
},
{
"name": "test2,
"usernam": "chi",
"passwor": "Chik"
}
]}
and then I really on a button click I would like to add a new element/data so It looks like:
{"logins" : [
{
"name": "test",
"usernam": "t",
"passwor": "ber"
},
{
"name": "test2",
"usernam": "chi",
"passwor": "Chik"
},
{
"name" : "test3"
"usernam" : "tester"
"passwor": "tester2"
]}
and all of this preferably should be done without node.js.
I am running this through a script tag in my html
Many thanks.
I assume you have a local .JSON file you are trying to edit with plain JS.
And the answer is no. Without a server, that is practically impossible.
You can however change it client-side, but that'll just be for you, and would never save anywhere else.
This is all for security, because letting a web page change a file... Could also allow it to delete files, including system ones.
Use something simple like Node.js for a backend server that can handle this.
I'd also recommend hashing passwords.

How do I make a basic typeahead search of JSON data using javascript or jQuery?

I have a JSON data file stored locally on my PC. The file is an array of various "name": "value". For instance -
[
{
"name": "Joe",
"id": "0",
"age": "24",
},
{
"name": "Mary",
"id": "1",
"age": "25",
}
]
Now I need to make a typeahead search box for this kind of JSON data using javascript.
The code I wrote is displayed in the image:
Where "dummy" is the name of my locally stored JSON file. The code is not displaying the suggestive data from the JSON file and just gives me an input search box to enter stuff. How do I make it work? Kindly correct me and help.

jQuery plugin with multiple instances to make events act on the current item

I am building a JavaScript/jQuery Plugin which will take a JSON array of objects and generate a popover list item selector/picker. You click an item/input on the page and the popover like the image below opens with your JSON list data.
Key Features:
Search filter
Paging and items per page option
Categories and tab panel/DIVs to organize the list items into different screens on the popover
Clicking an item selects that item by firing a custom DOM event which you can plug into to save the selected value to a DB or do whatever you want with it.
Makes list items from JSON array of objects
Categories can be plucked from the same JSON data and will build separate list objects for each category
JSON Data Format
Right now the JSON array of objects looks like this below:
var mock_data_types = [{
"type": "first_name",
"name": "First Name1",
"description": "Random generated First Name",
"example": "Jason",
"category": "person"
}, {
"type": "address",
"name": "address2",
"description": "Random generated Last Name",
"example": "Davis",
"category": "address"
}, {
"type": "domain",
"name": "Domain Name3",
"description": "Random generated WWW Domain Name",
"example": "google.com",
"category": "it"
}];
Current Problems
1)
When there are multiple popover pickers on the page, when you select a item it updates that selected value for all the input fields that have the popover instead of only the current open one.
I believe the code needs to maybe keep track of multiple instances
I have tried to do this but keep failing. Can anyone hep with this issue?
2)
The complete code base is crap and can be improved as I am a PHP developer and learning JS still!
Demo JSFiddle
https://jsfiddle.net/jasondavis/9fpc3LLo/
Great job with your plugin.
So, perhaps you could use it as a jQuery-UI (plugin) widget. You can get the docs here to use the jQuery-UI widgetFactory. I think this can help you with your first problem and give you some tips to improve the second one...
Hope that can help you.
Regards!

Angularjs: 'SyntaxError: Unexpected token , ' when loading object

I'm trying to use Symphony CMS to output its XSL as JSON, for use in the angular phonecat app example from the tutorial. My list view json is output as an array with [{ }] brackets around it. My detail view json is output with only { } around it:
{
"position": 1,
"order": 3,
"total": "1",
"id": "sunt",
"sym_id": "21",
"cat": "back-end",
"imageUrl": "http://localhost:8080/workspace/images/phones/Nondell-streak-7.0.jpg",
"name": "Sunt",
"done": "No",
"priority": "medium",
"date-created" : "18 November 2015",
"date-modified" : "19/11/15 10:41am",
"date-due" : "18/11/15 2:13pm",
"snippet": "Quam nihil molestiae"
}
When loading my page the list view loads. When I click on an item the detail view tries to load but I get: SyntaxError: Unexpected token ,. Why? The syntax seems normal to me?
A big problem I am struggling with is that my json is created dynamically with the CMS but no actual files are on the server as far as I know.
Before I was getting the error: Error in resource configuration for action get. Expected response to contain an object but got an array so I made some changes so the list is output as an array, and the single views are output as an object.
My json is generated on localhost:8080/json/{parameter}. The param is 'todos' when not set. So my list view is situated at localhost:8080/json/todos. The param takes the selected object's name, so my detail view would be at localhost:8080/json/foo when I press the link generated by angular.
I hope my question is complete and makes sense :D

Bilevel Partition D3 create a simple 2 layer

This is the first time I am using D3 and was wondering whether someone could explain how I can create the Bilevel Partition with only two layers.
A complete & not complete section. then the complete section shows avergae corrrect and incorrect.
I've had a look at this code http://bl.ocks.org/mbostock/5944371
but I am not fully aware of what it does.
The code doesn't really care what data you throw at it, as long as it is formatted the way it expects.
Right now, this is the JSON file it is loading in.
Make your own JSON file that looks like
{
"name": "sections",
"children": [
{
"name": "complete",
"children": [
{"name": "correct", "size": 10},
{"name": "incorrect", "size": 30}
]
},
{
"name": "incomplete",
"size": 20
}
]
}
And see if it works when you load that in instead of flare.json. If you load the file locally, use firefox to check out the code, chrome will block requests for local files.
I have it working somewhat on tributary, you can change the data and the code, and fork it if you want to save your edits. here it is

Categories