0

I've been recently trying a develop a simple application, which would use user input, convert it to json and use that json in another function to draw graph. I managed to output correct json to console, but when I try to call it as a parameter in the function which uses it to draw graph, it fails to work. If I manually copy console output directly to the plotting function it works normally. So the question here is, how does one use Json as a function parameter.

graph obj:

var graph = {
        "nodes": [],
        "edges": []
    }

..some code which fills graph (irrelevant here).. ending with a function call:

console.log(JSON.stringify(graph, null, '\t')); //works perfectly if manually copied
        var inpt = JSON.stringify(graph, null, '\t');
        execute(inpt); //doesn't work

And the execute() with the json line:

execute(input)
elements: input,
...some more code...
1
  • Why is it that you just not pass the graph object to the execute function? Commented Aug 22, 2015 at 14:45

1 Answer 1

1

Don't pass the JSON string. Instead pass the graph object directly. The JSON (JavaScript Object Notation) format is native for JavaScript. The JSON string representation is used to transfer the object to environments that does not support it directly, for example consuming a JSON object in a service written in C# or Java.

When an object in JavaScript var o = { field: "value" }; is passed as an argument to a function function f(p) {...}, then in the function the field can be accessed directly p.field....

When the object is needed outside the JavaScript scope, then it should be serialized to a JSON string { "field": "value" }. The recepient can retrieve the field and the value from the string following the JSON construction convention. JSON.stringify is used for object serialization.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. It was obviously a very noobish mistake, this works perfectly.
Glad that the answer helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.