Does the Apps Script parser JSON.parse() incorrectly remove leading zeros from string object keys?

I am facing an issue with my Apps Script Web App where JSON data is being manipulated when I try to parse it. Specifically, keys with leading zeros are being altered (e.g "0123" becomes "123") during the JSON.parse() function call. It seems like the function is interpreting my string as an integer but still treating it as a string. Can anyone confirm if this behavior is expected?

To demonstrate the problem, consider the following script:

function jsontest() {
  var json = '{"0123":"foo", "bar":"0123", "foobar":123}';
  var test = JSON.parse(json);
  var check = ("0123" in test);
  var check2 = ("123" in test);

  Logger.log(test);
  Logger.log(JSON.stringify(test));
  Logger.log(check);
  Logger.log(check2);
}

The result logged is as follows:

[17-06-07 15:22:56:241 CDT] {123=foo, bar=0123, foobar=123}
[17-06-07 15:22:56:241 CDT] {"123":"foo","bar":"0123","foobar":123}
[17-06-07 15:22:56:242 CDT] false
[17-06-07 15:22:56:243 CDT] true

Answer №1

When working with Google Apps Script, the JSON.parse() function has a behavior where it removes single and double quotations. Additionally, if the keys contain numbers, they are automatically converted to numbers. For example, "0123" is changed to the number 123. One way to avoid this conversion is by using the eval() function. When eval() is used, the quotations are removed without altering the strings.

While this approach may not be considered the best practice, it is one option to consider.

Sample :

var json = '{"0123":"foo", "bar":"0123", "foobar":123}';

var test1 = JSON.parse(json);
var test2 = eval("json=" + json);

Logger.log("%s, %s", test1, JSON.stringify(test1));
Logger.log("%s, %s", test2, JSON.stringify(test2));

Result :

{123=foo, bar=0123, foobar=123}, {"123":"foo","bar":"0123","foobar":123}
{bar=0123, foobar=123.0, 0123=foo}, {"0123":"foo","bar":"0123","foobar":123}

By incorporating the changes suggested in the sample script above, your modified script would look like this:

Modified script :

function jsontest() {
  var json = '{"0123":"foo", "bar":"0123", "foobar":123}';
  
  var test = eval("json=" + json); // <-- Added
//  var test = JSON.parse(json);
  
  var check = ("0123" in test);
  var check2 = ("123" in test);
  
  Logger.log(test);
  Logger.log(JSON.stringify(test));
  Logger.log(check);
  Logger.log(check2);
}

Result :

{bar=0123, foobar=123.0, 0123=foo}
{"0123":"foo","bar":"0123","foobar":123}
true
false

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

Having difficulty interpreting the json data retrieved from the specified url

<html> <body> <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script> <script> $(document).ready(function () { $.ajax({ type: 'GET& ...

The Facebook Comments feature on my React/Node.js app is only visible after the page is refreshed

I am struggling with getting the Facebook Comment widget to display in real-time on my React application. Currently, it only shows up when the page is refreshed, which is not ideal for user engagement. Is there a way to make it work through server-side r ...

Adding text with jQuery

Currently, I am utilizing the Jquery text editor plugin known as JqueryTE. While adding some functions to it, I have encountered a roadblock. Specifically, I am attempting to insert additional text into the source of the textarea but have been unsuccessfu ...

View real-time data in Vuejs 3 as it executes

I am currently working on a form that populates a table with data retrieved from a Laravel API. I am using Vue.js 3 and Composition API to build my entire application. When the button is clicked, I want the table to be filled with data from the form. The b ...

Converting data from a JSON-like file format into valid JSON using JavaScript

I have a unique situation where I am dealing with numerous files that have an unusual file extension. My goal is to utilize JavaScript to read these files and then convert their contents into either JSON or regular JavaScript objects. Is this task even fe ...

What is the best way to connect information from an HTML input field to a JavaScript object with the help of AngularJS?

As a beginner in AngularJS, I'm struggling to find the best approach to achieve my goal. I aim to create a grid of input tags with type=number in my HTML and have it set up so that whenever the value is increased, a new object is added to a list. Simi ...

What is the best way to transform api.Response into a data frame or json format using Python?

Looking at the data I have, my goal is to transform it into a data frame. t = cmc.globalmetrics_quotes_latest() (Cmc refers to the coinmarketcap api) type(t)= coinmarketcapapi.response """ RESPONSE: 820ms OK: {'active_cryptocurrencies ...

Struggling to grasp the syntax, trying to invoke a JavaScript function using an input button

I'm really struggling to grasp the xhtml syntax involved in calling a function with an input button. Despite my efforts to find a clear explanation, this concept still eludes me. Within the snippet of code from my book that I've been referencing ...

Please ensure there is space reserved for the header above the content

Currently, I am working on creating a webpage with a unique banner design. My goal is for the content to scroll from the bottom and remain fixed directly under the upper portion of the banner. Initially, my idea was to have the content box take up the siz ...

Dealing with an Undefined Property: Troubleshooting Guide

Encountering an error message Uncaught TypeError: Cannot read property 'top' of undefined at VM32939 own.js:819 resulting from var hands_corrected = (hands_original.top + 680) this issue arises because "hands_original" is not used in any par ...

The submission of the form with the ID "myForm" using document.getElementById("myForm").submit() is

<form name="formName" id="formName" action="" method="post" autocomplete="off"> <input type="hidden" name="text1" id="text1" value='0' /> <input type="button" name ="submit" onClick="Submit()" value="submit"> ...

Authenticating the Gmail API using a specified user ID

Is there a way to manually input email and password for authentication in the Gmail API without using the default Google popup? I need users to enter their login credentials directly, but I can't figure out how to do it. The code I am currently using ...

Analyzing and inserting elements into an array of objects

The following code aims to: 1) iterate through the two arrays, 2) if an item exists in both arrays, add its value to the value of the matching item in the first array, 3) if the item is found in arr2 but not in arr1, add the item to arr1. The code funct ...

Acquiring variables from a JQuery Ajax request while invoking a JavaScript file

I'm currently experimenting with using JQuery Ajax to retrieve a javascript file. I need to pass some parameters to it, but I'm not entirely sure how to do so in Javascript (PHP seems easier for this). Here is my current setup: function getDocum ...

Managing numerous invocations of an asynchronous function

I have an imported component that triggers a function every time the user interacts with it, such as pressing a button. Within this function, I need to fetch data asynchronously. I want the function calls to run asynchronously, meaning each call will wait ...

I am trying to figure out how to properly utilize server-only functions within Next.js middleware

In my current project, I am utilizing Next.js 13 along with the App Router feature. While attempting to include a server-specific fetch function in middleware.js, an error message is encountered: Error: Unable to import this module from a Client Compone ...

Derive Schema from deeply nested JSON string column using Pyspark

Given the table provided below: body {"Day":1,"vals":[{"id":"1", "val":"3"}, {"id":"2", "val":"4"}]} The objective is to define the schema in Pyspa ...

The design of Foundation's 4 form dropdown is not displaying correctly on Internet Explorer versions 8 and below

During the development of a website using Foundation 4 framework, I encountered an issue with form dropdowns not displaying correctly on Internet Explorer 8 and older versions. Instead of using Foundation's javascript rendering, they appear as the def ...

Unlocking the Secrets: Deserializing a Confidential Discriminated Union using Newtonsoft.Json in F#

Back in 2018, Onur Gumus asked a question on Stack Overflow regarding serialization and deserialization of a discriminated union with a private constructor. He utilized [JsonObject(MemberSerialization = MemberSerialization.Fields)] to achieve this function ...

A guide to decoding ElasticSearch JSON data using PHP

Although I've successfully parsed basic JSON files in the past, the structure of this specific one (from ElasticSearch) is proving to be quite perplexing. Here's a simplified example of the JSON data at hand: { "took": 7, "timed_out": false, ...