Creating nested json object using mysql query - javascript

I currently have a json file that is hard coded. Here is a snippet:
{
"name": "Projects",
"children": [
{
"name":"Project_Category(example:MobileApps)",
"description":"category",
"children": [{
"name":"Sub_category(example:MusicPlayer)",
"description":"some_description(example:The music app will play music from an android device...)",
"children":[
{//child 1 of the MusicPlayer subcategory
"name": "Actual_project_name(example:JukeBox)",
"description":"Actual_project_description",
"children":[
{"name":"projectGroupMember1", "email":"groupMember1Email#yahoo.com"},
{"name":"projectGroupMemeber2", "email":"groupMember2Email#yahoo.com"}
]},
{ //child 2 of the MusicPlayer subcategory
"name": "another_project_title",
"description":"another_project_description",
"children":[
{"name":"projectGroupMember1", "email":"groupMember1Email#gmail.com"},
{"name":"projectGroupMember2", "email":"groupMember2Email#gmail.com"}
]
}
]//end of MusicPlayer's children
}, //end of MobileApps children
...
Having a database where all this data is stored, I have been trying to use php and nested mysql queries to generate this output to a json file which I will use for other tasks (data visual). My goal was to generate this file everytime the web application is opened so I can the have the most up to date version of it from the database. However, I have been having difficulties using nested mysql queries to create this output. So my question is, given the way my json file is structured is this feasible? Is there a better approach I should do instead? Any suggestions would help.

mysql does not generate any nested structure for you. You have to do it by yourself.
There are many frameworks that would do it for you. The question is what server you have.
For example there is cakephp for Php which could give you a very similar structure. Also ASP.NET MVC with code first can do that. There are tons of possiblities.
If you want to do it without any framework, we still need to know, which server you use.

Related

How to filter data from all the records when the records are paginated?

I am working on a spring boot app where I am using Postgres for my data storage,I am using pagination to structure my data.
I recieve data like this:
{
"messages": {
"dtoList": [
{
"acknowledge_msg": "null",
"status": "QUEUED",
"msg_id": 2021082012204616000,
},
{
"acknowledge_msg": "null",
"status": "QUEUED",
"msg_id": 2021082012204575500,
},
],
"totalRecords": 4,
"pageSize": 2,
"pageNumber": 1,
"numPages": 2
}},
Now in my react page,when I will navigate to pages,I will simple do this api call with page size and page number and It will give me a response.
Now I want to apply filters but filters need to filter from all the records not from respective pages.
How can I achieve this?
What you want to achieve, regards only the backend, not the reactjs application.
You should send to your spring-boot application the query you would like to perform, then apply it into your postregsql query. It would update also pagination, since less results will be presented.
Maybe show here your frontend code, instead of the JSON data, and it would be easier to help you applying the remote filters.
As it was said before, the only proper way to achieve this is to do that on backend.
You can, of course, read all of the pages and filter them in frontend, but this is an awful solution

Nested json data in Angular 2 - how to filter?

I'm new with Angular, Js etc, and have a problem with understanding how should work with nested data. For example:
I have four json files:
categories
subcategories
posts
comments
It's better to have 4 different files like above, or one like this:
{
"id_category":"1",
"name":"business",
"subcategories":{
"id":"1",
"name":"ecommerse",
"posts": {
"id": 1,
"title": "New post"
"comments": {
"id": 1,
"text": "text text text"
}
}
}
Of course it's only an example, but for that example I need to find comment by Id = 1 to get information about which post this comment is related to, which subcategory and category.
Now I have four different files, and services to get data from json files. I can get a specific comment by ID:
getComment(Id: number) {
return this.comments.find(
(comment) =>
comment.id === Id
);
}
ok, fine. But If I want to get information about post, subcategory, and main category for this comment? What should I do?
It depends on what the specific needs of your application are.
With this structure:
{
"id_category":"1",
"name":"business",
"subcategories":{
"id":"1",
"name":"ecommerse",
"posts": {
"id": 1,
"title": "New post"
"comments": {
"id": 1,
"text": "text text text"
}
}
}
You could iterate over Categories and display a list, then allow a user to select a single category, and assign that to var currentCategory.
You could then iterate over currentCategory.subcategories to allow the user to select a subcategory and assign that to var currentSubCategory. You would keep drilling down then into currentSubCategory.posts, allow the user to select a post, assign that to var currentPost and then iterate over that to display currentPost.comments.
If you're fetching from a database in order to allow the user to drill down into the data for display only, then something like this would work.
If you're maintaining data in JSON files, then I would look at something like JSON Server https://github.com/typicode/json-server
If you're building something more substantial and you have a database backend, make use of the database and don't try to recreate that functionality in your JSON, use JSON as a transport for the data, but don't try to replicate entire tables or complex data structures in your front end code, just fetch what you need as you need it, as that will make for a much more stable and scalable application, and will also make it easier for you do fetch and update data in small manageable chunks.
For mocking a data backend using JSON, consider json-server
https://www.npmjs.com/package/json-server

Express/Loopback/NodeJS is there a possibility to add an "index" to db field

we are using Express/Loopback Framework for our backend and what i am trying right now to assure that the indexes on some fields in models are created int he MongoDB. Is there a possibility to use some kind of meta-tags in models, on hand of which DataJuggler creates indexes in MongoDB?
Have a nice day!
As M1kstur have mentioned in the comments, you can define indexes in the model definition JSON file:
"indexes": {
"name_age_index": {
"keys": {"name": 1, "age": -1}
},
"age_index": {"age": -1}
}
Also note that
Indexes will not be automatically created for you, even with NoSQL
datasource connectors like MongoDB. You must run automigrate
or autoupdate
create your indexes!

Json format issue in d3 pack layout

I am very new to d3. I have 3 days old d3 knowledge. I was trying to make one pack layout but I am not able to call translate(of transform) function based on the data in external json file. My json file is not formatted as name, children order (which has been used most of the examples). So, can any one clarify that whether we must have the json file in proper format like in tree structure to get the proper pack or tree layout. My json file format is:
{
"sourcefile":"Script",
"structure":{
"Links":[
[
"step1",
"port1",
"step2",
"port2"
],
[
"step3",
"port3",
"step4",
"port4"
]
],
"device":{
"step1":{
"args":{
"pin":[
"XXXX",
100
]
},
"device_type":"console"
},
"lock":{
"args":{
"username":[
"XXXX",
"test"
],
"address":[
"XXXX",
"10.0.0.1"
]
},
"device_type":"Light"
}
}
}
}
It it's true..I was wondering if anyone can tell me about some online tool to format this json file into the following format..
{
"name": "Names",
"children":
[
{ "name": "John", "size": 100 }
]
}
Your intuition is correct. The pack layout requires input data formatted as a hierachy. It seems unlikely that an online tool to convert general JSON data to this structure would exist, as such a tool would almost have to be custom-built for each particular data set. However, d3 itself has utility functions to help you to create the required structure. I'm referring to the Nest utilities. Looking at your input data, it's not at all obvious how to structure it in a hierarchy, so I can't offer any specific implementations suggestions. In general, though, I'd suggest transforming your data into a simple array of objects and then use the d3.nest utilities to extract the hierarchy from the data.

process csv/json data in java servlets and javascript

I need an opinion on how to approach my problem. I have no idea on how to start and on how to implement which functions on which parts of the software. So this is what I want to do:
I have a Java servlet which creates a simple csv file:
name1, value1
name2, value2
etc.
This needs to be somehow converted to JSON data, so it can be displayed on a jsp page:
[
{
"name": "name1",
"value": "value1"
},
{
"name": "name2",
"value": "value2"
}
]
Then the user will be redirected to the jsp page. Is it possible to send the whole JSON structure via request object to the jsp page? Or is it the easiest if all processing is done in javascript and only the path to the csv file is sent via request object?
I'm kind of lost on this, since I first started last week with programming of web applications. I'd just need a push in the right direction and then I should be able to figure out the rest on my own ;)
First, look for a CSV parser which can turn a CSV file into a List<Bean> or List<Map<K,V>>.
Then, look for a JSON parser which can turn a List<Bean> or List<Map<K,V>> into a JSON string.
Finally, just do the math and set the resulting JSON string as a request attribute which you print in JSP as if it's a JS variable, like so <script>var data = ${data};</script>.

Categories

Resources