2014-01-19 73 views
0

我有這從SugarCRM API返回的可怕的看JSON。最好的方法來扁平json

什麼是清理乾淨,併除去「姓名」鍵,只是有一個鍵和值(無嵌套對象)

如果需要的話,我不會介意使用下劃線庫的最佳方法。

{ 
"assigned_user_name": { 
    "name": "assigned_user_name", 
    "value": "" 
}, 
"modified_by_name": { 
    "name": "modified_by_name", 
    "value": "Website Administrator" 
}, 
"created_by_name": { 
    "name": "created_by_name", 
    "value": "Website Administrator" 
}, 
"id": { 
    "name": "id", 
    "value": "6f9ec13f-dc29-ff18-da36-52d81a0076ad" 
}, 
"name": { 
    "name": "name", 
    "value": " " 
}, 
"date_entered": { 
    "name": "date_entered", 
    "value": "2014-01-16 17:45:33" 
}, 
"date_modified": { 
    "name": "date_modified", 
    "value": "2014-01-16 17:45:33" 
}, 
"modified_user_id": { 
    "name": "modified_user_id", 
    "value": "ab5ff74f-8043-f125-1409-523b6767fca9" 
}, 
"created_by": { 
    "name": "created_by", 
    "value": "ab5ff74f-8043-f125-1409-523b6767fca9" 
}, 
"description": { 
    "name": "description", 
    "value": "" 
}, 
"deleted": { 
    "name": "deleted", 
    "value": "0" 
}, 
"assigned_user_id": { 
    "name": "assigned_user_id", 
    "value": "" 
}, 
"salutation": { 
    "name": "salutation", 
    "value": "" 
}, 
"first_name": { 
    "name": "first_name", 
    "value": "" 
}, 
"last_name": { 
    "name": "last_name", 
    "value": "" 
}, 
"full_name": { 
    "name": "full_name", 
    "value": " " 
}, 
"title": { 
    "name": "title", 
    "value": "" 
}, 
"department": { 
    "name": "department", 
    "value": "" 
}, 
"do_not_call": { 
    "name": "do_not_call", 
    "value": "0" 
}, 
"phone_home": { 
    "name": "phone_home", 
    "value": "" 
}, 
"email": { 
    "name": "email", 
    "value": "" 
}, 
"phone_mobile": { 
    "name": "phone_mobile", 
    "value": "" 
}, 
"phone_work": { 
    "name": "phone_work", 
    "value": "" 
}, 
"phone_other": { 
    "name": "phone_other", 
    "value": "" 
}, 
"phone_fax": { 
    "name": "phone_fax", 
    "value": "" 
}, 
"email1": { 
    "name": "email1", 
    "value": "[email protected]" 
}, 
"email2": { 
    "name": "email2", 
    "value": "" 
}, 
"invalid_email": { 
    "name": "invalid_email", 
    "value": "0" 
}, 
"email_opt_out": { 
    "name": "email_opt_out", 
    "value": "0" 
}, 
"primary_address_street": { 
    "name": "primary_address_street", 
    "value": "" 
}, 
"primary_address_street_2": { 
    "name": "primary_address_street_2", 
    "value": "" 
}, 
"primary_address_street_3": { 
    "name": "primary_address_street_3", 
    "value": "" 
}, 
"primary_address_city": { 
    "name": "primary_address_city", 
    "value": "" 
}, 
"primary_address_state": { 
    "name": "primary_address_state", 
    "value": "" 
}, 
"primary_address_postalcode": { 
    "name": "primary_address_postalcode", 
    "value": "" 
}, 
"primary_address_country": { 
    "name": "primary_address_country", 
    "value": "" 
}, 
"alt_address_street": { 
    "name": "alt_address_street", 
    "value": "" 
}, 
"alt_address_street_2": { 
    "name": "alt_address_street_2", 
    "value": "" 
}, 
"alt_address_street_3": { 
    "name": "alt_address_street_3", 
    "value": "" 
}, 
"alt_address_city": { 
    "name": "alt_address_city", 
    "value": "" 
}, 
"alt_address_state": { 
    "name": "alt_address_state", 
    "value": "" 
}, 
"alt_address_postalcode": { 
    "name": "alt_address_postalcode", 
    "value": "" 
}, 
"alt_address_country": { 
    "name": "alt_address_country", 
    "value": "" 
}, 
"assistant": { 
    "name": "assistant", 
    "value": "" 
}, 
"assistant_phone": { 
    "name": "assistant_phone", 
    "value": "" 
}, 
"email_and_name1": { 
    "name": "email_and_name1", 
    "value": "" 
}, 
"lead_source": { 
    "name": "lead_source", 
    "value": "" 
}, 
"opportunity_role_fields": { 
    "name": "opportunity_role_fields", 
    "value": "" 
}, 
"opportunity_role_id": { 
    "name": "opportunity_role_id", 
    "value": "" 
}, 
"opportunity_role": { 
    "name": "opportunity_role", 
    "value": "" 
}, 
"reports_to_id": { 
    "name": "reports_to_id", 
    "value": "" 
}, 
"report_to_name": { 
    "name": "report_to_name", 
    "value": "" 
}, 
"birthdate": { 
    "name": "birthdate", 
    "value": false 
}, 
"campaign_id": { 
    "name": "campaign_id", 
    "value": "" 
}, 
"campaign_name": { 
    "name": "campaign_name", 
    "value": "" 
}, 
"c_accept_status_fields": { 
    "name": "c_accept_status_fields", 
    "value": "" 
}, 
"m_accept_status_fields": { 
    "name": "m_accept_status_fields", 
    "value": "" 
}, 
"accept_status_id": { 
    "name": "accept_status_id", 
    "value": "" 
}, 
"accept_status_name": { 
    "name": "accept_status_name", 
    "value": "" 
}, 
"sync_contact": { 
    "name": "sync_contact", 
    "value": "" 
}, 
"event_contact_fields": { 
    "name": "event_contact_fields", 
    "value": "" 
}, 
"event_contact_id": { 
    "name": "event_contact_id", 
    "value": "" 
}, 
"event_status": { 
    "name": "event_status", 
    "value": "" 
}, 
"cc_sync": { 
    "name": "cc_sync", 
    "value": "0" 
}, 
"cc_id": { 
    "name": "cc_id", 
    "value": "" 
}, 
"cc_lists": { 
    "name": "cc_lists", 
    "value": "" 
}, 
"cc_lists_view": { 
    "name": "cc_lists_view", 
    "value": "" 
}, 
"cc_optout": { 
    "name": "cc_optout", 
    "value": "0" 
}, 
"mc_expires_c": { 
    "name": "mc_expires_c", 
    "value": false 
}, 
"birthyear_c": { 
    "name": "birthyear_c", 
    "value": "" 
}, 
"contact_id_c": { 
    "name": "contact_id_c", 
    "value": "" 
}, 
"currency_id": { 
    "name": "currency_id", 
    "value": "" 
}, 
"employer_c": { 
    "name": "employer_c", 
    "value": "" 
}, 
"employer_website_c": { 
    "name": "employer_website_c", 
    "value": "" 
}, 
"fico_score_c": { 
    "name": "fico_score_c", 
    "value": "" 
}, 
"household_income_c": { 
    "name": "household_income_c", 
    "value": "" 
}, 
"investment_capital_c": { 
    "name": "investment_capital_c", 
    "value": "" 
}, 
"job_title_c": { 
    "name": "job_title_c", 
    "value": "" 
}, 
"mbrs_company_c": { 
    "name": "mbrs_company_c", 
    "value": "" 
}, 
"membr_type_c": { 
    "name": "membr_type_c", 
    "value": "" 
}, 
"nickname_c": { 
    "name": "nickname_c", 
    "value": "" 
}, 
"num_properties_financed_c": { 
    "name": "num_properties_financed_c", 
    "value": "" 
}, 
"num_properties_owned_c": { 
    "name": "num_properties_owned_c", 
    "value": "" 
}, 
"own_primary_residence_c": { 
    "name": "own_primary_residence_c", 
    "value": "" 
}, 
"parent_id": { 
    "name": "parent_id", 
    "value": "" 
}, 
"parent_name": { 
    "name": "parent_name", 
    "value": "" 
}, 
"parent_type": { 
    "name": "parent_type", 
    "value": "" 
}, 
"planned_retirement_date_c": { 
    "name": "planned_retirement_date_c", 
    "value": false 
}, 
"realestateadvisor_c": { 
    "name": "realestateadvisor_c", 
    "value": "" 
}, 
"referral_c": { 
    "name": "referral_c", 
    "value": "" 
}, 
"sdira_c": { 
    "name": "sdira_c", 
    "value": "" 
}, 
"self_directed_ira_c": { 
    "name": "self_directed_ira_c", 
    "value": "" 
}, 
"self_directed_ira_capital_c": { 
    "name": "self_directed_ira_capital_c", 
    "value": "" 
}, 
"source_c": { 
    "name": "source_c", 
    "value": "" 
}, 
"source_sub_c": { 
    "name": "source_sub_c", 
    "value": "" 
}, 
"spouse_c": { 
    "name": "spouse_c", 
    "value": "" 
}, 
"user_id1_c": { 
    "name": "user_id1_c", 
    "value": "" 
}, 
"user_id_c": { 
    "name": "user_id_c", 
    "value": "" 
}, 
"usigndate_c": { 
    "name": "usigndate_c", 
    "value": "" 
}, 
"usign_c": { 
    "name": "usign_c", 
    "value": "" 
}, 
"longitude_c": { 
    "name": "longitude_c", 
    "value": "" 
}, 
"latitude_c": { 
    "name": "latitude_c", 
    "value": "" 
}, 
"newmemberform_c": { 
    "name": "newmemberform_c", 
    "value": "" 
}, 
"nr_contact_lead_source_c": { 
    "name": "nr_contact_lead_source_c", 
    "value": "" 
}, 
"nr_contact_lead_score_c": { 
    "name": "nr_contact_lead_score_c", 
    "value": "" 
}, 
"nr_contact_recent_activity_c": { 
    "name": "nr_contact_recent_activity_c", 
    "value": "" 
} 
} 
+1

我不明白這有多糟糕,它看起來非常合理。你爲什麼想要做你想做的事? – ejbs

+0

沒有理由,這是多餘的。我希望通過使用object.value來獲取值,而不是object.key.value。 – Eric

回答

3

我只是用一個簡單的方法:

var newArr = {}; 
for(var key in YOUR_JSON_ARRAY) 
    newArr[key] = YOUR_JSON_ARRAY[key].value; 
+0

是的,你是對的。我不知道它是否會影響使用哪一個(數據在兩種情況下都是正確生成的,至少在chrome中),在其他語言中,結果將被稱爲關聯數組,其中[]再次合理。 –

+0

然後,您可以使用點符號來獲取對象屬性的值(即newArr.id) – iii

4

如果你想用下劃線然後reduce將這樣的伎倆:

var o = _(ugly).reduce(function(m, h) { 
    m[h.name] = h.value; 
    return m; 
}, { }); 

演示:http://jsfiddle.net/ambiguous/mE56S/

+0

謝謝!我很新地圖和縮小,並錯誤地嘗試使用地圖功能。無論如何,這是完美的。 – Eric

+1

map-ish迭代器都想要本地處理數組,因爲這對於JavaScript來說很自然。所以,如果你想要一個對象,你幾乎總是使用'reduce',因爲這可以讓你指定返回的東西。這是你的基本經驗法則。 –

0

請記住,vardef數組中的約定是讓數組索引b e字段名稱,但它不是強制性的。我想不出一個他們不一樣的例子,但是你可以用'my_new_field' => array('name'=>'new_field_c')這樣的數組創建一個字段,它可以工作。