Navigating through nested objects in JSON when working with D3: a guide

Currently, I am in the process of learning D3 using various tutorials. Here is the code snippet I have been working with:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="//d3js.org/d3.v3.min.js"></script>
        <script>
            function draw(data)
            {
                "use strict";
                console.log("test");
                console.log(data);
                d3.select("body")
                        .append("ul")
                        .selectAll("li")
                            .data(data)
                            .enter()
                            .append("li")
                                .text(function (d) { return d.NAME + ": " + d.FACILITYID; });
            }
        </script>
    </head>
    <body>
        <script>
d3.json("http://maps.cityoftulsa.org/gis/rest/services/LGDM/Parks/FeatureServer/0/query?where=1%3D1&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&outFields=*&returnGeometry=true&maxAllowableOffset=&geometryPrecision=&outSR=&gdbVersion=&returnDistinctValues=false&returnIdsOnly=false&returnCountOnly=false&orderByFields=NAME&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&multipatchOption=&f=json",
                    function(error,json)
                    {
                        if(error) return console.warn(error);
                        data = json;
                        console.log(data);
                    }
            );
        </script>
    </body>
    </html>

The dataset corresponds to ESRI Feature Classes for Parks. Although each park contains a multitude of attributes, I am facing difficulties accessing them within my script. While I have successfully achieved this using regular Javascript on this link, adapting it to D3 has proven challenging. Since my experience with D3 is limited, specifically how to access fields like NAME or FACILITYID for each Park remains unclear. My current approach involves extensively using Console.Log statements to troubleshoot and understand the data structure better.

Answer №1

After posting my question, Stack Exchange recommended a link that led me to , sparking a new line of thought.

I decided to make some adjustments and the result was fantastic!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <title>Title</title>
    <script src="//d3js.org/d3.v3.min.js"></script>
    <script>
        function draw(data)
        {
            "use strict";
            console.log("test");
            console.log(data);
            d3.select("body")
                    .append("ul")
                    .selectAll("li")
                        .data(data)
                        .enter()
                        .append("li")
                            .text(function (d) { return d.attributes.NAME + ": " + d.attributes.FACILITYID; });
        }
    </script>
</head>
<body>
    <script>
        d3.json("http://maps.cityoftulsa.org/gis/rest/services/LGDM/Parks/FeatureServer/0/query?where=1%3D1&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&outFields=*&returnGeometry=true&maxAllowableOffset=&geometryPrecision=&outSR=&gdbVersion=&returnDistinctValues=false&returnIdsOnly=false&returnCountOnly=false&orderByFields=NAME&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&multipatchOption=&f=json",
                function(error,json)
                {
                    if(error) return console.warn(error);
                    data = json;
                    draw(data.features);
                }
        );
    </script>
</body>
</html>

The revised code worked perfectly. Now, I just need to fine-tune the draw function to complete this test run.

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

Is there a way to access specific methods within a JavaScript file?

Within my javascript assets folder, I have a file named jstester.js that looks like this: function hehe() { alert("wedsdsd"); } document.write("fdygsdfysdgf"); Then in the main index.html file in the public directory, I include the following code: & ...

Converting a byte slice to JSON in Golang: A step-by-step guide

Just starting out in Golang and struggling to convert a byte slice into valid JSON. Here's what I have so far: bytt := []byte({"tracks":{"track":[{"name":"Yellow","duration":"276","arti ...

Emphasize sections of text within a chart

Looking for a Specific Solution: I've encountered similar problems before, but this one has a unique twist. What I'm trying to achieve is to search for a substring within a table, highlight that substring, and hide all other rows (tr's) th ...

Arranging search findings based on relevance using javascript

I'm developing a personalized search feature. Currently, when I type "The R," the search results list starts with The Fellowship of the Ring because the phrase "the ring" is in its .text property. However, I want The Return of the King to appear first ...

How can I stop json_encode() from including the entire page in the JSON response when submitting a form to the same PHP script?

I only have a single index.php file in my project. I am aware that it's recommended to separate the logic from the view and use different files for PHP, JS, and HTML. This is just a test: <?php if($_SERVER["REQUEST_METHOD"] == "P ...

What type of loader is required to manage the output from these loaders?" - What specific loader should be used in this case?

I recently initialized a new react application using the create-react-app command. Upon attempting to utilize a react package by importing its default component, I encountered a compilation error: Compiled with problems: ERROR in ./node_modules/@USER-NAME ...

The setInterval() function is not functioning properly when used with PHP's file_get_contents

Currently, I'm encountering an issue while attempting to use the function get_file_contents() within a setInterval(). The objective is to continuously update some text that displays the contents of a file. Below is the code snippet: <script src="h ...

Trigger animation once you've scrolled past a designated point in the document

I created a count-up counter animation using JavaScript, but the issue is that the counter starts animating as soon as I refresh the page regardless of where I am on the page or if the counter is even visible. I would like the counter to only start workin ...

What are the steps to properly deserialize a JSON structure?

Here is the information in a specific format: {"UFs": [ { "Valor": "24.184,92", "Fecha": "2014-10-07" } ]} I am looking to access the value and date displayed above. In order to do so, I have created a class: Public Class clVal ...

"Utilizing the Image onLoad event in isomorphic/universal React: Activating event registration once the image has been

When a page is rendered isomorphically, the image can be downloaded before the main script.js file. This means that the image may already be loaded before the react register's the onLoad event, resulting in the event never being triggered. script.js ...

The concept of setting a value is not defined in JavaScript when compared to

Within my primary python script, the following code snippet is present. @app.route('/accounts/test/learn/medium') def medium(): word = random.choice(os.listdir("characters/")) return render_template('accounts/test/medium.html', word=w ...

A React-based frontend solution designed exclusively for managing data structures and databases

Challenges with Product Management In my React App, the shop features products sourced solely from a JSON file as an external API, all managed on the frontend. Recently, I encountered a product with limited availability - only 20 pieces in stock. I am uns ...

Retrieving all users in Sqlite database with a specific value

I am looking to identify and access each user who has a specific value in their row. Here is an example code snippet of what I want: sql.prepare("SELECT * FROM raid WHERE raid1 > 0 AND NOT id='685337576810610734'").get().forEach(async (user) ...

What is the best way to create TypeScript declarations for both commonjs modules and global variables?

Wanting to make my TypeScript project compatible with both the commonjs module system and globals without modules. I'm considering using webpack for bundling and publishing it into the global namespace, but running into issues with the definitions (.d ...

Challenge with inserting documents in Mongoose database

I have set up a schema and now I am trying to insert data into a MongoDB collection, but I keep getting an error stating that diagramModel.insert is not defined. Can anyone help me figure out what I did wrong? app.js mongoose.connect('mongodb://lo ...

Exploring the Positives and Negatives of Using JQuery and Glow JavaScript Libraries

Is there a detailed analysis available that compares JQuery with the BBC's Glow JavaScript libraries? ...

There seems to be an issue with the Link component from react-router-dom when used in conjunction with

I am currently utilizing react-router-dom along with Material-ui My main objective is to create a clickable row in a table that will lead to a specific path. Here is the code snippet: .map(client => ( <TableRow key={client.id} component={Link} to ...

Tips for interpreting the JSON data returned by a Node server in Angular

When trying to implement a login form in my Angular frontend, I encountered an issue with reading the JSON object returned from my node.js server. Despite following the correct steps, the console displays "undefined" as if it cannot recognize the format. ...

Sending images as a base64 string from a Titanium app to a Ruby on Rails web service

I am encountering an issue when trying to upload an image from an app that has been converted into a base64 string to a Ruby on Rails server. The app is developed using Titanium. However, after retrieving and decoding the image string back into an image, ...