Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want the key and value of the following multidimensional array with key :followersperdate
{"version":"1.2","username":"LGUS","url":"http://t.co/ospRRVboge","avatar":"http://pbs.twimg.com/profile_images/378800000016800237/3787f02f0e0e10a0a19d9b508abd6ce2_normal.png","followers_current":38775,"date_updated":"2013-11-15","follow_days":"774","started_followers":544,"growth_since":38231,"average_growth":"49","tomorrow":"38824","next_month":"40245","followers_yesterday":37232,"rank":"14934","followersperdate":{"date2013-11-15":38775,"date2013-11-05":37232,"date2013-11-04":37126,"date2013-10-26":36203,"date2013-10-10":34384,"date2013-10-02":33353,"date2013-09-18":30870},"last_update":1384679796}
i cleared it for you, the object you want, has 7 sub objects, that can parse base on your language,
check this web page to learn more, choose your language, at the bottom of the site:
http://www.json.org/
if you are on a javaScript, check this http://www.w3schools.com/json/json_intro.asp
{
"version": "1.2",
"username": "LGUS",
"url": "http://t.co/ospRRVboge",
"avatar": "http://pbs.twimg.com/profile_images/378800000016800237/3787f02f0e0e10a0a19d9b508abd6ce2_normal.png",
"followers_current": 38775,
"date_updated": "2013-11-15",
"follow_days": "774",
"started_followers": 544,
"growth_since": 38231,
"average_growth": "49",
"tomorrow": "38824",
"next_month": "40245",
"followers_yesterday": 37232,
"rank": "14934",
"followersperdate": {
"date2013-11-15": 38775,
"date2013-11-05": 37232,
"date2013-11-04": 37126,
"date2013-10-26": 36203,
"date2013-10-10": 34384,
"date2013-10-02": 33353,
"date2013-09-18": 30870
},
"last_update": 1384679796
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
My problem is basically in that JSON.
I can't get the value of bytes_on_disk because the first object 2020-07-14T12:57:15.770557Z is being generated randomly with each request:
{"2020-07-14T12:57:15.770557Z":{"requests_served":291575,"cache_hits":215441,"cache_misses":76134,"browser_cached":0,"bytes_sent":161829275429,"bytes_on_disk":29648157174}}
$json_data->{}->{bytes_on_disk};
If you know there's only going to be one key, then you can get it using the keys() function.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use JSON;
my $json = '{
"2020-07-14T12:57:15.770557Z" : {
"requests_served" : 291575,
"cache_hits" : 215441,
"cache_misses" : 76134,
"browser_cached" : 0,
"bytes_sent" : 161829275429,
"bytes_on_disk" : 29648157174
}
}';
my $json_data = JSON->new->decode($json);
my ($key) = keys %$json_data;
say $json_data->{$key}{bytes_on_disk};
Alternatively, you could simply it slightly by just getting the only value in the hash with values().
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use JSON;
my $json = '{
"2020-07-14T12:57:15.770557Z" : {
"requests_served" : 291575,
"cache_hits" : 215441,
"cache_misses" : 76134,
"browser_cached" : 0,
"bytes_sent" : 161829275429,
"bytes_on_disk" : 29648157174
}
}';
my $json_data = JSON->new->decode($json);
my ($data) = values %$json_data;
say $data->{bytes_on_disk};
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
def self.top
data = JSON.parse(open("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd").read)
i = 0
while i < data.length
puts "#{i +1}. #{data[i]["name"]}"
i += 1
end
data.instance_of? Array
end
end
I'm getting this weird issue. Its returning true for instance_of array and false for a hash when i check it? Am I misunderstanding something here?
Also, would I be able to somehow rewrite that while loop for as an enumerable using data.each or data.collect etc.? It's probably something really basic I'm missing here.
Here is the API I'm consuming: https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd
[
{
"id": "bitcoin",
"symbol": "btc",
"name": "Bitcoin",
"image": "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
"current_price": 9950.38,
"market_cap": 182891806069,
"market_cap_rank": 1,
"total_volume": 56103214574,
"high_24h": 10008.09,
"low_24h": 9714.09,
"price_change_24h": 151.47,
"price_change_percentage_24h": 1.54578,
"market_cap_change_24h": 3302266164,
"market_cap_change_percentage_24h": 1.83879,
"circulating_supply": 18369100.0,
"total_supply": 21000000.0,
"ath": 19665.39,
"ath_change_percentage": -49.41389,
"ath_date": "2017-12-16T00:00:00.000Z",
"atl": 67.81,
"atl_change_percentage": 14570.55603,
"atl_date": "2013-07-06T00:00:00.000Z",
"roi": null,
"last_updated": "2020-05-08T18:35:22.865Z"
},
{
"id": "ethereum",
"symbol": "eth",
"name": "Ethereum",
"image": "https://assets.coingecko.com/coins/images/279/large/ethereum.png?1547034048",
"current_price": 214.11,
"market_cap": 23772912780,
"market_cap_rank": 2,
"total_volume": 18611819582,
"high_24h": 215.12,
"low_24h": 207.21,
"price_change_24h": 0.573439,
"price_change_percentage_24h": 0.26855,
"market_cap_change_24h": 169896199,
"market_cap_change_percentage_24h": 0.71981,
"circulating_supply": 110845051.624,
"total_supply": null,
"ath": 1448.18,
"ath_change_percentage": -85.23081,
"ath_date": "2018-01-13T00:00:00.000Z",
"atl": 0.432979,
"atl_change_percentage": 49298.3854,
"atl_date": "2015-10-20T00:00:00.000Z",
"roi": {
"times": 27.75112172597611,
"currency": "btc",
"percentage": 2775.112172597611
},
"last_updated": "2020-05-08T18:35:50.326Z"
},
That IS a hash right? I'm not crazy?
Just going to continue our discussion from the comments in this answer because I think it solves your question.
In terms of whether you need collect (or its more commonly used alias map), well that depends on what you need to do with this data.
To use the code you've given as a guide, then no, you don't need it in this case. You are just trying to loop over the array of hashes and print out the "name" property of each:
data.each_with_index do |hsh, i|
puts "#{i +1}. #{hsh["name"]}"
end
If you wanted to print out every key-val of every hash as its own line, that would be a case where you can iterate over the hash. Whether you use each or map/collect depends on whether you need to assign the result to a variable. For example the following two snippets are functionally identical, however with map you are transforming the array into a new array (each is only used for side effects and the return value of the block is discarded):
Printing each key-val of each hash with each:
data.each.with_index do |hsh, i|
hsh.each do |key, val|
puts "#{i + 1} #{key} = #{val}"
end
end
The same thing but using map to make a new, transformed array:
data.each.with_index do |hsh, i|
lines_to_print = hsh.map do |key, val|
"#{i + 1} #{key} = #{val}"
end
lines_to_print.each { |line| puts line }
end
I recommend reading more about each, map, and other Ruby Enumerable methods, I can't really give an in-depth explanation of everything here because the scope is a bit too large. But hopefully this example is illustrative anyway.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
please give me your guidance.
I'm using node with mysql database (without using orm).
i use snake_case naming convention for mysql.
my question is:
in node,
should i use snake_case or use camelCase ?
for e.g. at models/movie.js:
snake_case:
return db.execute(SELECT title, daily_rental_rate FROM movies);
the result sent to the client:
{
title: 'abc',
daily_rental_rate: 20
}
camel_case:
return db.execute(SELECT title, daily_rental_rate as dailyRentalRate FROM movies);
the result sent to the client:
{
title: 'abc',
dailyRentalRate: 20
}
thank you so much /\
There is no fixed convention for JSON casing, however most APIs tend to use camelCase for properties, include Google, see their style guide here.
You can also map object properties within JavaScript, you don't have to do this manually in your queries. This allows you to be relatively flexible with your casing, even changing to kebab-case or snake_case if you wish to later on. This example uses the lodash library to convert object case.
const _ = require("lodash");
function objectToCamelCase(obj) {
return _.mapKeys(obj, (v, k) => _.camelCase(k))
}
let rows = db.execute("SELECT title, daily_rental_rate FROM movies");
console.log("Result (snake_case): ", rows);
rows = rows.map(objectToCamelCase);
console.log("Result (camelCase):", rows);
The results might look like so:
Result (snake_case):
[
{
"title": "The Shawshank Redemption",
"daily_rental_rate": "€2.99"
},
{
"title": "Ferris Bueller's Day Off",
"daily_rental_rate": "€2.99"
}
]
Result (camelCase):
[
{
"title": "The Shawshank Redemption",
"dailyRentalRate": "€2.99"
},
{
"title": "Ferris Bueller's Day Off",
"dailyRentalRate": "€2.99"
}
]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to sum up all the values in the json that contain the string "Example". But I have no idea how to do it.
I'm using the "Newtonsoft.Json" framework.
{
"Example1344": 13,
"Example925": 16,
"Example454Example": 24,
"Nothing": 51,
"Other9235": 45
}
So that the result would be 53
Something along these lines might work for you:
Imports Newtonsoft.Json.Linq
Module Module1
Sub Main()
Dim json As String = $"{{
""Example1344"": 13,
""Example925"": 16,
""Example454Example"": 24,
""Nothing"": 51,
""Other9235"": 45
}}"
Dim model As JObject = JObject.Parse(json)
Dim value As Int16 = 0
value = model.Properties _
.Where(Function(m) m.Name.Contains("Example")) _
.Sum(Function(m) m.Value)
Console.WriteLine($"Sum: {value}")
Console.WriteLine("PRESS ANY KEY TO EXIT")
Console.ReadKey(True)
End Sub
End Module
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am working on designing the JSON document of my data. Below is a single value (v) which contains site-id, price-score and confidence-score for now.
{
"v" : {
"site-id" : 0,
"price-score" : 0.5,
"confidence-score" : 0.2
}
}
Now, I want to add categories list into the above JSON document. As I am going to have multiple categories for a single value (v) so I came up with below JSON document-
{
"v" : {
"site-id" : 0,
"price-score" : 0.5,
"confidence-score" : 0.2,
"categories": [
{
"category-id": "123",
"price-score": "0.5",
"confidence-score": "0.2"
},
{
"category-id": "321",
"price-score": "0.2",
"confidence-score": "0.4"
}
]
}
}
Can anyone take a look and let me know if it looks good in the way I have added list of categories in the above JSON document? Or is there any better way of doing the same? As I don't want to start having problems when I am working on serializing and deserializing the above JSON document.
I recommend:
{
"v" : {
"site-id" : 0,
"price-score" : 0.5,
"confidence-score" : 0.2,
"categories": {
"123" : {
"price-score": "0.5",
"confidence-score": "0.2"
},
"321" : {
"price-score": "0.2",
"confidence-score": "0.4"
}
}
}
}
This way, you can easily use:
json.v.categories[id]
to get information about a specific category, instead of having to write:
var the_category;
for (var i = 0; i < json.v.categories.length; i++) {
if (json.v.categories[i]['category-id'] == id) {
the_category = json.v.categories[i];
break;
}
}
Another suggestion: use _ rather than - in the keys (or camelCase if you prefer), as hyphen prevents you from using . notation to access elements (notice that I had to write ['category-id'] above instead of .category-id.