Basically, I have a really large JSON file I need to parse, and while searching, I came across this answer.
The only problem is I don't know how to format my JSON array into a single object per line. Is there a straightforward Javascript/Ubuntu way to do this? (I've used jq in the past and it's pretty good for minifying json files, for example)
My JSON file looks something like this
[
{
"country":"monrovia",
"street" :"grove street",
"where" : "home"
},
{
"country": "uk",
"street": "diagon alley",
"where": "mystery"
},
{
...
}
]
But I need it to look like this
[{"country":"monrovia", "street": "grove street", "where": "home" },
{"country": "uk", "street": "diagon alley", "where": "mystery happens"},
{...}]
What you can do is parse the json array by using the JSON.stringify Method like so
// This can be the array of json
var obj = {
"name": "John Doe",
"age": 29,
"location": "Denver Colorado",
};
// stringify the json
var result = JSON.stringify(obj);
// see the output
console.log(result);
jq to the rescue once again! Here is what I needed.
And it's apparently referred to as JSONL.
An even better option is 'new-line delimited JSON' (ndjson). The Javascript implementation of the same (with streams!) is here
Related
I'd like to use a csv file instead of a json file in this example:
https://bl.ocks.org/mbostock/4339083
Any idea for loading a csv instead of a json file?
The problem here is way more complicated than it seems, and I suggest you that you leave the data file as JSON. The reason is this: the JSON file in your question contains nested data.
Here is an explanation:
Apparently, the only difference between loading a CSV file and loading a JSON file is the request function:
d3.json("data.json", function(data){
//code here
)}
... for a JSON and:
d3.csv("data.csv", function(data){
//code here
)}
... for a CSV.
But there is more. Besides the fact that d3.csv accepts an accessor (row) function and d3.json does not, d3.json loads the data as it is. On the other hand, d3.csv parses the file according to the columns, creating an array of objects.
Thus, if you have this CSV:
city,population,area
New York,3400,210
Melbourne,1200,350
Tokyo,5200,125
Paris,800,70
... it will be parsed to this array:
[{
"city": "New York",
"population": "3400",
"area": "210"
}, {
"city": "Melbourne",
"population": "1200",
"area": "350"
}, {
"city": "Tokyo",
"population": "5200",
"area": "125"
}, {
"city": "Paris",
"population": "800",
"area": "70"
}]
And here comes the problem: As you can see, there is no nested data in the array created by d3.csv. All the objects are side by side in the array.
However, the data object in Bostock's code you linked is way different:
{
"name": "flare",
"children": [
{
"name": "analytics",
"children": [
{
"name": "cluster",
"children": [
{"name": "AgglomerativeCluster", "size": 3938},
{"name": "CommunityStructure", "size": 3812},
//...
As you can see, you have arrays inside objects inside arrays inside objects...
So, to recreate the nested JSON in your question, you'll have to create an additional column, specifying who is parent of who and who is child of who:
name,value,parentOf
foo,42,bar
bar,53,baz
...
Then, after parsing this CSV, you'll have to stratify it, using stratify():
var nestedData = d3.stratify()
.id(function(d) { return d.name; })
.parentId(function(d) { return d.parentOf; })
(data);
As you can see, those are complicated steps.
Therefore, as a general rule: if you have nested data as a JSON file, just use d3.json, which loads the data as it is.
You can use d3.csv() instead of d3.json(). See D3's documentation.
I have this json file (data.json):
{
"country":[{
"Russia":[
"Voronezh",
"Moscow",
"Vorkuta"
],
"United Kingdom":[
"London"
]
}],
"countryCodes":[
"ru",
"uk"
]
}
and such code:
$.getJSON('data.json', function success(data){
alert(data.country[0]);
});
this returned "undefined". But i want get "Russia", and having indexes object Russia, i want get "Voronezh", don't use "data.country.Russia".
Sorry for my English.
If I were you, I would restructure the JSON to look something like this:
var data = {
"countries": [
{
"name": "Russia",
"cities": [
"Voronezh",
"Moscow",
"Vorkuta"
]
},
{
"name": "United Kingdom",
"cities": [
"London"
]
}
],
"countryCodes": [
"ru",
"uk"
]
}
data.countries[0].name; //Russia
data.countries[0].cities[0]; //Voronezh
Adding to ArgOn's answer where he has used Dot notation, there is one more way to get the answer i.e. by using Square bracket notation
var option = "countries"; (assign value to a variable)
data[option][0]["name"]; //Russia
data[option][0]["cities"][0]; //Voronezh
Dot notation is faster to write and clearer to read.
Square bracket notation allows access to properties containing special characters and selection of properties using variables.
If I receive data from a server in plain JSON that looks like this:
{
"f223dc3c-946f-4da3-8e77-e8c1fe4d241b": {
"name": "Dave",
"age": 16,
"jobs": [{
"description": "Sweep the floor",
"difficulty": 4
},{
"description": "Iron the washing",
"difficulty": 6
}]
},
"84af889a-8fc9-499b-a6ea-97e7a483130c": {
...
}
}
Do I need to loop through all the jobs and convert them to Maps, then convert each object's jobs into a List, then the entire thing into a Map?
Or does ImmutableJS do this all recursively for me?
There is Immutable.fromJS() designed for exactly that.
Hi i have a return json data which returns the webservice
The structure of webservice is like that:
jsonp1332655154667({"products": [{"uid": "37",
"samsid": "hjk",
"name": "Science%20Essentials%2010%20AC%20edn",
"shortname": "scienceessentials10",
"description": "Science%20Essentials%2010%20ACE%20is%20the%20fourth%20in%20a%20series%20of%20four%20books%20designed%20for%20the%20National%20Curriculum.%20",
"generated": "3/25/2012%205:59:19%20AM",
"Description": "Science%20Essentials%2010%20ACE%20is%20the%20fourth%20in%20a%20series%20of%20four%20books%20designed%20for%20the%20National%20Curriculum.%20",
"PublishingCompany": "Macmillan%20Australia",
"Service": "OneStopScience",
"Service": "OneStopDigital",
"Icon": "http://curriculumplatform.s3.amazonaws.com/prod/icons/brain48.png",
"Country": "Australia",
"Shortname": "scienceessentials10",
"MarketingSite": "http%3a%2f%2fwww.macmillan.com.au%2fsecondary%2fonix%2fall%2f6F597241EFC0E43DCA257791001CAFC0%3fopen%26div%3dSecondary%26cat%3dScience%253EAustralian%252BCurriculum%26template%3ddomSecondary%26ed%3dsite%2fseced31.nsf",
"Skin": "OneStopScience%20Green"},
"tag":"s_science"'
"tag":"s_maths"'
"tag":"s_arts",
{"uid": "5",}]})
I have three "tag" elements. but when we access the products.tag it gives always last element like:s_arts.
Is there any way to find out all the elements eg:s_science,s_maths,s_arts.
please help.
It is invalid json, your tag should be:
...,
"tag": ["s_science", "s_maths", "s_arts" ],
...
Then product.tag would be an array that you could access successfully
Regards
If you have multiple keys in the same object, you're going to get undefined behaviour. Only one will be preserved, and since pairs are not ordered, you can't guarantee which you'll get.
In short: the webservice is returning you faulty data. If multiple tags are expected, the service should return an array of values in the tag attribute:
...
"tag":["s_science", "s_maths", "s_arts"],
...
You need to send the tags as an array:
jsonp1332655154667({"products": [{"uid": "37",
"samsid": "hjk",
"name": "Science%20Essentials%2010%20AC%20edn",
"shortname": "scienceessentials10",
"description": "Science%20Essentials%2010%20ACE%20is%20the%20fourth%20in%20a%20series%20of%20four%20books%20designed%20for%20the%20National%20Curriculum.%20",
"generated": "3/25/2012%205:59:19%20AM",
"Description": "Science%20Essentials%2010%20ACE%20is%20the%20fourth%20in%20a%20series%20of%20four%20books%20designed%20for%20the%20National%20Curriculum.%20",
"PublishingCompany": "Macmillan%20Australia",
"Service": "OneStopScience",
"Service": "OneStopDigital",
"Icon": "http://curriculumplatform.s3.amazonaws.com/prod/icons/brain48.png",
"Country": "Australia",
"Shortname": "scienceessentials10",
"MarketingSite": "http%3a%2f%2fwww.macmillan.com.au%2fsecondary%2fonix%2fall%2f6F597241EFC0E43DCA257791001CAFC0%3fopen%26div%3dSecondary%26cat%3dScience%253EAustralian%252BCurriculum%26template%3ddomSecondary%26ed%3dsite%2fseced31.nsf",
"Skin": "OneStopScience%20Green"},
"tags": [
"s_science"'
"s_maths"'
"s_arts"
],
{"uid": "5",}]})
Then you reference them as data.tags[0], data.tags[1], data.tags[2].
if your response is in this format
YourResponse = {
"products" : [
{"uid" :"5", ......., "whtever":"someval"},
{"uid" :"6", ......., "whtever":"someval1"}
]
};
you can use this
$(YourResponse).each(
function(objName, objValue) {
console.log(objName); // wil get object name like uid, whtever
console.log(objValue); // wil get object's value
});
so to get Tags you will have to take Tuan's suggestion; send them in array
First, I should point out that I verified my JSON object with http://jsonlint.com and it is, indeed, valid.
Now that is out of the way, I'm looking at examples of the YUI DataTable, specifically the datasource and the structure of the JSON objects the examples use (see http://developer.yahoo.com/yui/examples/datatable/dt_basic.html).
The Basic Example uses a DataSource composed as follows:
YAHOO.example.Data = {
bookorders: [
{id:"po-0167", date:new Date(1980, 2, 24), quantity:1, amount:4, title:"A Book About Nothing"},
{id:"po-0783", date:new Date("January 3, 1983"), quantity:null, amount:12.12345, title:"The Meaning of Life"},
{id:"po-0297", date:new Date(1978, 11, 12), quantity:12, amount:1.25, title:"This Book Was Meant to Be Read Aloud"},
{id:"po-1482", date:new Date("March 11, 1985"), quantity:6, amount:3.5, title:"Read Me Twice"}
]
}
Whereas my JSON object looks like this:
[
{
"Listing": {
"Name": "Jay",
"Address": "Main Street",
"City": "New York"
}
},
{
"Listing": {
"Name": "Thomas",
"Address": "Union Street",
"City": "New York"
}
},
{
"Listing": {
"Name": "Jason",
"Address": "Square Street",
"City": "Boston"
}
}
]
Here is how Yahoo's example specifies the datasource and a few other lines tied to it:
var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.bookorders);
myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
myDataSource.responseSchema = {
fields: ["id","date","quantity","amount","title"]
};
In my JSON object, each "Listing" would be a row in the YUI DataTable. What do I need to modify in the YUI code to make it work with my JSON object?
Thank you.
Above you have defined an object, with an unnamed array of objects, each object is composed of another object, with members. While this might be valid JSON, I don't think this is compatible with the expectations of the YUI datatable. It is more of a contrived or obfuscated challenge.
I am unable to provide a way, using the existing JSON object. While your JSON is valid, IMHO, I do not believe it to be compatable with the YUI datatable.
I think you need an object containing a named array of objects that have members, not other objects. There are too many layers in the existing data structure, that serve no apparent purpose, to me.
'Change', below, implies changing the basic datatable example, provided by YAHOO.
Simply restructuring your data like so,
YAHOO.example.Data = {
Listing: [
{
"Name": "Jay",
"Address": "Main Street",
"City": "New York"
},
{
"Name": "Thomas",
"Address": "Union Street",
"City": "New York"
},
{
"Name": "Jason",
"Address": "Square Street",
"City": "Boston"
}
]
};
will simplify your data structure and make this work. This is the minimum change, I believe, considering the constraints.
Then change the datasource:
var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.Listing);
and the column defs
var myColumnDefs = [
{key:"Name"},
{key:"Address"},
{key:"City"}
];
and finally the response schema
myDataSource.responseSchema = {
fields: ["Name","Address","City"]
};
Hope that helps.