D3.js - puzzling interpretation of a JSON file? - javascript

The JSON file which I am using is being interpreted differently than I would expect.
The external JSON file:
{"organizations":[{"member":{"source":"TCAN","target":"Resilient Toronto","category-source":"organization","category-target":"organization"}},{"member":{"source":"City of Toronto","target":"","category-source":"organization","category-target":""}},{"member":{"source":"Resilient Toronto","target":"City of Toronto","category-source":"organization","category-target":"organization"}},{"member":{"source":"Rita Bijons","target":"ZCO","category-source":"volunteer","category-target":"organization"}},{"member":{"source":"Rita Bijons","target":"Green 13","category-source":"volunteer","category-target":"community group"}},{"member":{"source":"Green 13","target":"TCAN","category-source":"community group","category-target":"organization"}},{"member":{"source":"ZCO","target":"TCAN","category-source":"organization","category-target":"organization"}}]}
I don't understand why the source field is being interpreted as an object while category-source is not. Is it just because of the name? and why is there a category field in the source object? And the name is undefined? Obviously I am missing some basic understanding. Is there somewhere this is documented?

I've just tested your JSON with PrettyPrint and it's formatted properly. So it appears that the problem is with the software/code you are using to display(format) the JSON data.

It looks like you are running your data through the force layout and it is conflicting with your property name "source". Specifically, the force.links call will change the data by adding "source" and "target" properties that look just like the structure you reference.
Check to see if you're passing your data into force.links at any point.

Related

Suitescript, nlapiRequestURL, can't turn a JSON from a URL into an object/array

In netsuite i'm using the nlapiRequestURL to retrieve a JSON data from flexport, an overseas shipping company. I have have the data as a string(to my knowledge retrieving json data makes it a string) and want to turn it into an array of objects, but everything I have tried has resulted in various errors.
trying...
`var output = nlapiRequestURL(url,null,headers,"GET");
var split = JSON.parse(output.getBody());
response.write(split);`
gave me
{records=[Ljava.lang.Object;#7220fad}
and trying to show any element of split gave me undefined or that it cant read element from index.
I've ran the string through a JSON checker and it said it was a valid JSON file. I've done various variations of JSON.parse and looked tried Tostring. I've been working on this for a while and have no idea why I can't parse this information properly. Any help is appreciated.
You have parsed the result but then you are writing the parsed object which just gets you the object’s implementation dependent toString() output.
If you are just trying to echo the response re-stringify the parsed payload.

How to read Python list in Javascript [in a Django template]

I'm programming in oTree (which is a Django based environment for social experiments) and I have the following problem. I defined some lists in Python and I'd like to import them and use them in an HTML template. If I print them in HTML I manage to see them without any problem, however, once I need to use them in Javascript, the program fails to read them and the single quotes of the elements of the list are converted in '.
The list is imported like this var filtered_elements = {{ array }};.
I think the problem is exactly here, as JS cannot work with them. Do you have any suggestion on how to do that? I considered using JSON, but since I'm quite new to programming, I cannot understand if it's just a waste of time or there is a simpler way out.
Thanks for your answers!
It sounds like your data is already JSON, otherwise you would be getting single quotes and u prefixes. So the only issue is Django autoescaping; you can disable it with the safe filter:
var filtered_elements = {{ array|safe }};
Your data should be JSON, instead of putting the Python list into the contact directly, put "array": json.dumps(array) in the context dictionary.
The JSON string doesn't need HTML escaping inside a tag, but it does need JS escaping! Otherwise some string may include something like </script><script>absolutely anything goes here... to run arbitrary JavaScript, if the JSON contains user data.
So use |escapejs:
var filtered_elements = {{ array|escapejs}};

what is data type of {'Content-Type:':'text/plain'} in response.writeHead() method?

The common way to creating web server in node.js is something like this:
response.writeHead(200,{'Content-Type':'text/plain'});,but what is data type of {'Content-Type:':'text/plain'} section? an Object? or a tuple?
I have never seen anything like that before in Javascript.
It is an anonymous object. Have you seen something simple like this:
{x: 10}
Well it is the same thing except it has a '-' in the key name so the key has to be put in quotes. The object is using specific naming that corresponds with the standards for HTTP headers (ref).
Text/plain is full text like .txt file.
In text/plain content, the HTML doesn't be parsed.
If you write "<b>Hi</b>", you will see the same thing ("<b>Hi</b>").

Parse Javascript To JSON Using Python 3

this is a very specific request, and for that i apologise, but i am at a loss for what to do..
for a javascript project i am working on i want to be able to parse javascript with python and i found this implementation`port of the original narcissus called pynarcissus:
https://github.com/jtolds/pynarcissus
the problem for me is the information is buried in the python class structure.. something with which i am only vaguely competent.. and i want the output to be in JSON
i have tried to mine the data out but each time the JSON is invalid
my question is how would you go about doing something like this? i'd appreciate any specifics because the project contains nested classes of disparate types creating what seems to be a wholly unique problem
here are my attempts:
i took the return value for parse() and created a function that descends through the class structure returning values based on their type: 'str', 'int', 'bool', 'NoneType', 'list', 'dict', 'main.Node', 'main.Tokenizer', 'main.Object'; but the returned object is missing some properties in the classes, ie 'type', while retaining others like 'type_', also tokenizer always contains the same values
i took the output of the str function that the program prints to stdout, removed the clear and copy functions and the tokenizer: [object Object], then tried to manually add in double quotes where necessary to make the output a valid JSON object.. a few problems here, first off ignoring the tokenizer object seems like i am missing out on vital information, and the other problem was that sometimes there is "value" : "{" and sometimes there is "value" : { .. }, after completing the work the JSON was invalid
assuming the issue lied in the "value" : { .. } issue i resolved to add a new function identical the the str function but instead of printing just %s values, it would print \"%s\" where necessary.. now, i could differentiate between "value": "{" and "value" : "{ .. }" but i would have to go through and manually remove the quotes around objects.. after doing so the JSON was invalid
i've tried every copy'pasta solution for python class to json from the stacks but the nested aspect of the classes along with the changing types add complexity.. some properties lack a .dict even when the type is "class 'dict'" so the one'method'fits'all lambda solutions fail
for posterity:
once i massaged the code of pynarcissus to print json i found that the process fails on nested functions.. hilariously exactly the reason i stepped away from my homebrew method
in another thread(i) #firstfire suggested esprima and pyesprima
so i looked into the esprima suggestions, and after a bit of 2to3`ing and some more work returning valid json i got it working and so far it fits my needs perfectly
check out the issue, pull request, and fork:
issues : https://github.com/int3/pyesprima/issues/1
pr : https://github.com/int3/pyesprima/pull/2
fork : https://github.com/ghissues/pyesprima
(i) http://www.linuxquestions.org/questi....php?p=5364199
the old answer::
closed the issue and added a pull request
https://github.com/jtolds/pynarcissus/issues/6
you can check out the fork if you got here by looking for a way to parse javascript to json in python 3
https://github.com/ghissues/pynarcissus

Exporting and importing JSON data to Cytoscape.js

Based on this question and answer, I've made this JSFiddle.
What I'm trying to do is to find a way to properly export / import JSON data to cytoscape.js.
I'm using JSON.stringify(cy.json()) to get JSON data from elements, and on the other hand I'm cleaning the cy area and using cy.add(text-input) to add the elements back.
I.e.: you can add a node, copy it's JSON data generated, then you can refresh the browser and paste the JSON data from node directly, tryng to add it to cy.
But I couldn't get this to work, and I can't really figure out where I'm wrong (probably using the cy.add function). Always geting both errors:
An element must be of type 'nodes' or 'edges'; you specified 'undefined'
Uncaught TypeError: Cannot read property 'single' of undefined
Any ideas?
Thanks in advance.
If you build from the source (or use 2.1 when released), you can use eles.jsons(), which gives an array of element JSONs. You're calling cy.json(), which gives the entire graph init options JSON -- which you can't pass to cy.add() or similar.
Alternatively to eles.jsons(), you can use the already existing ele.json() and build up an array yourself by iterating over the elements.
You also need to pass the objects to cy.add() etc. You can't pass a JSON string.
e.g.
cy.add( JSON.parse( jsonString ) )

Categories