Importing and exporting JSON information into Cytoscape.js

After coming across this interesting question and answer, I decided to create this JSFiddle to explore further.

My objective is to find an effective way to export and import JSON data into cytoscape.js.

The approach I took involved using JSON.stringify(cy.json()) to retrieve the JSON data from elements. On the other hand, I cleared the cy component and utilized cy.add(text-input) to reintroduce the elements.

For example, you can add a node, copy its generated JSON data, refresh the browser, and then try pasting the JSON data directly into the cy component. However, despite my efforts, I couldn't get it to work. I suspect the issue lies in my usage of the cy.add function, as I keep encountering these two errors:

An element must be of type 'nodes' or 'edges'; you specified 'undefined'

Uncaught TypeError: Cannot read property 'single' of undefined

Any insights or suggestions would be greatly appreciated.

Thank you in advance.

Answer №1

To utilize ele.json() or eles.jsons(), you can either build an array of element JSONs or iterate over the elements. When using cy.json(), keep in mind that it returns the entire graph init options JSON, which cannot be passed to cy.add() or similar methods.

Remember that when adding elements using cy.add(), you must pass the actual objects and not a JSON string.

For example:

cy.add( JSON.parse( jsonString ) )

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

Using NPM packages with vanilla JavaScript can be achieved without the need for an HTML file

Is there a way to include an NPM package in my index.js file without installing it via a package manager? I do not have an HTML file, only the index.js file which is executed with 'node index.js' command. Since I cannot use a CDN, is there any me ...

Is there a glitch in the Selenium Java CSS Selector functionality?

Everything seems to be working smoothly with that code! It successfully locates and clicks on my button within the span tag. driver.findElement(By.cssSelector("span[id$=somePagesCollection] a")).click(); However, after clicking the button, an input field ...

Encountered an error while attempting to convert react-native-reanimated-65-jsc.aar

ERROR: App installation failed after 19 seconds Failed to install the app. Ensure your Android development environment is properly set up. Follow this link for setup instructions: https://reactnative.dev/docs/environment-setup. Erro ...

The console is displaying the array, but it is not being rendered in HTML format in AngularJS

Can you please review my App.js file and let me know if there are any mistakes? I have provided the necessary files index.html and founditemtemplate.html below. Although it is returning an array of objects in the console, it is not displaying them as inten ...

What are the steps to customize and design your own unique password?

Looking to create a pass with Store Card style. I would like to add a 2nd row of data and change the label position so that it appears below the value. Currently, when I try to add a second item in the secondaryFields array, it inserts the item in the 2nd ...

Extract information from a web address to display on a web page

Can I link specific information from different URLs, such as the number of views on a YouTube video, to my website using jQuery or JavaScript? I attempted the following: <script> $(document).ready(function(){ $('#response').load(&apos ...

Refreshing Facebook comments does not occur when using FB.XFBML.parse()

My JavaScript photo gallery dynamically loads images from an API onto the page when users click on a thumbnail. I want users to be able to leave Facebook comments on each image, so I've included a Facebook comments element on the page containing the g ...

Guide to storing a collection in an object with Java

Before the changes were saved https://i.stack.imgur.com/hjpXa.jpg After the changes were saved https://i.stack.imgur.com/xABzN.jpg @Entity @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) public class Notification { @Id @GeneratedVa ...

Is it possible to modify an input using a specific code?

Is it possible to dynamically change the value of a form input based on specific codes entered in another input without using PHP? For example, when a user enters "HFS73H" into one input, the value of another input changes to "John" - and if a different ...

What is the most effective way to stop zooming when focusing on form fields on iOS or similar devices when using font sizes of 16px or lower?

It appears that many proposed solutions involve changing the text to 16px or using JavaScript to determine if the phone is an iPhone. However, altering the style may not be the most practical solution and checking for specific devices like iPhones could ...

The Next.js build failed due to the absence of the required "slug" parameter being passed as a string

I'm currently working on setting up a blog using Next.js and TypeScript, and I've encountered an issue with [slug].tsx. The error message I'm receiving is: Build error occurred Error: A required parameter (slug) was not provided as a strin ...

Steps for transferring data from one flatlist to another (an array of objects) within the same page featuring identical data:

const DATA = [ { car_name: 'Ford', model_number: '2021 . 68 - 1647', full_tank: false, suggestion: [ { price: '$2.50', type: 'Oil Top up&apos ...

The error is popping up as I try to deploy my Next.js application on Vercel

warning " > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d0f181c1e095009120d5011121c1914131a501f1c0f3d4f534c534d">[email protected]</a>" has an incorrect peer dependency "react@^16 || ^17" ...

Retrieving information through a loop in Next.js

My goal is to utilize the data retrieved from one endpoint to make a call to another endpoint, filtered by ID. I intend to fetch both calls using getServerSideProps and then pass the data to a different component. The initial call will result in an array ...

Jasmine test failing due to uninitialized angular controller

I encountered some difficulties while writing jasmine tests for an AngularJS application that utilizes angular ui-router. Despite proper initialization of my services and app in the test, I found that the controllers were not starting up correctly. In an e ...

What is the best way to pass a bind value in an Angular function controller?

I am new to working with AngularJS and I'm trying to pass a model bind value into a function declared in the controller. However, when I try to access that value from the controller, it returns undefined. Here is the code snippet: HTML: <div> ...

Exploring the capabilities of Typescript arrays by implementing a forEach loop in conjunction with the

I possess an array: set Array ( [0] => Array ( [name0] => J [name1] => L [name2] => C ) [1] => Array ( [data0] => 3,1,3 [data1] => 5,3 ...

What is the most effective method for transmitting a zip file as a response in Azure functions with node.js?

With the Azure function app, my goal is to download images from various URLs and store them in a specific folder. I then need to zip these images and send the zip file back as a response. I have successfully achieved this by following these steps: Send ...

Converting Table data to JSON in SQL Server by utilizing column values as nodes

Greetings, I am new to the JSON processing in SQL server. Here is a sample of my table: DECLARE @Example TABLE ( ThirdPartyInterfaceData VARCHAR(10) ,ThirdPartyInterfaceName VARCHAR(10) ,Name VARCHAR(512) ,Value VARCHAR(800) ) I ...

Using JavaScript to redirect to a different page while passing parameters

I've been experimenting with passing parameters from one ASP.NET page to another within our website by using JavaScript, jQuery, Ajax, Fetch, etc., and then capturing this parameter on the Load event of the redirected page using JavaScript. I'm u ...