Parse the JSON file in the local directory and extract specific keys for each object, storing them in an array

Seeking to retrieve data from a local JSON file that contains nodeIDs with longitude, latitude, and adjacent nodeIDs as values.

For example:

[{"33583379": {"lat": 40.7046387, "lon": -74.0167729, "adj": ["33583379", "1659428533"]}, "33583380": {"lat": 40.7043271, "lon": -74.0169624, "adj": ["33583379", "33583381"]}, etc.}]

The goal is to extract the Object name, "lat", and "long" values of each object and store them in arrays like this:

[33583379, -73.984293,40.729468],
  [33583380, -73.987752,40.732017]

Attempted to use Object.keys(data[0]) but it retrieves all object names, and finding a way to fetch them one by one in the same loop has been a challenge.

Any assistance is welcome. Have been struggling for hours to extract the necessary key values from the JSON file and format them into an array. PS: Working on a project in React.

Answer №1

To retrieve data from a JSON file, you can use the following code snippet: require('location-of-json'). Extract the latitude and longitude details using a for-loop and store them in an array.

a=require('./test.json')
data=[]

for(var i in this.a[0]){
  console.log(new Array(this.a[0][i].lat,this.a[0][i].lon))
  this.data.push(new Array(this.a[0][i].lat,this.a[0][i].lon))
}

This implementation is done using React. Feel free to check out the code here: https://stackblitz.com/edit/react-nphzxn

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

Remove Vue Component from the styling of current application

We integrated a Vue component (using Vuetify) into our existing .NET MVC application. The issue we are facing is that the Vue component is inheriting all the CSS styles from the rest of the application. Here's a simplified version of the HTML structur ...

Calculate the pixel distance between various divs by measuring the horizontal distance from the left border to the right

I am trying to find the precise distance between : #main div left edge to the first div with class="b" between the first div with class="b" to the second div with class="b" It's worth noting that the divs may be arranged randomly and could have fix ...

Prevent any clicking on the page for a brief period of time following the execution of a function

After clicking a div, an image appears and the user can click the image to hide it. To prevent the user from accidentally closing the image while waiting for it to appear, I want to disable all clicks on the page for a few seconds after the div is clicked. ...

Encountering a TypeScript type error when returning a promise from a function

I currently have a scenario in which there is a function that checks if user whitelisting is required. If not, it calls the allowUserToLogin function. If yes, it then checks if a specific user is whitelisted. If the user is not whitelisted, an error is thr ...

Daniel Opitz explores the best placement for DataTables within the slim4 framework

After purchasing Daniel Opitz's eBooks, I found myself on page 226 trying to implement data tables in my project. The books mention: DataTables Setup DataTables.net is a very flexible table plug-in for jQuery. You have to setup jQuery for Webpack firs ...

Dynamic sliding box jumps instead of simply fading in/out

My app features both a navigation bar and a sub-navigation bar. Within the sub-navigation bar, users can click on a button that triggers the appearance of another sub-bar while hiding the original one. The new sub-bar should smoothly slide out from behind ...

Upon upgrading kombu, an issue has arisen where the task_id is not JSON serializable

After upgrading, I encountered an ERROR EncodeError(TypeError('6JQAKHNMG9 is not JSON serializable',),). The following packages were successfully installed: amqp-2.1.4 billiard-3.5.0.2 celery-4.0.2 kombu-4.0.2 pytz-2017.2 (These were i ...

Enhancing Efficiency with Laravel 5: Updating Multiple Data Entries

My Simple Todo App lacks the ability to persist multiple record updates simultaneously. The client-side version can be found here: http://codepen.io/anon/pen/bVVpyN Whenever I perform an action, I send an HTTP request to my Laravel API for data persistenc ...

Form submission is not functioning as expected

I am having trouble with my form submission. When I try to submit the form, nothing happens. I have tried various solutions but have not been able to resolve the issue. I have reviewed all available resources but still cannot figure out why my code is not ...

I'm looking to add autocomplete functionality to a text input in my project, and then retrieve and display data from a MySQL database using

Looking to enhance user experience on my form where users can select inputs. Specifically, I want to implement a feature where as the user starts typing in a text input field with data from a MYSQL database, suggestions will be displayed. The form is locat ...

Use vtip jQuery for showcasing titles

Currently, I am utilizing the vTip plugin for displaying titles upon hover. Here is a snippet of my code: http://jsfiddle.net/bnjSs/ In my HTML, there's a Div with the ID #showTitles. Whenever this Div is clicked, I aim to toggle the display of all ...

What is the best way to include the API body in a GET request?

I'm facing an issue with passing parameters to the body instead of the query in my code. Here's what I have attempted: const fetchData = async () => { let response = await apiCall("URL" + { "companyArr": ["SBI Life Insurance C ...

validating if Object may be either 'null' or 'undefined'

In the code snippet below, I am attempting to verify whether hostel.country.address is null: return hostel.country.address && hostel.country.address.internalEmployeeIdentifier !== null || hostel.country.address.exter ...

What is a method for saving a file from Chrome to a network drive using scripting language, even though it's currently functioning in IE?

In a SharePoint 2013 document library, I have implemented a JavaScript function that captures user access to files. The function creates a .txt file containing information about the user ID, username, and file path, which is then saved to a network drive l ...

What is the best way to transform JSON into a Swift Dictionary so that it can be used for an HTTP POST

I'm encountering difficulties in organizing my JSON data into a Swift dictionary to send as the HTTP body through an API. This is the structure of my JSON: { "idNo": "string", "name": "string", "isRe ...

The "ID" value for the row that was chosen is unable to be parsed with AJAX JQUERY

After populating an HTML table with data retrieved from a database using PHP, I encountered an issue while trying to fetch the correct ID using AJAX. Following a recommendation to use classes to build my jQuery code, I find myself stuck and unsure of what ...

Unable to render Google map on Vue CLI component

I am currently using the google map api to develop a basic application with vue.js. Interestingly, when I utilize a simple html and javascript setup with the api key, everything runs smoothly. However, once I transition the same process to vue, the map fai ...

Illuminate objects using three.js

My expertise in shaders and three.js is limited, however I am currently experimenting with creating a dynamic glowing effect similar to lights being flicked on and off. Currently, I am adjusting the color saturation and lightness which somewhat achieves ...

Angular ng-if directive contains a specific class

I am trying to create a directive that will only be active when an element has a specific class. I have tried the following code: <feedback-analytics ng-if="$('#analyticTab').hasClass('active')"></feedback-analytics> Unfor ...

Error message encountered in React Material-UI while attempting to run the basic Hello World test - Invalid hook call

My plan is to kick off a project using React and Material-UI. I decided to start with the simplest Hello World example based on the official documentation, in a brand new Ubuntu OS. However, when I tried running the code, an error popped up! Here's wh ...