What is the best method for retrieving a JSON string that contains commas?

I have a JSON data with coordinates:

"geometry":{"type":"Point","coordinates":[95.9174,3.8394,59]},"id":"us10002b0v"

I am looking to extract each value in the coordinates array that is comma separated. In PHP, I would use

extract(",",$geometry[coordinates]);
. Is there a way to achieve this in JavaScript?

The source JSON can be found here:

This snippet shows my current code:

google.maps.event.addDomListener(window, 'load', function() {
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: { lat: 7.8, lng: 98.3},
    zoom: 4,
    styles: mapStyle
  });

  map.data.setStyle(styleFeature);

    infowindow = new google.maps.InfoWindow({
        content: '<div class = "corp" style="width: 260px; height: 200px">' + '</div>'
    })


//InfoWindow
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(map,'click',function() {
        infowindow.close();
});
map.data.addListener('click', function(event) {
    var place = event.feature.getProperty('place');
    var mag = event.feature.getProperty('mag');
    var depth = event.feature.getProperty('geometry');//I need the depth from this line which is arrayed
    var link = event.feature.getProperty('url');
    var jsonTime = event.feature.getProperty('time');
    var humanTime = new Date('jsonTime');


    infowindow.setContent('<div><h3>'+place+'</h3><p>Mag: '+mag+'<br />Depth '+depth+'<br />Time : '+humanTime+'<br /><a href="'+link+'" target="_blank">More</a></p></div>');
    infowindow.setPosition(event.feature.getGeometry().get());
    infowindow.setOptions({pixelOffset: new google.maps.Size(0,-30)});
    infowindow.open(map);
});

Answer №1

Indeed, extracting array values is possible:

var data = '{"location":{"type":"Point","coordinates":[122.3321,37.7749,99]},"id":"example123"}';
var obj = JSON.parse(data);
var latitude = obj.location.coordinates[0];
var longitude = obj.location.coordinates[1];
console.log(latitude); //122.3321
console.log(longitude); //37.7749

Answer №2

One approach is to utilize

let data = JSON.parse("<your-json-string>");

By performing this action, your JSON will be converted into a JavaScript object that can be navigated in the typical manner.

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 discrepancy in performance between a mutable array in Objective-C and a variable array in C?

I'm working with a multidimensional array (3D matrix) that has an unknown size, where each element is of type short int. The size of the matrix can be estimated to be around 10 x 10 x 1,000,000. My current dilemma involves choosing between using Muta ...

Struggling to find a way to delay the query in Apollo's useQuery until the API request is complete to avoid returning an undefined variable when using useSWR

Having some trouble with my code that interacts with a user API using useSWR. When I log the user for the first two renders, it shows as undefined. The useQuery is throwing an error because user.id is undefined at one point during the render. I attempted t ...

Having trouble getting custom validation messages to function with jQuery

Within my form, I am dealing with the following input field: <input id="edit-submitted-tel" name="submitted[tel]" value="telefoonnummer (optioneel)" size="4" maxlength="15" class="form-text" type="text"> Although I have successfully implemented jQu ...

The validation of forms using ajax and jsp is not functioning

I've been working on developing a username validation system using JSP and AJAX, but I keep encountering an error that looks like this: POST ://localhost:8080/web_application/deneme.jsp Error Message: 405 (Method Not Allowed) This is my JSP page, c ...

Is it possible to incorporate multiple IDs into the changeImage function for a radio

I've encountered an issue with my function that changes images based on radio button selections. It works fine for one image slider, but now I want to make it available for a second slider. The problem is that when I use the radio buttons for the seco ...

Hiding the keypad on an Android device in an Ionic app when user input is detected

I am currently utilizing the syncfusion ej2 Calendar plugin for a datepicker, but I am only using options such as selecting ranges like today, 1 month, or last 7 days from the plugin itself. The plugin provides dropdown options when the calendar is trigger ...

Having trouble accessing information from a JSON file

Currently, I'm working on a project where I need to read data from a JSON file. However, I encountered an issue. The data I'm getting when I try to read it doesn't match the content of my file pliki.json. pliki.json: [ { & ...

JavaScript generated by PHP not functioning on IE version 7 and above

I've been experimenting with JavaScript generated from PHP, but I've run into issues specifically with Internet Explorer. While other browsers such as Firefox and Chrome have successfully processed and executed the JS code. As an example, you ca ...

Tone.js is failing to sync sequences during playback

I'm currently working on developing a sequencer using Tone.js. Right now, I have a basic sequence that plays when the user clicks the play button. However, I've encountered an issue where if the button is pressed to pause and then played again, t ...

Why does the Leaflet map appear inconsistently?

It's baffling that Firefox 24.0 and the latest Chrome are both failing to load my Leaflet map, but surprisingly, it works perfectly on Safari on my iPhone. Quite an interesting dilemma. This is my initial attempt at using Leaflet and Bootstrap...ever ...

Tips on implementing a script injected through the JS console to update form data in an Angular webpage and ensure that the changes are successfully saved

I am currently working on enhancing an existing electron app integrated with Angular. The main goal is to inject a script once the application is loaded in order to add a hotkey for efficiency. This hotkey should automatically click an edit button, input s ...

Creating a Python nested list with a touch of "numpification"

I'm struggling with my program's performance due to my lack of understanding on how to effectively utilize numpy arrays. Currently, my code may seem messy, but it does achieve the desired outcome (creating l k-by-80000 arrays with 2000 iterations ...

Extracting a precise value from JSON using PHP

Currently, I am attempting to extract the price data from the JSON provided below, but I am struggling to identify the correct reference for the actual values: {"cards": [ { "high": "0.73", "volume": 1, "percent_change": "-2.67", "name": "Lightning Bolt", ...

Issue with SmartAdmin Template Pagefunction Being Executed Twice

While developing a web application using the smartAdmin Template, an ajax-based template that you can preview here, I encountered some challenges. The issue I faced was that when I wrote a JavaScript function on one page, it affected all pages. For exampl ...

You can only use RSA and DSA public keys during the babel-node installation process

I'm having difficulties with the installation process of babel-node npm i -g babel-node > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53313231363f7e3d3c373613657d67d75716b70793f777b79">[email protected]< ...

Preventing Prepend Scroll: Tips and Tricks

Adding extra content to the top of the body can be frustrating, especially if it pushes everything down as you're trying to read. Is there a way to smoothly prepend this additional content without shifting the page layout, so that it seamlessly appear ...

The jq tool encounters issues with parsing invalid JSON, specifically with numeric values that are separated by spaces

It appears to be invalid as JSON format, however the jq command does not raise an error and is able to parse it successfully. What could be the reason for this behavior? $ echo '1 2 3' | jq . 1 2 3 (Version: jq-1.5) ...

Utilize jQuery to retrieve values from a Dropbox and Textbox and populate them inside a table

I'm attempting to retrieve values from a table row using the onfocus event. I have been assigned the task of inserting data into a database without clicking a button, so I decided to utilize the onfocus event for the last cell in the row. The challen ...

ERROR: No compatible version of jQuery Foundation could be located

Encountering issues while installing Foundation due to conflicts with Jquery. λ bower install foundation bower foundation#x cached https://github.com/zurb/bower-foundation.git#5.5.1 bower foundation#x validate 5.5.1 against https: ...

Using GSON to Encode and Decode JSON into String and Byte Array

My current process involves using hibernate to map objects to the database. The issue arises when a client, in this case an iOS app, sends me specific objects in JSON format. To convert these JSON objects to their actual representation, I leverage the util ...