Yihang Wang
Yihang Wang

Reputation: 112

Can I process a string which is like a json format using d3.json?

I've searched online while most people are directly reading the .json file from url or local folder like

 console.log("before csv ");
 d3.csv("cities.csv", function(data) {console.log(data)});
 console.log("before json");
 d3.json("flare.json",function(error,data2) {console.log(error, data2)});

while currently my requirement is that I have a string, which is in a json format, like

{"nodes": [
    {"id": "Myriel"},
    {"id": "Napoleon"},
    {"id": "Mlle.Baptistine"},
    {"id": "Mme.Magloire"},
    {"id": "CountessdeLo"},
    {"id": "Geborand"},
    {"id": "Champtercier"},
    {"id": "Cravatte"},
    {"id": "Count"},
    {"id": "OldMan"},
    {"id": "Labarre"},
    {"id": "Valjean"},
    {"id": "Marguerite"},
    {"id": "Mme.deR"}
]}

is there a method I could directly process this string as the data read from .json or .csv file?

Upvotes: 2

Views: 943

Answers (2)

Ipsit Gaur
Ipsit Gaur

Reputation: 2927

You can directly use JSON.parse function to parse the string to JSON without using d3 like this -

var json = JSON.parse(yourString);

Upvotes: 1

NullPointer
NullPointer

Reputation: 7368

D3 works with the JS objects only, d3.json is just a method to load the object from an external file. So if you dont need to load data from external file then dont use d3.json.

One of the way you can do like:

 var jsonObj = JSON.parse(data);

Upvotes: 3

Related Questions