Convert the JSON/YAML data to a correct format in JavaScript

I am in search of a solution to get Google Maps suggestions. Unfortunately, the official geocoding API does not provide real suggestions, only geocoding.

Currently, there are two versions available:

http://maps.google.de/maps/suggest?q=test&cp=1&hl=de&gl=de&v=2&json=b
(YAML)
http://maps.google.de/maps/suggest?q=test&cp=1&hl=de&gl=de&v=2&json=a
(invalid JSON)

This makes it difficult for json.parse to work properly.

I am looking for suggestions on how to deal with this data - should we try to parse it or fix it before parsing?

Answer №1

Neither of these examples are valid JSON format, however they are both valid in javascript. One way to parse them is by utilizing the eval function, although this approach can introduce security vulnerabilities. For instance:

eval("let x =" + data);

Answer №2

Although <code>JSON.parse() is unable to process the data retrieved from either URL, it is interesting that eval() can successfully handle both sources of data.

Here are some cities and countries as interpreted data:

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

What is the best way to retrieve location data in a React application?

In my React project, I am trying to retrieve location information using the Geolocation API. To accomplish this task, I have included a button in the code below: import React, { Component } from 'react' class App extends Component { construc ...

What is the process for customizing the marker icon on a leaflet map using NUXT.js?

I'm currently in the process of changing the marker icon for individual markers on my OpenStreetMap. mapIconsReinit(L) { delete L.Icon.Default.prototype._getIconUrl; L.Icon.Default.imagePath = '' L.Icon.Default.mergeOptions({ ...

If you utilize a span element with the attribute role="textbox" and contenteditable set to true, the copying and pasting of text within it may not function correctly

Consider the following span: <span class="comment-box2" role="textbox" contentEditable=true></span> Using the following CSS: .comment-box2 { display: block; width: 100%; overflow: hidden; outline: none; ...

The error message states that the property '$refs' cannot be found on the void type

Attempting to automatically focus on the next input field after typing 1 character. Encountering error: The property '$refs' does not exist on type 'void'. Here is the code snippet: setup() { const autoFocusNextInput = (event, max: ...

Monitor $localStorage for any modifications

Utilizing the ngStorage module, which can be accessed at this link: https://github.com/gsklee/ngStorage In order to maintain a lastSeenId for a real-time social feed in my hybrid app (Laravel/AngularJS), I am storing it in localStorage instead of $scope. ...

Issue with onresize event not triggering when checking window width

After successfully attaching the onresize listener to the body tag, I encountered an issue when I modified my code to access the window.innerWidth and window.innerHeight properties. Strangely, my resize event only fires once. <script type="text/javas ...

While the file does exist in the current directory, the `readFileSync` function is unable to locate

For my latest SvelteKit project, I was developing a Joke API using Bun as the runtime and TypeScript as the programming language. The goal of this API was to fetch one random dad joke from a text file that contained 363 jokes. Everything was going smoothly ...

When the JavaScript string retrieved from the database is null, it will be displayed as an empty string

Currently, my Ajax setup involves querying a database on the server with SELECT someColumn FROM someTable. The returned value of someColumn is then updated on the client side by using $('#someElement').text(someColumn); Everything works perfectl ...

Ways to troubleshoot and resolve the jQuery error with the message "TypeError: 'click' called"

I am currently developing a project for managing Minecraft servers, focusing on a configuration panel. I have set up a form that users need to fill out in order to configure the settings and send the values using Ajax. However, I encountered an error: Type ...

The only information returned from calling mongoose's Model.save() method are the fields { _id, __

I've encountered a problem even though I believe I'm following all the right steps. I'm attempting to save an item from a form into my mongodb collection using mongoose. This is what my schema looks like: // stationmodel.js export const Sta ...

Tips for transferring objects between AngularJS directives

Is it possible to access an object named foo that is defined/instantiated inside the link function of directive A from the link function of another separate directive? ...

Getting the full referrer URL can be achieved by using various methods depending

I am currently working with ASP.Net. My goal is to retrieve the complete referrer URL when visitors arrive at my website from: https://www.google.co.il/webhp?sourceid=chrome-instant&rlz=1C1CHEU_iwIL457IL457&ion=1&espv=2&ie=UTF-8#q=%D7%90 ...

Modifying a Nested Component with react-icons

As I work on creating a rating component that utilizes react-icons to display icons, I have encountered an interesting challenge. Currently, I am using the FaStarhalf icon which represents a pre-filled half star that can be flipped to give the appearance o ...

Implementing a separate detail view in Vuejs

I am currently working on a page with multiple cases. When I click on a case, the details for that case appear on the same page. Here is a screenshot of how it looks: https://i.sstatic.net/Y9S4J.png As you can see in the screenshot, there are two cases li ...

Mastering the art of scrolling to a specific element with angular-scroll

Currently implementing Angular Scroll and when reaching a specific page, I trigger a function to scroll to an ID. Encountering the following error in the code snippet below: TypeError: $document.scrollToElement is not a function initHelp(); function ...

Put a hyphen in front of the final three characters

I am looking for a way to automatically insert a dash before the last three characters in a text field, using either JavaScript or PHP. For example, if a user enters '1dsb3rs', I would like the input to change to '1dsb-3rs'. Whether the ...

What is the most efficient way to move queried information from one table to another while maintaining all data entries?

Currently, I am working on a small POS project. In this project, I have two tables. The main purpose of the first table is to retrieve products from a search box with two column names retrieved from the database. If the products are found, I want to be abl ...

What is the best approach for accessing values from dynamic or multiple form fields upon submission in React?

In my form, users have the ability to add Textfields in order to include additional items. However, I am facing a challenge when it comes to retrieving these items from the form upon submission. The Textfields are dynamically created by a component functi ...

Trouble with using AJAX to transfer a JavaScript variable to a PHP file

I need assistance in sending a JavaScript variable to a PHP file for displaying comments on a webpage. While I have successfully sent the JS variable to other PHP files, I'm facing issues when trying to do the same with the comment-list.php file. It ...

Using JQuery to load and process a file efficiently

What is the most effective method in terms of performance to load and parse a log file, and then highlight all occurrences of the word "error" in HTML format? <div class="mydiv"></div> $.get(path, function(data) { var lines = $(data. ...