The json response from api is like this
{
"ResponseCode": "1",
"Response": "Data Found",
"data": [
{
"Season": "KHARIF",
"Sector": "AGRICULTURE",
"Category": "Cereals",
"Crop": "Paddy (Dhan)",
"QueryType": "\tField Preparation\t",
"QueryText": "top dressing for paddy",
"KccAns": "top dressing for paddy : apply urea 25kg+SSP 15kg + neem cake 5kg+MN mixture 5kg mixed with 40kg of sand",
"StateName": "PUDUCHERRY",
"DistrictName": "KARAIKAL",
"BlockName": "KARAIKAL",
"CreatedOn": "1/5/2014 6:48:09 PM"
},
{
"Season": "KHARIF",
"Sector": "AGRICULTURE",
"Category": "Others",
"Crop": "Others",
"QueryType": "Weather",
"QueryText": "weather forecasting details",
"KccAns": "today no rain",
"StateName": "PUDUCHERRY",
"DistrictName": "KARAIKAL",
"BlockName": "KARAIKAL",
"CreatedOn": "1/5/2014 9:04:50 PM"
},
{
"Season": "KHARIF",
"Sector": "AGRICULTURE",
"Category": "Others",
"Crop": "Others",
"QueryType": "0",
"QueryText": "details about soil testing",
"KccAns": "contact to agricultural department",
"StateName": "PUDUCHERRY",
"DistrictName": "KARAIKAL",
"BlockName": "KARAIKAL",
"CreatedOn": "1/8/2014 10:21:18 AM"
},
{
"Season": "KHARIF",
"Sector": "AGRICULTURE",
"Category": "Cereals",
"Crop": "Paddy (Dhan)",
"QueryType": "Fertilizer Use and Availability",
"QueryText": "paddy top dressing fertilizer",
"KccAns": "apply urea 25 kg + potash 15 kg + neem cake 5 kg + microfood 5 kg / ac",
"StateName": "PUDUCHERRY",
"DistrictName": "KARAIKAL",
"BlockName": "KARAIKAL",
"CreatedOn": "1/12/2014 8:01:45 AM"
}
]
}
I am trying to write a golang template which returns only the first two data points in data section of the response object. This is the template {{range $element := .data}} {{$element}} {{end}} i am using at the moment but this returns all the sub data in .data feild. How can i make this workout.
You can use the slice template function to take the first two elements. Example:
{{$dataSliced := slice .data 0 2}}
{{range $element := $dataSliced}}
{{$element}}
{{end}}
Or you can also create a custom template function for the slicing.
More about template function: https://golang.org/pkg/text/template.
Related
https://developer.foursquare.com/overview/versioning it says:
You can specify the locale by setting the Accept-Language HTTP header in your request. Alternatively, you can add a locale=XXX parameter to your request but HTTP header specification is preferred. We currently support en (default), es, fr, de, it, ja, th, tr, ko, ru, pt, and id.
and
If nothing is specified, for geographical entities (e.g., city names), we'll fall back to using the language that's most popular in the country for that venue.
Now, how do we get the locale of the API result? If I query with tr locale, it may answer me back with en or another fallback locale. I couldn't find it neither inside the results nor the response headers.
I need this to cache and show the users the right data. My website's fallback language is en for example, but the API might give me de for example.
Thanks.
Edit Here's an example of what I try to tell;
URL:
https://api.foursquare.com/v2/venues/4adcda50f964a520354121e3
?client_id=[client_id]&client_secret=[client_secret]&v=20161115&locale=tr
Headers:
array (
'Accept-Language' => 'tr',
'Accept' => 'application/json'
)
Result (Cropped some unrelated items):
{
"meta": {
"code": 200,
"requestId": "582bfe9e1ed21964a18d3aab"
},
"response": {
"venue": {
"id": "4adcda50f964a520354121e3",
"name": "La Pedrera (Casa Milà) (Casa Milà 'La Pedrera')",
"contact": {
"phone": "+34902202138",
"formattedPhone": "+34 902 20 21 38",
"twitter": "catfundacio",
"facebook": "429182007214758",
"facebookUsername": "lapedrera.barcelona",
"facebookName": "La Pedrera-Casa Milà"
},
"location": {
"address": "Pg. Gràcia, 92",
"crossStreet": "Carrer Provença",
"lat": 41.39509128050475,
"lng": 2.1618343621091296,
"postalCode": "08008",
"cc": "ES",
"city": "Barselona",
"state": "Cataluña",
"country": "İspanya",
"formattedAddress": [
"Pg. Gràcia, 92 (Carrer Provença)",
"08008 Barselona Catalonia",
"İspanya"
]
},
"canonicalUrl": "https:\/\/foursquare.com\/v\/la-pedrera-casa-mil%C3%A0\/4adcda50f964a520354121e3",
"categories": [
{
"id": "4bf58dd8d48988d12d941735",
"name": "Anıt \/ Abide",
"pluralName": "Anıtlar \/ Abideler",
"shortName": "Abide",
"icon": {
"prefix": "https:\/\/ss3.4sqi.net\/img\/categories_v2\/building\/government_monument_",
"suffix": ".png"
},
"primary": true
},
{
"id": "4bf58dd8d48988d130941735",
"name": "Bina",
"pluralName": "Binalar",
"shortName": "Bina",
"icon": {
"prefix": "https:\/\/ss3.4sqi.net\/img\/categories_v2\/building\/default_",
"suffix": ".png"
}
}
],
"description": "La Pedrera (Casa Milà) és un edifici construït per Antoni Gaudí i declarat Patrimoni de la Humanitat. Actualment és la seu de la Fundació Catalunya-La Pedrera.",
"storeId": "",
"tags": [
"gallery",
"gaudi",
"modernisme",
"museum",
"photobooth",
"sicted"
],
"shortUrl": "http:\/\/4sq.com\/72NCMi",
"timeZone": "Europe\/Madrid",
"hours": {
"status": "09:00'e kadar kapalı",
"isOpen": false,
"isLocalHoliday": false,
"timeframes": [
{
"days": "Pzt\u2013Paz",
"includesToday": true,
"open": [
{
"renderedTime": "09:00\u201321:00"
}
],
"segments": [
]
}
]
},
"attributes": {
"groups": [
{
"type": "wifi",
"name": "Kablosuz Bağlantı",
"summary": "Ücretsiz Wi-Fi",
"count": 1,
"items": [
{
"displayName": "Kablosuz Bağlantı",
"displayValue": "Ücretsiz"
}
]
}
]
},
"bestPhoto": {
"id": "55672a92498ec352981dd144",
"createdAt": 1432824466,
"source": {
"name": "Foursquare Web",
"url": "https:\/\/foursquare.com"
},
"prefix": "https:\/\/irs2.4sqi.net\/img\/general\/",
"suffix": "\/39082263_M0PK-NTv3A-0tI4j_aEIp5k6BEkliQTkMXS9TIPg_NQ.jpg",
"width": 620,
"height": 478,
"visibility": "public"
}
}
}
}
The addresses, categories, some of the names, timeframes, attributes sections are in fact Turkish, but the description and the most of the names are in fallback/local language which is Spanish in this case.
The locale is not specified in the API response.
If you don't specify anything in the Accept-Language HTTP header the response will default to the language that's most popular in the country. However, if you do specify a local the API won't fallback to anything else.
If your website's language is english, you can specify en in the Accept-Language HTTP header and the API will give you english too.
I have data like this:
{
"movies": [
{
"abridged_cast": [
{
"characters": [
"Dominic Toretto"
],
"id": "162652472",
"name": "Vin Diesel"
},
{
"characters": [
"Brian O'Conner"
],
"id": "162654234",
"name": "Paul Walker"
},
{
"characters": [
"Louie Tran"
],
"id": "162684066",
"name": "Tony Jaa"
},
{
"characters": [
"Deckard Shaw"
],
"id": "162653720",
"name": "Jason Statham"
},
{
"characters": [
"Luke Hobbs"
],
"id": "770893686",
"name": "Dwayne \"The Rock\" Johnson"
}
],
"alternate_ids": {
"imdb": "2820852"
},
"critics_consensus": "",
"id": "771354922",
"links": {
"alternate": "http://www.rottentomatoes.com/m/furious_7/",
"cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/771354922/cast.json",
"reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/771354922/reviews.json",
"self": "http://api.rottentomatoes.com/api/public/v1.0/movies/771354922.json",
"similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/771354922/similar.json"
},
"mpaa_rating": "PG-13",
"posters": {
"detailed": "http://resizing.flixster.com/pVDoql2vCTzNNu0t6z0EUlE5G_c=/51x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/18/14/11181482_ori.jpg",
"original": "http://resizing.flixster.com/pVDoql2vCTzNNu0t6z0EUlE5G_c=/51x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/18/14/11181482_ori.jpg",
"profile": "http://resizing.flixster.com/pVDoql2vCTzNNu0t6z0EUlE5G_c=/51x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/18/14/11181482_ori.jpg",
"thumbnail": "http://resizing.flixster.com/pVDoql2vCTzNNu0t6z0EUlE5G_c=/51x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/18/14/11181482_ori.jpg"
},
"ratings": {
"audience_rating": "Upright",
"audience_score": 88,
"critics_rating": "Certified Fresh",
"critics_score": 82
},
"release_dates": {
"theater": "2015-04-03"
},
"runtime": 140,
"synopsis": "Continuing the global exploits in the unstoppable franchise built on speed, Vin Diesel, Paul Walker and Dwayne Johnson lead the returning cast of Fast & Furious 7. James Wan directs this chapter of the hugely successful series that also welcomes back favorites Michelle Rodriguez, Jordana Brewster, Tyrese Gibson, Chris \"Ludacris\" Bridges, Elsa Pataky and Lucas Black. They are joined by international action stars new to the franchise including Jason Statham, Djimon Hounsou, Tony Jaa, Ronda Rousey and Kurt Russell.",
"title": "Furious 7",
"year": 2015
}
]
}
I need to parse all data from all fields from this JSON file. Is there a way to do it in React JS? Could you please suggest me any way to parse data from a structured JSON file like this?
React lives in JavaScript. So parsing a JSON string is done with:
var myObject = JSON.parse(myjsonstring);
How to fetch a file from somewhere with AJAX is a different question.
You could use fetch() for this. See for example
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API or
https://davidwalsh.name/fetch or
https://blog.gospodarets.com/fetch_in_action
{
"statusCode": "000",
"statusMessage": "Record Successfully Fetched",
"dsStatusCode": "000",
"dsStatusMessage": "Record Successfully Fetched",
"businessInput": null,
"businessOutput": {
"systemCircleId": "2",
"category": [
{
"categoryId": "abcs",
"sys": "5ID",
"displayName": "National Roaming Recharge",
"packsList": [
{
"amount": "79",
"benefits": "dsdsdsds",
"packId": "1344",
"processingFees": "70.3",
"serviceTax": "8.7",
"validity": "30 Days",
"volume": "0.0",
"isTop5": "no",
"fileName": "null"
},
{
"amount": "188",
"benefits": "Roaming Tariff - Incoming Free, Outgoing local # 80p/min, STD #1.15Rs/min with Talk Time 120 in main A/c",
"packId": "1263",
"fess": "47.3",
"serviceTax": "20.7",
"validity": "28 Days",
"volume": "0.0",
"isTop5": "no",
"fileName": "null"
},
{
"amount": "306",
"benefits": "FTT 306 with Roaming Tariff - Incoming Free, Outgoing local # 80p/min, STD #1.15Rs/min",
"packId": "1290",
"processingFees": "0",
"serviceTax": "33.7",
"validity": "28 Days",
"volume": "0.0",
"isTop5": "no",
"fileName": "null"
}
]
}
]
}
}
I want to parse this json to filter packlist for each category id using angularjs
assign a variable to the JSON you have. and use scope.$eval on the variable
Example
var jsonVar = { "statusCode": "000",
"statusMessage": "Record Successfully Fetched",
"dsStatusCode": "000",
"dsStatusMessage": "Record Successfully Fetched",
"businessInput": null
}
scope.$eval(jsonVar) // this gives the object on which you can do the ng-repeat
if you still have problems. Try using JSON.stringify(jsonVar) and then perform a scope.$eval on the this.
var jsonString = JSON.stringify(jsonVar);
scope.$eval(jsonString);// This returns a object too
I have a json file and I want to convert it to csv format.
The problem I face is that every json object in the file has not the same length of the converted columns I have. For example the one object have 49 columnns and the next have 50.
I provide here an example of 2 data from which the first one has not the creator.slug but the next has it is and so there is the problem with data. The problem is that the process create all 50 columns but for the object which don't have the value creator.slug it takes the next price.
{
"id": 301852363,
"name": "Song of the Sea",
"blurb": "One evening, two shows: SIRENS and The Girl From Bare Cove. Building a community. Giving voice to survivors of sexual violence.",
"goal": 5000,
"pledged": 671,
"state": "live",
"slug": "song-of-the-sea",
"disable_communication": false,
"country": "US",
"currency": "USD",
"currency_symbol": "$",
"currency_trailing_code": true,
"deadline": 1399293386,
"state_changed_at": 1397133386,
"created_at": 1396672480,
"launched_at": 1397133386,
"backers_count": 20,
"photo": {
"full": "https://s3.amazonaws.com/ksr/projects/939387/photo-full.jpg?1397874930",
"ed": "https://s3.amazonaws.com/ksr/projects/939387/photo-ed.jpg?1397874930",
"med": "https://s3.amazonaws.com/ksr/projects/939387/photo-med.jpg?1397874930",
"little": "https://s3.amazonaws.com/ksr/projects/939387/photo-little.jpg?1397874930",
"small": "https://s3.amazonaws.com/ksr/projects/939387/photo-small.jpg?1397874930",
"thumb": "https://s3.amazonaws.com/ksr/projects/939387/photo-thumb.jpg?1397874930",
"1024x768": "https://s3.amazonaws.com/ksr/projects/939387/photo-1024x768.jpg?1397874930",
"1536x1152": "https://s3.amazonaws.com/ksr/projects/939387/photo-1536x1152.jpg?1397874930"
},
"creator": {
"id": 1714048992,
"name": "Maridee Slater",
"slug": "maridee",
"avatar": {
"thumb": "https://s3.amazonaws.com/ksr/avatars/996153/DSC_0310.thumb.jpg?1337713264",
"small": "https://s3.amazonaws.com/ksr/avatars/996153/DSC_0310.small.jpg?1337713264",
"medium": "https://s3.amazonaws.com/ksr/avatars/996153/DSC_0310.medium.jpg?1337713264"
},
"urls": {
"web": {
"user": "https://www.kickstarter.com/profile/maridee"
},
"api": {
"user": "https://api.kickstarter.com/v1/users/1714048992?signature=1398256877.e6d63adcca055cd041a5920368b197d40459f748"
}
}
},
"location": {
"id": 2459115,
"name": "New York",
"slug": "new-york-ny",
"short_name": "New York, NY",
"displayable_name": "New York, NY",
"country": "US",
"state": "NY",
"urls": {
"web": {
"discover": "https://www.kickstarter.com/discover/places/new-york-ny",
"location": "https://www.kickstarter.com/locations/new-york-ny"
},
"api": {
"nearby_projects": "https://api.kickstarter.com/v1/discover?signature=1398256786.89b2c4539aeab4ad25982694dd7e659e8c12028f&woe_id=2459115"
}
}
},
"category": {
"id": 17,
"name": "Theater",
"slug": "theater",
"position": 14,
"urls": {
"web": {
"discover": "http://www.kickstarter.com/discover/categories/theater"
}
}
},
"urls": {
"web": {
"project": "https://www.kickstarter.com/projects/maridee/song-of-the-sea"
}
}
},
{
"id": 967108708,
"name": "Good Bread Alley",
"blurb": "A play by April Yvette Thompson. A Gullah Healer Woman and an Afro-Cuban Priest forge a new world of magic & dreams in Jim Crow Miami.",
"goal": 100000,
"pledged": 33242,
"state": "live",
"slug": "good-bread-alley",
"disable_communication": false,
"country": "US",
"currency": "USD",
"currency_symbol": "$",
"currency_trailing_code": true,
"deadline": 1399271911,
"state_changed_at": 1396334313,
"created_at": 1393278556,
"launched_at": 1396334311,
"backers_count": 261,
"photo": {
"full": "https://s3.amazonaws.com/ksr/projects/883489/photo-full.jpg?1397869394",
"ed": "https://s3.amazonaws.com/ksr/projects/883489/photo-ed.jpg?1397869394",
"med": "https://s3.amazonaws.com/ksr/projects/883489/photo-med.jpg?1397869394",
"little": "https://s3.amazonaws.com/ksr/projects/883489/photo-little.jpg?1397869394",
"small": "https://s3.amazonaws.com/ksr/projects/883489/photo-small.jpg?1397869394",
"thumb": "https://s3.amazonaws.com/ksr/projects/883489/photo-thumb.jpg?1397869394",
"1024x768": "https://s3.amazonaws.com/ksr/projects/883489/photo-1024x768.jpg?1397869394",
"1536x1152": "https://s3.amazonaws.com/ksr/projects/883489/photo-1536x1152.jpg?1397869394"
},
"creator": {
"id": 749318998,
"name": "April Yvette Thompson",
"avatar": {
"thumb": "https://s3.amazonaws.com/ksr/avatars/9751919/kick_thumb.thumb.jpg?1396128151",
"small": "https://s3.amazonaws.com/ksr/avatars/9751919/kick_thumb.small.jpg?1396128151",
"medium": "https://s3.amazonaws.com/ksr/avatars/9751919/kick_thumb.medium.jpg?1396128151"
},
"urls": {
"web": {
"user": "https://www.kickstarter.com/profile/749318998"
},
"api": {
"user": "https://api.kickstarter.com/v1/users/749318998?signature=1398256877.af4db50c53f93339b05c7813f4534e833eaca270"
}
}
},
"location": {
"id": 2459115,
"name": "New York",
"slug": "new-york-ny",
"short_name": "New York, NY",
"displayable_name": "New York, NY",
"country": "US",
"state": "NY",
"urls": {
"web": {
"discover": "https://www.kickstarter.com/discover/places/new-york-ny",
"location": "https://www.kickstarter.com/locations/new-york-ny"
},
"api": {
"nearby_projects": "https://api.kickstarter.com/v1/discover?signature=1398256786.89b2c4539aeab4ad25982694dd7e659e8c12028f&woe_id=2459115"
}
}
},
"category": {
"id": 17,
"name": "Theater",
"slug": "theater",
"position": 14,
"urls": {
"web": {
"discover": "http://www.kickstarter.com/discover/categories/theater"
}
}
},
"urls": {
"web": {
"project": "https://www.kickstarter.com/projects/749318998/good-bread-alley"
}
}
}
Here is the code I run
#open the json file
require(RJSONIO)
require(rjson)
library("rjson")
filename2 <- "C:/Users/Desktop/in.json"
json_data <- fromJSON(file = filename2)
#unlist the json because it has a problem
unlisted <- unlist(unlist(json_data,recursive=FALSE),recursive=FALSE)
use to fill the NA but as I can understand now it is for already existed nulls http://stackoverflow.com/questions/16947643/getting-imported-json-data-into-a-data-frame-in-r/16948174#16948174
unlisted <- lapply(unlisted, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
})
json <- do.call("rbind", unlisted)
Here is a full list with the columns of the output csv and after that I provide what I would like to keep from every object of json, less columns
id
name
blurb
goal
pledged
state
slug
disable_communication
country
currency
currency_symbol
currency_trailing_code
deadline
state_changed_at
created_at
launched_at
backers_count
photo.full
photo.ed
photo.med
photo.little
photo.small
photo.thumb
photo.1024x768
photo.1536x1152
creator.id
creator.name
creator.slug
creator.avatar.thumb
creator.avatar.small
creator.avatar.medium
creator.urls.web.user
creator.urls.api.user
location.id
location.name
location.slug
location.short_name
location.displayable_name
location.country
location.state
location.urls.web.discover
location.urls.web.location
location.urls.api.nearby_projects
category.id
category.name
category.slug
category.position
category.urls.web.discover
category.urls.web.project
category.urls.web.rewards
Here it is the list of columns I would try to have in the output csv:
id
name
blurb
goal
pledged
state
slug
disable_communication
country
currency
currency_symbol
currency_trailing_code
deadline
state_changed_at
created_at
launched_at
backers_count
creator.id
creator.name
creator.slug
location.id
location.name
location.slug
location.short_name
location.displayable_name
location.country
location.state
category.id
category.name
category.slug
category.position
Looks like there's a very similar question (with answer, though not pure R) here: convert json to csv format
However, since you do seem to want most, if not all, the JSON in a "wide CSV" format you can use fromJSON from jsonlite, rbindlist from data.table (which gets you the fill=TRUE parameter to handle uneven lists nicely) and unlist:
library(jsonlite)
library(data.table)
# tell fromJSON we want a list back
json_data <- fromJSON("in.json", simplifyDataFrame=FALSE)
# iterate over the list we have so we can "flatten" it then
# covert it back to a data.frame-like object
dat <- rbindlist(lapply(json_data, function(x) {
as.list(unlist(x))
}), fill=TRUE)
You may need to tweak column names, but I think this gets you what you're looking for.
I'm trying to get the hourly forecast temperature for a specific hour of the day from the Wunderground API for a research project:
This is an example of the JSON:
http://api.wunderground.com/api/[key]/geolookup/astronomy/forecast/history_/hourly/conditions/q/mo/columbia.json
The specific section looks like this:
"hourly_forecast": [
{
"FCTTIME": {
"hour": "17","hour_padded": "17","min": "00","sec": "0","year": "2013","mon": "7","mon_padded": "07","mon_abbrev": "Jul","mday": "15","mday_padded": "15","yday": "195","isdst": "1","epoch": "1373925600","pretty": "5:00 PM CDT on July 15, 2013","civil": "5:00 PM","month_name": "July","month_name_abbrev": "Jul","weekday_name": "Monday","weekday_name_night": "Monday Night","weekday_name_abbrev": "Mon","weekday_name_unlang": "Monday","weekday_name_night_unlang": "Monday Night","ampm": "PM","tz": "","age": ""
},
"temp": {"english": "86", "metric": "30"},
"dewpoint": {"english": "71", "metric": "22"},
"condition": "Thunderstorm",
"icon": "tstorms",
"icon_url":"http://icons-ak.wxug.com/i/c/k/tstorms.gif",
"fctcode": "15",
"sky": "66",
"wspd": {"english": "10", "metric": "16"},
"wdir": {"dir": "ESE", "degrees": "119"},
"wx": "Scattered Thunderstorms , Scattered Light Rain Showers",
"uvi": "3",
"humidity": "60",
"windchill": {"english": "-9998", "metric": "-9998"},
"heatindex": {"english": "91", "metric": "33"},
"feelslike": {"english": "91", "metric": "33"},
"qpf": {"english": "", "metric": ""},
"snow": {"english": "", "metric": ""},
"pop": "30",
"mslp": {"english": "30.14", "metric": "1020"}
}
,
I get the JSON like so:
$.ajax({
url : "http://api.wunderground.com/api/[key]/geolookup/forecast/hourly/history_/astronomy/conditions/q/"+lat+","+lon+".json",
dataType : "jsonp",
success : function(parsed_json) {
Then I try to run through the hourly forecasts like so (normally mday and hour are replaced with a variable containing today's date and a specific hour, but for troubleshooting, I put these numbers in).
// get the forecast
$.each( parsed_json['hourly_forecast'], function( i, value ) {
if( value.FCTTIME.mday == 15 && value.FCTTIME.hour == 19) {
six_hour_forecast = value.temp.english;
}
However, I consistently get the wrong temp.english for six_hour_forecast.
So close — what am I missing?
This did it:
// Get the forecast 6 hour temp
$.each( parsed_json['hourly_forecast'], function( index, value ) {
if( value['FCTTIME']['hour']==sunrise_hour*1 + 6 && value['FCTTIME']['mday']==window.current_day ) {
window.six_hour_forecast = value.temp.english;
}
}); // end each