What steps can I take to make sure JSON keys containing only digits are treated as Strings instead of Numbers?

Within a JSON String, I have IDs as keys, represented as Strings of numbers, while the values are actual Numbers (Float). The problem arises when parsing this information to obtain an object in Safari 10.1.2...

var jsonData = "{\"53352\":0.6, \"82008\":0.4}";
var parsedData = JSON.parse(jsonData);
console.log(parsedData);

Upon parsing, the resulting object looks like:

{
  "0": NaN,
  "1": NaN,
  "2": NaN,
  "3": NaN,
  "4": NaN,
  "5": NaN,
  "6": NaN,
  "7": NaN,
  ...

However, the expected outcome should be:

(as observed in Firefox 55.0.3)

 Object {53352=0.6, 82008=0.4}

(as seen in Safari 11.0)

{
  "53352": 0.6,
  "82008": 0.4
}

The issue with Safari stems from its misinterpretation of the String keys as Number, leading to an array structure with indices up to 82008. This is unintended. How can I ensure Safari interprets the keys correctly as Strings?

Possible Solutions Attempted

One approach involves adding an underscore prefix to all keys on the server side, like _53352 instead of 53352. While this resolves the issue, it necessitates removing the underscore on the client side. However, this method feels unconventional and inconsistent since underscores are not added to IDs elsewhere. Are there other ways to guarantee proper interpretation as Strings?

Consider using org.json for encoding JSON data on the server side.

Answer №1

It has been identified as a known issue in Safari 10.x

Bug 170442 - Safari 10.1 JSON.parse returns incorrect object for numeric keys with decimal values

@evolutionxbox mentioned that the problem appears to be resolved in version 11.

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

How to shuffle the elements of an array saved in a JSON file using discord.js

I've been working on a Discord bot as a way to learn more about Javascript. While creating a command that pulls random quotes from an array stored in a separate JSON file, I encountered an issue that has me stumped. var config = require("./settings.j ...

struggling with utilizing ajax for outputting data using PHP and POST variables

Currently, I am attempting to execute a PHP script that retrieves data from a database by simply clicking a button. My intention is to implement AJAX in order to prevent the page from refreshing. While testing with traditional post/submit methods and enc ...

What is the best way to interpret a JSON with nested structure?

I can't seem to find any errors in the code below, but I keep receiving an error message stating "item._highlightResult.map is not a function". {items.map((item, index) => ( <li key={index}><a href={item.story_url} target="_blank& ...

Visualizing network graphs using JavaScript

I am in search of a JavaScript network visualization graph (not a chart) that can handle JSON input effectively. I have tried using the JIT infovis toolkit, RGraph, and space tree to display multiple levels in the graph. However, I have encountered issue ...

The JSON parsing encountered an error: Domain=NSCocoaErrorDomain Code=3840 "Failed to complete the operation. (Cocoa error 3840.)"

Currently, I am using the sendAsynchronousRequest request method to communicate with the server and retrieve data from it. Oddly enough, when I test the web service in a browser, everything seems to be functioning correctly and I receive the expected respo ...

Tips for hiding a sidebar by clicking away from it in JavaScript

My angular application for small devices has a working sidebar toggling feature, but I want the sidebar to close or hide when clicking anywhere on the page (i.e body). .component.html <nav class="sidebar sidebar-offcanvas active" id="sid ...

No duplication of Collada material present

I imported a collada model (.dae) into Three.js and encountered an issue with the object's material. Ideally, the material should appear as follows: However, it currently looks like this: The color is not an issue; I can modify the lighting within t ...

Ways to grant entry to a Vue.js page exclusively to authenticated users or when localStorage is present

My Vue.js application is set up with vue-router as shown below. index.html <p> <router-link to="/" exact> Home </router-link> <router-link to="/about"> About </router-link> <router-link to="/contact"> Contact < ...

Monitor changes in the visible price range in lightweight-chart

Is there a way to detect when the visible price range changes in lightweight-chart, similar to how the timeScale's visible time range change can be detected through subscribeVisibleTimeRangeChange? I couldn't find anything related in the document ...

What is the best way to use AJAX to send a downloadable file in WordPress?

Currently working on developing a WordPress plugin and could use some assistance ...

Is it possible to declare variables using the "this" keyword?

Consider the scenario where this.x=5 is declared and assess the accessibility of all relevant places. <script> $(document).ready(function(){ $("button").click(function(){ this.x=!this.x; $("#div1").fadeTo(400,this.x ? 0.4 : 1); }); }); & ...

How to pull specific nested dictionary values based on conditions using JINJA2 within Ansible for JSON data extraction

I am attempting to retrieve the CDN domain name associated with a specific Alias from Ansible's cloudfront_facts. The output, in summary form, looks like this: { "cdn_facts": { "ansible_facts": { "cloudfront": { "summary": { "di ...

Guide to adding directional arrow indicators on the Y and X axes of a ChartJS graph

Is it possible to add directional arrows on the Y and X axis of a ChartJS graph? I'm seeking advice or tips on how to achieve this. Below is the code snippet for creating the chart. var ctx = $("#mycanvas"); var LineGraph = new Chart(c ...

Each time my classes are initialized, their components undergo reinitialization

Apologies if the question is not well-formed, I am completely new to working with React. I have been attempting to create a dashboard but encountering issues with my states getting reinitialized. Below is the content of my app.js file. import './inde ...

Utilizing Prolog to process incoming Json posts

<pHello there, this is my very first question on stackoverflow so please be patient with me.</p> <pI am working on creating a simple Prolog API that can receive JSON posts and then send back another JSON post after processing. I came acr ...

I'm having trouble with res.redirect, why isn't it redirecting me as expected?

In my login controller, I have a form that updates the user's scope when they click a button triggering the login() function. .controller('loginCtrl', ['$scope','$http',function($scope,$http) { $scope.user = { ...

Canceling text as soon as the search input is entered in Vue.js

When I use the search input box, the last text I typed in gets cancelled. Can anyone help me with this issue? <input class="navbar-searchbar__text-field" type="search" :value="searchQuery" name=" ...

Obtaining a value from an input field in Vue.js

Just starting out with Vue and could use a hand extracting a value from an input field: Here's what I have in my form: <input type="hidden" id="groupId" value="1"> If I were using jQuery, I would do the following: ...

serving static content on subdomains with Express JS

I created an API using Node.js Express JS and utilized apidocjs.com to generate the static content. The issue I am currently facing is that I want the documentation file to be located under a subdomain such as docs.example.com or api.example.com/docs. What ...

Enhancing code branch coverage using Istanbul

The code snippet provided has a branch coverage of only 50% (refer to the coverage report below). I am unsure how to enhance this as there are no if statements present. I suspect that Istanbul must utilize some form of measurement that I have yet to grasp ...