Having trouble parsing an empty JSON array with Nashorn

I am currently utilizing Oracle JDK 1.8.0_65 with nashorn to execute some test cases, and I have come across a rather peculiar behavior when trying to parse an empty JSON Array.

Below is the script I am running in nashorn:

var testCase = {
    start:function() {
        // Case 1: a initialized from JavaScript Array
        var a = [];
        this.log.debug("a before:" + JSON.stringify(a) + " (length:" + a.length + ")");
        a.push(15);
        this.log.debug("a after:" + JSON.stringify(a) + " (length:" + a.length + ")");

        // Case 2: b initialized by parsing a JSON Array
        var b = JSON.parse("[]"); 
        this.log.debug("b before:" + JSON.stringify(b) + " (length:" + b.length + ")");
        b.push(15);
        this.log.debug("b after:" + JSON.stringify(b) + " (length:" + b.length + ")");
    }
};

and the resulting output is:

a before:[] (length:0)
a after:[15] (length:1)
b before:[] (length:0)
b after:[0,15] (length:2)

It appears to be a bug within the nashorn JSON parser. The returned Array does not genuinely seem to be empty, as there is a hidden "0" that emerges after the initial push operation.

I have been unable to locate any bug reports regarding this issue. Am I possibly misusing the JSON.parse method?

Thank you. J

Answer №1

Based on your correct usage, it seems like the bug has been identified and resolved. I just tested version 1.8.0_112 and can confirm that it is working as intended.

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 transfer a segment of CSS, HTML, and JavaScript code from one static template to another?

I have a collection of static files (html, css, and js) and I've identified a specific piece of code (let's say a dinosaur animation) that I want to move to another set of static files (a separate project I'm working on with a different temp ...

What is the process for changing colors once vertex colors have been updated?

Explaining the issue with an example. I have included a brief snippet of HTML code here to demonstrate the problem. In this scenario, I have created a simple triangle geometry as a global variable. By clicking the "Red" button, function red() is invoked ...

Generating table rows dynamically using functions

I am working on a table where I need to dynamically add or remove rows. Each row contains a hyperlink in the last column to delete the record. Sometimes, if the record is not found in the database, this can cause issues as new rows are added dynamically af ...

Getting a variable from an ajax call and using it in a Pug template

Currently, I am experimenting with Express and Pug to learn some new concepts. Upon making a request to a website, I received some dynamic information stored in an array. However, I am facing challenges when trying to iterate over this array using Pug. The ...

Is it possible to use async/await with the React setState method?

Seeking clarification on the use of async/await in React setState method. I previously believed it only functioned with Promises, but I have not found any definitive documentation supporting this. Any assistance would be greatly appreciated! In my applica ...

The reduce function is displaying an undefined result

Check out this code snippet: const filterByType = (target , ...element) => { return element.reduce((start, next) =>{ if(typeof next === target){ start.push(next) } } , []) } I'm trying to achieve a specific g ...

How to display information from a JSON file using dynamic routing in a React.js application

I'm currently working on a project to replicate Netflix using reactjs, but I've hit a roadblock and can't figure out what to do next. I've tried watching YouTube tutorials and reading articles online, but I haven't been able to fin ...

Struggling to fix TypeScript error related to Redux createSlice function

Here is the code snippet I am working on: import { Conversation } from "@/types/conversation"; import { PayloadAction, createSlice } from "@reduxjs/toolkit"; const initialState: Conversation | null = null; export const conversationSli ...

Press on any two table cells to select their content, highlight it, and save their values in variables

I have a table retrieved from a database that looks like this (the number of rows may vary): |Player 1|Player 2| ------------------- |Danny |Danny | |John |John | |Mary |Mary | My goal is to select one name from each Player column and sto ...

What is the distinction between selecting and entering a date input value?

When a user selects a date, it needs to be immediately sent to the server. If they manually type in the date, it should be sent on blur. The issue arises when the oninput event is triggered for each keydown event, causing unnecessary server requests while ...

Retrieve the ID values from the database and store them in an array variable called "checkbox". Then, insert all the values from the array variable

My question is regarding an input array checkbox that has id values. I'm uncertain if the array is functioning correctly. <?php $resource=mysql_query("Select * from material_rec",$con); ?> <?php while($result=mysql_fetch_array($resource) ...

Creating a List of Lists from a Json File - A Step-by-Step Guide

I am currently working with a JSON file that I am iterating over using gson. As I loop through the file, I store the data in lists. However, I am facing an issue when it comes to iterating over nested lists. Instead of having multiple inner lists inside th ...

Tips on sending the event object as the second parameter to a callBack function

I am looking to enhance a callback function by including the execution of event.stopPropagation() on the specific div element where it is called, alongside updating the state. QueryInput represents a custom input div element for adding text provided by the ...

Why is My JQuery Button Data Null?

I am facing an issue with a button where I want to pass the HTML object as a parameter to a JavaScript function. The objective is to print the data-hi attribute value from the element in the button. HTML BUTTON <button type = "button" onclick = "whoIs ...

How can you refer to the current element in TypeScript when using jQuery's .each method?

Consider the TypeScript snippet below: export class MyClass { myMethod() { // ... $myQuery.each(function(idx, elm) { $(this)... // Original javascript code which obviously not correct in typescript } } } However, i ...

Exploring Angular: Techniques for searching within nested arrays

I am looking for a function that can search and filter the largest value in a nested array, and then return the parent scope. Here is an example of my array: data = {"people": [{"male": [ {"name": "Bob" ,"age": "32"}, {"name":"Mike", "age ...

How can I launch five separate instances of Chrome on a Selenium grid, each with a different URL?

I have a list of URLs saved in a JSON file, structured like this: { "urls": [ "http://www.google.com/", "http://www.stackoverflow.com" ] } Currently, these URLs are being opened sequentially by the Selenium WebDriver JavaScript manager on a ...

Divide the table row and store each cell using regular expressions

Here is the original source text: gi0/1 1G-Fiber -- -- -- -- Down -- -- Access gi0/2 1G-Fiber -- -- -- -- Down -- -- gi0/3 1G-Fiber -- -- -- -- Down -- -- gi0/4 1G-Fiber -- -- -- -- Down -- -- gi0/5 1G-Fiber -- -- -- -- Down -- -- gi0/0/1 1G-Fiber -- ...

Having trouble getting jQuery css() function to work properly?

<a href="https://www.timeatthebar.co.uk" target="_blank"><img id="icon-img" src="assets/img/tabicon2.png" class="position-absolute top-50 start-50 translate-middle" style="max-width:113px; top ...

What is the most effective method for inputting a date/time into a Django view?

Looking to create a feature where users can see what events are happening at a specific time. What is the most efficient method to implement this request? For example, if I want to display all current events, should I submit a post request to /events/2009 ...