dpqaaczn141761 2015-07-28 22:46
浏览 22
已采纳

Socrata consumerfinance.gov complaintdatabase API JSON解析

I want to retrieve consumerfinance.gov complaintdatabase data using the Socrata API.

This is straightforward to retrieve the data using the instructions on the site at http://www.consumerfinance.gov/complaintdatabase/technical-documentation/#api-documentation

I used the following query with '6yuf-367p' to get just 'prepaid card' product data and the '.json' tag to get it in JSON format:

http://data.consumerfinance.gov/api/views/6yuf-367p/rows.json

I used PHP to retrieve data with this query:

$url = "http://data.consumerfinance.gov/api/views/6yuf-367p/rows.json";
$json = file_get_contents($url);
$data = json_decode($json);
var_dump($data); 

The results are paraphrased below. A couple of things about the results that are different than what I was expecting.

  1. I wasn't expecting the 'Meta' section. The column names are in the Meta section not associated directly with the data in { key: value } format.

  2. I was expecting the Data section to have { key: value } format instead of just 'values'. This is different than the format described on Socrata help page at http://dev.socrata.com/docs/formats/json.html

I am not advanced javascript developer so I am wondering how best to proceed. Ideally I want only the 'Data' section with column names in the { key: value } format. I wanted it in that { key: value } format to use with things like Google Charts.

I am imaging I would have to save the column names in array and then loop through each data row and rewrite the data rows with column names included to get { key: value } format.

My other option would be to use the csv API format which is super clean with nice clean columns and no Meta section. But then I would have to convert the csv to JSON which seems unnecessary as the JSON is available.

CSV query is this:

http://data.consumerfinance.gov/api/views/6yuf-367p/rows.csv

So couple of questions:

  1. Does socrata provide an API feed without the 'Meta' section? Is there a filter I can use to exclude the 'Meta' section?

  2. If answer to #1 is no, does Socrata have ready javascript to parse the JSON to get the 'Data' section in { key: value } format similar format as described on Socrata help page?

Thanks!

    {
    "meta" : {
    "view" : {
    "id" : "6yuf-367p",
    "name" : "Prepaid Card Complaints",
    "averageRating" : 0,
    "createdAt" : 1434039311,

     etc etc

    "columns" : [ {
    "id" : -1,
    "name" : "sid",
    "dataTypeName" : "meta_data",
    "fieldName" : ":sid",
    "position" : 0,
    "renderTypeName" : "meta_data",
    "format" : {
    }
    }, {

    etc etc

    "data" : [ [ 208134, "A7A3941C-A764-44CA-ABC0-66DE814F1969", 208134, 1438091214, "924763", 1438091214, "924763", null, "2015-07-13T00:00:00", "Prepaid card", "General purpose card", "Managing, opening, or closing account", null, null, null, "Amex", "WA", "982XX", "Web", "2015-07-19T00:00:00", "Closed with monetary relief", "Yes", null, "1464043" ]

    ......

    ]
    }
  • 写回答

1条回答 默认 最新

  • douluhaikao93943 2015-07-28 23:24
    关注

    It looks like you grabbed the wrong JSON URL. The one you grabbed is for the JSON export, which will dump you the entire dataset in a JSON format along with all of the metadata, and it doesn't provide a queryable API endpoint.

    Instead, you should use the endpoint https://data.consumerfinance.gov/resource/jhzv-w97w.json. That'll get you the {"key" : "value"} pairs you're looking for, like this:

    [ {
      "zip_code" : "982XX",
      "complaint_id" : "1464043",
      "issue" : "Managing, opening, or closing account",
      "date_received" : "2015-07-13T00:00:00",
      "state" : "WA",
      "product" : "Prepaid card",
      "company_response" : "Closed with monetary relief",
      "company" : "Amex",
      "submitted_via" : "Web",
      "date_sent_to_company" : "2015-07-19T00:00:00",
      "sub_product" : "General purpose card",
      "timely" : "Yes"
    }, ... ]
    

    You can also view API docs for that dataset at: http://dev.socrata.com/foundry/#/data.consumerfinance.gov/jhzv-w97w

    Good luck, and let me know if you have more questions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛