Tips for structuring JSON data to retrieve numerous values

Creating a tool where users can enter their postcode to check for nearby windfarms is my current project. I have organized the data by named locations, and it's important to maintain that structure due to the specific API mapping tool I am using. Here is an example of the data:

Location Windfarm name
Copeland Sustainable Keswick
Crewe and Nantwich Nantwich Hydro
Derbyshire Dales Matlock Community Energy Project
Derbyshire Dales Derby Dales Community Energy

I've previously built a tool using keyed JSON data, but encountered limitations as there was only one value per location, like this:

"Copeland": {
      "hasWindfarm": "Yes",
}

This time, I aim to provide information about each individual windfarm in multiple locations. However, my usual setup is struggling with handling multiple locations and windfarms.

My query revolves around the most efficient way to organize and retrieve JSON data. As a novice in JSON and Javascript, any advice on best practices would be greatly appreciated.

Answer №1

If you're looking to organize information on windfarms in a specific region, one approach is to create an array listing all the windfarms in that area. For instance, you could structure it like this:

{"Copeland": {  
   "windfarms": ["windfarm1", "windfarm2", "windfarm3"]
}}

To retrieve data on windfarms in a particular location, you can implement the following steps:

const windfarmData = `{"Copeland": {"windfarms": ["windfarm1","windfarm2","windfarm3"]}}`;

// Convert JSON string to object
const parsedData = JSON.parse(windfarmData);

// Loop through each windfarm in Copeland and display their names

for (let i=0; i < parsedData.Copeland.windfarms.length; i++) {
   console.log(parsedData.Copeland.windfarms[i]);
}

This code snippet will output the names of all windfarms within the specified area.

Alternatively, if you wish to include more details about each windfarm, your JSON structure can be modified as follows:

const windfarmData = {"Copeland": {
   "windfarm1": {
       "employees": 300
   },
   "windfarm2": {
       "employees": 500
   },
   "windfarm3": {
       "employees": 100
   }
}}

After parsing the JSON data, you can access specific information by using the syntax:

parsedData.Copeland.windfarm1.employees
. This will yield the value 300.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display navigation bar upon touch or lift

In the mobile version of a website, there is a fixed navigation bar at the top. The positioning of this navbar is achieved using position:absolute: .navbar-fixed-top{position:absolute;right:0;left:0;z-index:1000; The goal is to make the navbar invisible ...

Is there a specific JSONSchema available for defining the structure of JSONSchema documents?

Has anyone come across a JSON schema for validating JSON schemas? I'm working on a project that involves verifying whether the user has submitted a correct JSON schema. Despite my extensive search efforts, I haven't been able to find an example. ...

Unable to access parameters from Pug template when using onclick event

I am facing an issue with my pug template test.pug. It contains a button that, when clicked, should call a function using parameters passed to the template from the rendering endpoint. Below is the structure of my template: doctype html html head tit ...

error in three.js occurs when attempting to load .obj files

I've been encountering issues with OBJ files that I've downloaded from the internet. All of these files render like this: View Obj File Pic I'm using the standard OBJLoader for rendering. var loader = new THREE.OBJLoader(); loader.load( ...

quickest method for retrieving a property by name, regardless of its location within the object hierarchy

I am looking for a solution to retrieve the value of a property from an object graph, and I am wondering if there is a more efficient alternative to the method I have outlined below. Instead of using recursion, I have chosen tree traversal because it cons ...

Nested React Components in React Router Components

class AppRoutes extends Component { render () { return ( <Suspense fallback={<Spinner/>}> <Switch> <Route exact path="/" component={ HomePage } /> <Route exact path="/acti ...

Just diving into JavaScript, what makes jQuery so powerful?

As a newcomer to javascript (although functional programming is no problem for me), I find myself questioning some of the design choices made by jQuery. Is it too late to make changes now, or are they simply too ingrained in the framework? For example, the ...

Kineticjs is not performing up to par

I apologize if this question has already been asked, but I haven't been able to locate it. I am working on a project that involves a canvas displaying approximately 400-500 rectangles, each ranging in height and width from 20-30 pixels. The goal is t ...

Mapping routes in ExpressJS

I am interested in developing a programmatic route generator. Within my project, I have a module called ./utils/crud.js structured as follows: const express = require('express'); const router = express.Router(); module.exports = function (Mode ...

Error: JSON at position 1 is throwing off the syntax in EXPRESS due to an unexpected token "

I'm currently utilizing a REST web service within Express and I am looking to retrieve an object that includes the specified hours. var express = require('express'); var router = express.Router(); /* GET home page. ...

Getting the selected value from a dropdown menu in ReactJS

I am working on creating a form that resembles the following structure: var BasicTaskForm = React.createClass({ getInitialState: function() { return { taskName: '', description: '', emp ...

Mastering the art of transforming JSON data for crafting an exquisite D3 area chart

I often find myself struggling with data manipulation before using D3 for existing models. My current challenge is figuring out the most efficient way to manipulate data in order to create a basic D3 area chart with a time-based x-axis. Initially, I have a ...

We encountered an error while trying to locate the 'socket.io' view in the views directory

Having an issue with my nodejs server. Check out the code below: server.js global.jQuery = global.$ = require('jquery'); var express = require('express'), path = require('path'), menu = require("./routes/menu"); var ...

Vue/Vuex - using async dispatch for AJAX requests in multiple components

I am working with vuex and using a store module to load user lists in my app through ajax. After the list is loaded, it doesn't fetch again if it's already present in the vuex store. I have implemented this logic in the main application layout as ...

Iterate through the Ionic3/Angular4 object using a loop

I've been attempting to cycle through some content. While I tried a few solutions from StackOverflow, none seem to be effective in my case. Here is the JSON data I am working with: { "-KmdNgomUUnfV8fkzne_":{ "name":"Abastecimento" }, ...

The external javascript file is unable to recognize the HTML table rows that are dynamically inserted through an AJAX request

I have a situation where I'm pulling data from an SQL database and integrating it into my existing HTML table row. Here's the code snippet: Using Ajax to fetch data upon clicking analyze_submit: $(document).ready(function(e) { $('#anal ...

Issue: The input must either be a single String containing 12 bytes or a string consisting of 24 hexadecimal characters. This problem appears in the context of MongoDB and node.js

Please add the description hereI am attempting to make a get request for each individual blog when they are clicked, however I am encountering an error. Below is the code snippet in question. <% blogs.forEach(blog => { %> <a href="/blo ...

Strategies for injecting data into VueJS components after the page has fully loaded

My project utilizes Vue.js and Nuxt.js, and within the settings page, users can modify their personal settings. This single page allows users to navigate between tabs. <template> <div class="account-wrapper"> // Code content he ...

Incorporating an external library into a Node.js virtual machine

I'm currently working on a nodejs library that enables users to write and execute their own JS code. Here is an example: var MyJournal = Yurnell.newJournal(); module.exports = function(deployer) { MyJournal.description = "my first description& ...

vue-router: Maintaining routing consistency despite argument changes

I am currently working on verifying the unsaved form route before leaving, and in case the user declines, I want to prevent changing the route. Most of it is functioning properly with beforeRouteLeave: function (to, from, next) { ...