Importing a csv into d3; can't convert strings to numbers - csv

I have a csv file with data as follows:
Time, Conditions, Temperature, Humidity, WindDir, WindDeg, U1, Pressure, U2, U3, U4, U4, UV, U5, MoonPercent, SunriseHr, SunriseMin, SunsetHr
"2014/06/19 19:00:00", "Clear", 16.8, "87%", "North", 355, 8.0, "1010", "11", 15, "NA", "1.2", " 0", "11", "47", "5", "03" "22", "07"
"2014/06/19 19:31:01", "Mostly Cloudy", 17.2, "86%", "NNE", 26, 12.9, "1010", "11", 15, "NA", "0.7", " 0", "11", "47", "5", "03" "22", "07"
"2014/06/19 19:40:00", "Mostly Cloudy", 17.4, "85%", "ENE", 68, 6.4, "1010", "11", 15, "NA", "0.7", " 0", "11", "47", "5", "03" "22", "07"
I want to import it and have array contain the numbers as numbers, not as strings as it does at first:
Object { Time="2014/06/19 19:00:00", Conditions=" "Clear"", Temperature=" 16.8", more...}
The problem is, no matter what I try I've not been able to turn the numbers into actual numbers, just NaN. Here's my import code:
d3.csv("weatherLog.csv", function(error, csv) {
if (error) return console.warn(error);
csv.forEach(function(d){ (d['Temperature'] = +d['Temperature']); });
console.log(csv);
I tried d.Temperature instead, I tried parseInt(d.['Temperature']) instead, but nothing works out, and of course d3 can't use a string (or a NaN) as data so I need to convert this first somehow. I tried manually removing the space from the CSV file (so the imported string was "16.8" rather than " 16.8") but that didn't help either...

You should use an accessor function to convert the strings to dates and numbers. The way to do that is to use the .row() method, which iterates over the rows of your csv, and for each row, lets you provide an output object that will be used to represent that row, based on the given data.
The parameter given to the accessor, d, represents a single row, or datum. Within the accessor function, d has properties corresponding to the names of your data columns, such as Time, Conditions, Temperature, etc... You can manipulate these values before returning your output object, so in the case of temperature, you could assign the temperature property of your output object to be +d.Temperature, which converts it to a number.
By convention, property names are camel cased in JavaScript, so the object you return from the accessor should use camel cased keys.
Now when your callback runs, your variable csv contains an array of objects, each of which has properties such as time, conditions, temperature, etc... and the values for temperature have been converted to numbers.
Here is an example:
d3.csv('weatherLog.csv')
.row(function(d) {
// for each row of the data, create an object with these properties...
return {
time: d3.time.format('%Y/%m/%d %H:%M:%S').parse(d.Time),
conditions: d.Conditions,
temperature: +d.Temperature,
humidity: d.Humidity,
windDir: d.WindDir,
windDeg: +d.WindDeg,
pressure: +d.Pressure
};
})
.get(function(error, csv) {
if (!error) {
// lets say you want to log all the temperatures to the console
csv.forEach(function(d,i) {
var theTime = d3.time.format('%I:%M %p')(d.time);
console.log('The temperature at', theTime, 'was', d.temperature, 'degrees.');
});
} else {
// handle error
}
});
This will output the following to console:
The temperature at 7:00 PM was 16.8 degrees.
The temperature at 7:31 PM was 17.2 degrees.
The temperature at 7:40 PM was 17.4 degrees.

This worked for me.
d3.csv("weatherLog.csv", function(d) {
return {
temperature: +d.Temperature;
}
}, function(error, csv) {
if (error) return console.warn(error);
console.log(csv);
});

Related

How to access field in list of JSON objects in a django SerializerMethodField?

I am creating an API for a game where users get points for labelling images. If a user has labeled an image with the same label as another, the score gets updated by 5 points (and 25 if that player is their opponent). I have declared the field score inside my serializer as a SerializerMethodField.
In the get_score method, I am trying to access the tag__name field inside the list of tags_to_compare and simply extract the strings and store them in a list.
I have tried the following inside my serializer:
Update
I have updated my get_score method as follows:
serializers.py
score = serializers.SerializerMethodField('get_score')
...
def get_score(self, tagging):
score = 0
resource_id = tagging.resource.id
# A previously played gameround for this resource is coordinated for Tag verification
coordinated_gameround = Gameround.objects.all().filter(taggings__resource_id=resource_id).order_by("?").first()
# list of tag_name from coordinated gameround
coordinated_gameround_tags = coordinated_gameround.taggings.all().values_list("tag__name", flat=True)
if Tag.objects.all().filter(name=tagging) in coordinated_gameround_tags:
score += 25
return score
I tested the following line and it retrieves a list of taggings.
coordinated_gameround_tags = coordinated_gameround.taggings.all().values_list("tag__name", flat=True)
This is the JSON Object where the list of tags is:
{
"gameround": {
"id": 2014305513,
"user": {
"id": 1,
"username": "carina",
"is_superuser": true
},
"score": 50,
"tags_to_compare": [
{
"tag_id": 153,
"tag__name": "night",
"tag__language": "en",
"resource_id": 323570
},
{
"tag_id": 10437,
"tag__name": "dress",
"tag__language": "en",
"resource_id": 323570
},
{
"tag_id": 9598,
"tag__name": "sleep",
"tag__language": "en",
"resource_id": 323570
}
]
}
}
coordinated_gameround is a Gameround instance of your model. Not a Dict. So you cannot access it with coordinated_gameround[key].
If your Gameround has a field named "name" or "langage", you can get those values with
name = coordinated_gameround.name
langage = coordinated_gameround.langage
Also, there might be an error with this line
Tag.objects.all().filter(name=tagging) in coordinated_gameround_tags
Do you expect coordinated_gameround_tags to contain a list of queryset ?

how to extract properly when sqlite json has value as an array

I have a sqlite database and in one of the fields I have stored complete json object . I have to make some json select requests . If you see my json
the ALL key has value which is an array . We need to extract some data like all comments where "pod" field is fb . How to extract properly when sqlite json has value as an array ?
select json_extract(data,'$."json"') from datatable ; gives me entire thing . Then I do
select json_extract(data,'$."json"[0]') but i dont want to do it manually . i want to iterate .
kindly suggest some source where i can study and work on it .
MY JSON
{
"ALL": [{
"comments": "your site is awesome",
"pod": "passcode",
"originalDirectory": "case1"
},
{
"comments": "your channel is good",
"data": ["youTube"],
"pod": "library"
},
{
"comments": "you like everything",
"data": ["facebook"],
"pod": "fb"
},
{
"data": ["twitter"],
"pod": "tw",
"ALL": [{
"data": [{
"codeLevel": "3"
}],
"pod": "mo",
"pod2": "p"
}]
}
]
}
create table datatable ( path string , data json1 );
insert into datatable values("1" , json('<abovejson in a single line>'));
Simple List
Where your JSON represents a "simple" list of comments, you want something like:
select key, value
from datatable, json_each( datatable.data, '$.ALL' )
where json_extract( value, '$.pod' ) = 'fb' ;
which, using your sample data, returns:
2|{"comments":"you like everything","data":["facebook"],"pod":"fb"}
The use of json_each() returns a row for every element of the input JSON (datatable.data), starting at the path $.ALL (where $ is the top-level, and ALL is the name of your array: the path can be omitted if the top-level of the JSON object is required). In your case, this returns one row for each comment entry.
The fields of this row are documented at 4.13. The json_each() and json_tree() table-valued functions in the SQLite documentation: the two we're interested in are key (very roughly, the "row number") and value (the JSON for the current element). The latter will contain elements called comment and pod, etc..
Because we are only interested in elements where pod is equal to fb, we add a where clause, using json_extract() to get at pod (where $.pod is relative to value returned by the json_each function).
Nested List
If your JSON contains nested elements (something I didn't notice at first), then you need to use the json_tree() function instead of json_each(). Whereas the latter will only iterate over the immediate children of the node specified, json_tree() will descend recursively through all children from the node specified.
To give us some data to work with, I have augmented your test data with an extra element:
create table datatable ( path string , data json1 );
insert into datatable values("1" , json('
{
"ALL": [{
"comments": "your site is awesome",
"pod": "passcode",
"originalDirectory": "case1"
},
{
"comments": "your channel is good",
"data": ["youTube"],
"pod": "library"
},
{
"comments": "you like everything",
"data": ["facebook"],
"pod": "fb"
},
{
"data": ["twitter"],
"pod": "tw",
"ALL": [{
"data": [{
"codeLevel": "3"
}],
"pod": "mo",
"pod2": "p"
},
{
"comments": "inserted by TripeHound",
"data": ["facebook"],
"pod": "fb"
}]
}
]
}
'));
If we were to simply switch to using json_each(), then we see that a simple query (with no where clause) will return all elements of the source JSON:
select key, value
from datatable, json_tree( datatable.data, '$.ALL' ) limit 10 ;
ALL|[{"comments":"your site is awesome","pod":"passcode","originalDirectory":"case1"},{"comments":"your channel is good","data":["youTube"],"pod":"library"},{"comments":"you like everything","data":["facebook"],"pod":"fb"},{"data":["twitter"],"pod":"tw","ALL":[{"data":[{"codeLevel":"3"}],"pod":"mo","pod2":"p"},{"comments":"inserted by TripeHound","data":["facebook"],"pod":"fb"}]}]
0|{"comments":"your site is awesome","pod":"passcode","originalDirectory":"case1"}
comments|your site is awesome
pod|passcode
originalDirectory|case1
1|{"comments":"your channel is good","data":["youTube"],"pod":"library"}
comments|your channel is good
data|["youTube"]
0|youTube
pod|library
Because JSON objects are mixed in with simple values, we can no longer simply add where json_extract( value, '$.pod' ) = 'fb' because this produces errors when value does not represent an object. The simplest way around this is to look at the type values returned by json_each()/json_tree(): these will be the string object if the row represents a JSON object (see above documentation for other values).
Adding this to the where clause (and relying on "short-circuit evaluation" to prevent json_extract() being called on non-object rows), we get:
select key, value
from datatable, json_tree( datatable.data, '$.ALL' )
where type = 'object'
and json_extract( value, '$.pod' ) = 'fb' ;
which returns:
2|{"comments":"you like everything","data":["facebook"],"pod":"fb"}
1|{"comments":"inserted by TripeHound","data":["facebook"],"pod":"fb"}
If desired, we could use json_extract() to break apart the returned objects:
.mode column
.headers on
.width 30 15 5
select json_extract( value, '$.comments' ) as Comments,
json_extract( value, '$.data' ) as Data,
json_extract( value, '$.pod' ) as POD
from datatable, json_tree( datatable.data, '$.ALL' )
where type = 'object'
and json_extract( value, '$.pod' ) = 'fb' ;
Comments Data POD
------------------------------ --------------- -----
you like everything ["facebook"] fb
inserted by TripeHound ["facebook"] fb
Note: If your structure contained other objects, of different formats, it may not be sufficient to simply select for type = 'object': you may have to devise a more subtle filtering process.

jq: Turn an array of objects into individual objects and use each array index as a new key

I have several large json objects (think GB scale), where the object values in some of the innermost levels are arrays of objects. I'm using jq 1.4 and I'm trying to break these arrays into individual objects, each of which will have a key such as g__0 or g__1, where the numbers correspond to the index in the original array, as returned by the keys function. The number of objects in each array may be arbitrarily large (in my example it is equal to 3). At the same time I want to keep the remaining structure.
For what it's worth the original structure comes from MongoDB, but I am unable to change it at this level. I will then use this json file to create a schema for BigQuery, where an example column will be seeds.g__1.guid and so on.
What I have:
{
"port": 4500,
"notes": "This is an example",
"seeds": [
{
"seed": 12,
"guid": "eaf612"
},
{
"seed": 23,
"guid": "bea143"
},
{
"seed": 38,
"guid": "efk311"
}
]
}
What I am hoping to achieve:
{
"port": 4500,
"notes": "This is an example",
"seeds": {
"g__0": {
"seed": 12,
"guid": "eaf612"
},
"g__1": {
"seed": 23,
"guid": "bea143"
},
"g__2": {
"seed": 38,
"guid": "efk311"
}
}
}
Thanks!
The following jq program should do the trick. At least it produces the desired results for the given JSON. The program is so short and straightforward that I'll let it speak for itself:
def array2object(prefix):
. as $in
| reduce range(0;length) as $i ({}; .["\(prefix)_\($i)"] = $in[$i]);
.seeds |= array2object("g__")
So, you essentially want to transpose (pivot) your data in BigQuery Table such that instead of having data in rows as below
you will have your data in columns as below
Thus, my recommendation would be
First, load your data as is to start with
So now, instead of doing schema transformation outside of BigQuery, let’s rather do it within BigQuery!
Below would be an example of how to achieve transformation you are looking for (assuming you have max three items/objects in array)
#standardSQL
SELECT
port, notes,
STRUCT(
seeds[SAFE_OFFSET(0)] AS g__0,
seeds[SAFE_OFFSET(1)] AS g__1,
seeds[SAFE_OFFSET(2)] AS g__2
) AS seeds
FROM yourTable
You can test this with dummy data using CTE like below
#standardSQL
WITH yourTable AS (
SELECT
4500 AS port, 'This is an example' AS notes,
[STRUCT<seed INT64, guid STRING>
(12, 'eaf612'), (23, 'bea143'), (38, 'efk311')
] AS seeds
UNION ALL SELECT
4501 AS port, 'This is an example 2' AS notes,
[STRUCT<seed INT64, guid STRING>
(42, 'eaf412'), (53, 'bea153')
] AS seeds
)
SELECT
port, notes,
STRUCT(
seeds[SAFE_OFFSET(0)] AS g__0,
seeds[SAFE_OFFSET(1)] AS g__1,
seeds[SAFE_OFFSET(2)] AS g__2
) AS seeds
FROM yourTable
So, technically, if you know max number of items/object in seeds array – you can just manually write needed SQL statement, to run it against real data.
Hope you got an idea
Of course you can script /automate process – you can find examples for similar pivoting tasks here:
https://stackoverflow.com/a/40766540/5221944
https://stackoverflow.com/a/42287566/5221944

How to check a value matches with another correpsonding value in a json response

What is the most dynamic way of checking each instance of a json response value matches another json response value within a script assertion?
What I mean is lets say I have the following response below:
{
"xxx": [{
"roomInformation": [{
"xxx": xxx
}],
"totalPrice": xxx
},
{
"roomInformation": [{
xxx: xxx
}],
"totalPrice": xxx
}
]
}
I want to check that the first room price to match with the first totalPrice and the second roomPrice to match with the second totalPrice. It has to be dynamic as I may get many different instances of this so I can't simply just look through the json with [0] and [1]. Virtually check each roomPrice matches with its corresponding totalPrice.
Thanks
So given the Json as a variable:
def jsonTxt = '''{
"hotels": [{
"roomInformation": [{
"roomPrice": 618.4
}],
"totalPrice": 618.4
},
{
"roomInformation": [{
"roomPrice": 679.79
}],
"totalPrice": 679.79
}
]
}'''
We can then use the following script:
import groovy.json.*
new JsonSlurper().parseText(jsonTxt).hotels.each { hotel ->
assert hotel.roomInformation.roomPrice.sum() == hotel.totalPrice
}
As you can see, I'm using sum to add all the roomInformation.roomPrice values together. In your example, you only have one price, so this will be fine.. And it will also cover the case where you have multiple rooms adding up to make the total
Here is the script assertion to check each roomPrice is matching or not with totalPrice.
EDIT: based on OP's full response provided here
Script Assertion:
//Check if the response is not empty
assert context.response, "Response is empty or null"
def json = new groovy.json.JsonSlurper().parseText(context.response)
def sb = new StringBuffer()
json.regions.each { region ->
region.hotels.each { hotel ->
(hotel?.totalPrice == hotel?.roomInformation[0]?.roomPrice) ?: sb.append("Room price ${hotel?.roomInformation[0]?.roomPrice} is not matching with total price ${hotel.totalPrice}")
}
}
if (sb.toString()) {
throw new Error(sb.toString())
} else { log.info 'Prices match' }

Having trouble to decode json string

I have an json string i want to parse to small array of objects, i am using decoder for that but it wont help, why is this happining?
I have defined the variable as $cleanforcharacters
$cleanforcharacters = preg_replace('/["{mtwrdayfsasusseto}"]_/', '', $found['newdiscounthours']);
this is my output
discount_org: "{"day":"8:00","time":"12:00","discount":"10","day":"8:00","time":"12:00","discount":"10"}"
this is desired output (array of objects)
discount_org: [
{
day: 0,
time: 8,
discount: 10
},
{
day: 0,
time: 14,
discount: 10
},
this is how i tried
$arrayOfEmails = json_decode($cleanforcharacters);
and this is what i am getting now
discount_org: {
day: "20",
time: "12:00",
discount: "20"
}
the rest is not coming out either
This is because you have declared that as an object and the keys are overriding the values rather than being set as new values:-
You have given:-
discount_org: "{"day":"8:00","time":"12:00","discount":"10","day":"8:00","time":"12:00","discount":"10"}"
It should be:-
discount_org: "[{"day":"8:00","time":"12:00","discount":"10"},{"day":"8:00","time":"12:00","discount":"10"}]"
Then use:-
$arrayOfEmails = json_decode($cleanforcharacters,true);
This would give you correct result.