Showing JSON information in Cytoscape.js

I am currently working on a Java Web Application where I need to generate a graph programmatically and send it to Cytoscape.js (version 2.7.10) for display. In order to transport the data from my application to Cytoscape.js, I am using gson (version 2.8.1) to build a JSON Object. The formatted data that is sent to the JavaScript part looks like this:

{"elements":[{"nodes":[{"group":"nodes","data":{"id":"a","name":"Eins"}},{"group":"nodes","data":{"id":"b","name":"Zwei"}},{"group":"nodes","data":{"id":"c","name":"Drei"}},{"group":"nodes","data":{"id":"d","name":"Vier"}}]},{"edges":[{"group":"edges","data":{"id":"e","name":"1-2","source":"a","target":"b"}},{"group":"edges","data":{"id":"f","name":"2-3","source":"b","target":"c"}},{"group":"edges","data":{"id":"g","name":"2-4","source":"b","target":"d"}}]}]}

When setting up the cytoscape "window," the code structure is as follows:

return function(callbackuri, inputdata, divname, title, colorstr) {
    console.log("CytoScape Test ver:0.1.9");
    console.log("Data String sent to cyto * "+inputdata+" *");
    var cy = cytoscape({
        container : $('#cy'),
        elements: JSON.parse(inputdata),
        style: [
            {
                selector: 'node',
                style: {
                    'label': 'data(name)',
                    'width': '60px',
                    'height': '60px',
                    'color': 'blue',
                    'background-fit': 'contain',
                    'background-clip': 'none'
                }
            }
            , {
                selector: 'edge',
                style: {
                    'text-background-color': 'yellow',
                    'text-background-opacity': 0.4,
                    'width': '6px',
                    'target-arrow-shape': 'triangle',
                    'control-point-step-size': '140px'
                }
            }]
    });

    //
    cy.layout(concentric_options);
    cy.fit();
})

I have tried various methods to skip the setup via elements: JSON.parse(inputdata) but none of them seem to work as expected. Most times, I end up with a single large node that can be zoomed and panned around, which is not the desired outcome.

Interestingly, I am able to manually set up a graph by adding data after the initial setup and before the layout using the following function call:

var eles = cy.add([ {
    group : "nodes",
    data : {
        id : "n0",
        name : "A",
        weight : 75
    }
}, {
    group : "nodes",
    data : {
        id : "n1",
        name : "B",
        weight : 75

    }
},
….

If anyone has any ideas or suggestions, please feel free to share!

Cheers

Janko

Answer №1

The JSON you provided is not correctly formatted. Please refer to the specification here:

In valid JSON, the elements should be either an array or an object and not nested within each other.

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 trouble establishing a connection between the Android app and the Express server

I have a backend built with express and an Android app using Java. Background: My goal is to establish socket communication between the app and the server, but I am encountering difficulties connecting to the server from the app. The backend has been tho ...

Exploring the functionality of the onblur HTML attribute in conjunction with JavaScript's ability to trigger a

How do the HTML attribute onblur and jQuery event .trigger("blur") interact? Will both events be executed, with JavaScript this.trigger("blur") triggering first before the HTML attribute onblur, or will only one event fire? I am using ...

The Java/Android application is encountering an error that is preventing it from starting the activity ComponentInfo: java.lang.NullPointerException

I am excited to share my first app, designed to function as a TCP terminal with a HLK-RM04. While I acknowledge that this error has been discussed previously, I have yet to come across a solution that aligns with the issues in my code. Any assistance woul ...

Build an object using a deeply nested JSON structure

I am working with a JSON object received from my server in Angular and I want to create a custom object based on this data. { "showsHall": [ { "movies": [ "5b428ceb9d5b8e4228d14225", "5b428d229d5b8e4 ...

Retrieving objects at a lower level in JSON for Android

I am struggling with utilizing the JSONObject.getJSONObject(); method. I am having difficulty extracting data from a JSON Object that is stored as a string. JSONObject x = new JSONObject(); x.getJSONObject("name"); This object contains multiple layers. ...

Android file writing is not producing any output

Currently, I am developing a basic web browser for a school project and working on implementing a favorites feature. Below is the code snippet for the favorites class: public class Favorite extends Activity{ ...

Rails: Issue with form partial not refreshing correctly post AJAX submission

In my quest to develop a single page/view application, I am facing an issue with form submission. The form creates a basic tweet object: _form.html.erb <div id="schedule-tweet-form"> <%= simple_form_for @tweet, :remote => true do |f| %> ...

Utilize JSON information on a different JSP webpage

I'm encountering an issue with passing information between two JSP pages. I am unable to display the information in the second page. function sendSuccess (dir) { console.log(jdata); $.ajax({ url: dir, type: "POST", dat ...

Encountering the "Invalid JSON primitive" issue when making an AJAX request in MVC

Despite my efforts to resolve the "Invalid JSON primitive" issue by reading various references, I have not been able to find a solution. As a last resort, I am sharing my code in its simplest form and still encountering the error. See below for the minimal ...

Clicking on links in Internet Explorer using RemoteWebDriver is not functioning properly

Currently, I am employing the RemoteWebDriver to conduct UI tests on a distant machine using Internet Explorer: RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), DesiredCapabilities.internetExplorer()); driver.get("http ...

Just systems that have been positively identified

Greetings! I am currently in the process of creating a small online stock management system using PHP. However, my goal is to restrict access to this web app to only certain systems that I designated. I want to ensure that only the systems under my contro ...

Is jquery.validate showing errors more than once?

For my testing program, I utilize the jquery.validate plugin to validate user input fields. Here is how it's set up: <script src="js/jquery-1.12.4.min.js"></script> <script src="js/jquery-form-3.51.min.js"></script> <script ...

Error encountered during module build in Vue loader version 17.0.0 with Webpack version 5.74.0

I am encountering an issue while trying to integrate vue-loader into my SPA VUE APP. The error message I'm receiving is as follows: ERROR in ./app2.vue Module build failed (from ./node_modules/vue-loader/dist/index.js): TypeError: Cannot read prope ...

The backend response was "<Response [502]>" along with an error message stating that string indices must be integers

Currently, my goal is to initiate a REST request towards the specified end-point. import requests param1 = "abc" param2 = "def" input_data = """{{"param1": {}, "param2": {}}}""".format ...

Placing a div tag directly beneath another div tag

Imagine you have a div element with the class name divstudent, let's call this one div1. Now, you want to dynamically create another div element below div1, but outside of it using JavaScript. How can you achieve this? "div1" <div class="divstuden ...

Updating the contents of one specific div without affecting all other divs with the same class

Below is the HTML code snippet in question: <a class="btn test-btn test-btn-1"> <span>Content 1</span> </a> This particular code block appears multiple times on the page. The number at the end of test-btn- is dynamically generat ...

What is the process of manually registering filters in a Spring Boot application?

I have three Filter classes: AFilter, BFilter, and CFilter. I tried to register them using the code snippet below: @Component public class DefaultFilterRegister implements BeanDefinitionRegistryPostProcessor { // ...... private List<Filter> ...

Discovering the onClick event of an element in React using the DOM: A step-by-step guide

I have been tasked with implementing analytics events for almost every onclick event on our site. While I am new to analytics, the suggested approach was to create a standardized data set for each event and trigger the analytics event during the onclick a ...

The regex pattern checks for the presence of '<' followed by a space or

When using a multi-line textbox, I encountered the following string: index a<3 2<x I want to use regex to add a space after the character '<' if there isn't already one present. If there is already a space after the character &ap ...

Having an issue with both of my states being called simultaneously, resulting in a TypeError: Cannot read property 'map' of undefined

I am facing an issue with displaying both of my states on localhost (I am receiving the following error message: TypeError: Cannot read property 'map' of undefined). Skills.js import React, { Component } from 'react'; import ProgressBa ...