[R] How to save multiple values of a variable in a json file in R

Richard O'Keefe r@oknz @end|ng |rom gm@||@com
Sat Jan 18 12:17:33 CET 2020


In your example, these keys occur
   1  countries
   1  maximum_im
   1  minimum_im
 61  name
 66  pk           -- many of these pertain to countries, some do not
   1  taxonomy_gem
It is not clear what you mean by 'the name of the countries'.
There appear to be 61 countries each with two 'names'.
I am guessing that you want the 'name' slots, not the locale-
independent 3-letter ISO codes.
So 'name' occurs once per country.
'maximum_im' and 'minimum_im' occur once per predictor_var?
And 'taxonomy_gem' appears to occur once per dataset.
Now in a data frame, every variable occurs the same number of times
(once per row = observation = case = thingy).
So, leaving aside the issue of parsing JSON, suppose someone
gave you
 - a vector of N country Names
 - a vector of P maximum_im values for Predictors
 - a vector of P minimum_im values for Predictors
 - a vector of T taxonomy_gems
How do you intend to fit these into a data frame?

On Fri, 17 Jan 2020 at 04:03, Ioanna Ioannou <ii54250 using msn.com> wrote:
>
> ok, my problem is the follwoing
> a<- read_json(json_file)
> a$fields$geo_applicability$fields$countries[[1]]$fields$name
>
> this was i can see the name of a single country reported first in the json. HOwever, i  need to save all 59. My worry is that i have multiple files that i need to read and in some cases there will be one country reported and in some others multiple. How can i optimise the code?
>
> Best,
>
> ________________________________
> From: Rainer M Krug <Rainer using krugs.de>
> Sent: 16 January 2020 14:47
> To: Ioanna Ioannou <ii54250 using msn.com>
> Subject: Re: [R] How to save multiple values of a variable in a json file in R
>
> Check the hep of the read_json() function and please keep the conversation on the list
>
> On 16 Jan 2020, at 15:42, Ioanna Ioannou <ii54250 using msn.com<mailto:ii54250 using msn.com>> wrote:
>
> I will try the package but i could do with a worked example.
>
> ________________________________
> From: Rainer M Krug <Rainer using krugs.de<mailto:Rainer using krugs.de>>
> Sent: 16 January 2020 14:29
> To: Ioanna Ioannou <ii54250 using msn.com<mailto:ii54250 using msn.com>>
> Subject: Re: [R] How to save multiple values of a variable in a json file in R
>
> Have you looked at the jsonlite package?
>
> Rainer
>
>
> On 16 Jan 2020, at 15:21, Ioanna Ioannou <ii54250 using msn.com<mailto:ii54250 using msn.com>> wrote:
>
> hello everyone,
>
> and happy new year!
>
> I have this problem: I want to save the name of the 'countries', the 'taxonomy_gem' and the 'minimum_im' and 'maximum_im' . The problem is that there are several names of countries.  How can i transfer the information from the json file to an R data.frame? See below for the json file.
>
> Best,
> ioanna
>
>
>
> json_file<- {
>    "pk": 670,
>    "model": "vulnerability.generalinformation",
>    "fields": {
>        "category": "Structure class",
>        "article_title": "A GLOBAL DATABASE OF VULNERABILITY MODELS FOR SEISMIC RISK ASSESSMENT",
>        "name": "CR/LFINF/DUL/H:2",
>        "publication_conference_name": "16EECE",
>        "llrs": null,
>        "material": null,
>        "web_link": "",
>        "owner": {
>            "pk": 1900,
>            "model": "people.profile",
>            "fields": {
>                "username": "lmartins",
>                "first_name": "",
>                "last_name": "",
>                "email": "luis.martins using globalquakemodel.org<mailto:luis.martins using globalquakemodel.org>"
>            }
>        },
>        "general_comments": "",
>        "geo_applicability": {
>            "pk": 669,
>            "model": "vulnerability.geoapplicability",
>            "fields": {
>                "general_information": 670,
>                "area": "",
>                "countries": [
>                    {
>                        "pk": "CIV",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Cte d'Ivoire"
>                        }
>                    },
>                    {
>                        "pk": "CMR",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Cameroon"
>                        }
>                    },
>                    {
>                        "pk": "SDN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Sudan"
>                        }
>                    },
>                    {
>                        "pk": "SSD",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "South Sudan"
>                        }
>                    },
>                    {
>                        "pk": "TUN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Tunisia"
>                        }
>                    },
>                    {
>                        "pk": "TGO",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Togo"
>                        }
>                    },
>                    {
>                        "pk": "ZAF",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "South Africa"
>                        }
>                    },
>                    {
>                        "pk": "NER",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Niger"
>                        }
>                    },
>                    {
>                        "pk": "MAR",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Morocco"
>                        }
>                    },
>                    {
>                        "pk": "KEN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Kenya"
>                        }
>                    },
>                    {
>                        "pk": "SYC",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Seychelles"
>                        }
>                    },
>                    {
>                        "pk": "ESH",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Western Sahara"
>                        }
>                    },
>                    {
>                        "pk": "SHN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Saint Helena"
>                        }
>                    },
>                    {
>                        "pk": "REU",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Reunion"
>                        }
>                    },
>                    {
>                        "pk": "SLE",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Sierra Leone"
>                        }
>                    },
>                    {
>                        "pk": "DJI",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Djibouti"
>                        }
>                    },
>                    {
>                        "pk": "STP",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Sao Tome and Principe"
>                        }
>                    },
>                    {
>                        "pk": "BDI",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Burundi"
>                        }
>                    },
>                    {
>                        "pk": "MLI",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Mali"
>                        }
>                    },
>                    {
>                        "pk": "GMB",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Gambia"
>                        }
>                    },
>                    {
>                        "pk": "ZMB",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Zambia"
>                        }
>                    },
>                    {
>                        "pk": "RWA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Rwanda"
>                        }
>                    },
>                    {
>                        "pk": "MWI",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Malawi"
>                        }
>                    },
>                    {
>                        "pk": "BFA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Burkina Faso"
>                        }
>                    },
>                    {
>                        "pk": "GAB",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Gabon"
>                        }
>                    },
>                    {
>                        "pk": "TCD",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Chad"
>                        }
>                    },
>                    {
>                        "pk": "SEN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Senegal"
>                        }
>                    },
>                    {
>                        "pk": "NAM",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Namibia"
>                        }
>                    },
>                    {
>                        "pk": "ATF",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "French Southern Territories"
>                        }
>                    },
>                    {
>                        "pk": "BWA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Botswana"
>                        }
>                    },
>                    {
>                        "pk": "TZA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Tanzania"
>                        }
>                    },
>                    {
>                        "pk": "ERI",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Eritrea"
>                        }
>                    },
>                    {
>                        "pk": "MYT",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Mayotte"
>                        }
>                    },
>                    {
>                        "pk": "SOM",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Somalia"
>                        }
>                    },
>                    {
>                        "pk": "UGA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Uganda"
>                        }
>                    },
>                    {
>                        "pk": "LSO",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Lesotho"
>                        }
>                    },
>                    {
>                        "pk": "LBY",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Libya"
>                        }
>                    },
>                    {
>                        "pk": "LBR",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Liberia"
>                        }
>                    },
>                    {
>                        "pk": "COD",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Democratic Republic of the Congo"
>                        }
>                    },
>                    {
>                        "pk": "CPV",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Cape Verde"
>                        }
>                    },
>                    {
>                        "pk": "CAF",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Central African Republic"
>                        }
>                    },
>                    {
>                        "pk": "COM",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Comoros"
>                        }
>                    },
>                    {
>                        "pk": "ETH",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Ethiopia"
>                        }
>                    },
>                    {
>                        "pk": "GIN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Guinea"
>                        }
>                    },
>                    {
>                        "pk": "COG",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Republic of Congo"
>                        }
>                    },
>                    {
>                        "pk": "GHA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Ghana"
>                        }
>                    },
>                    {
>                        "pk": "SWZ",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Swaziland"
>                        }
>                    },
>                    {
>                        "pk": "GNB",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Guinea-Bissau"
>                        }
>                    },
>                    {
>                        "pk": "MDG",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Madagascar"
>                        }
>                    },
>                    {
>                        "pk": "ZWE",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Zimbabwe"
>                        }
>                    },
>                    {
>                        "pk": "MOZ",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Mozambique"
>                        }
>                    },
>                    {
>                        "pk": "MRT",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Mauritania"
>                        }
>                    },
>                    {
>                        "pk": "MUS",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Mauritius"
>                        }
>                    },
>                    {
>                        "pk": "NGA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Nigeria"
>                        }
>                    },
>                    {
>                        "pk": "BEN",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Benin"
>                        }
>                    },
>                    {
>                        "pk": "GNQ",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Equatorial Guinea"
>                        }
>                    },
>                    {
>                        "pk": "EGY",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Egypt"
>                        }
>                    },
>                    {
>                        "pk": "AGO",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Angola"
>                        }
>                    },
>                    {
>                        "pk": "DZA",
>                        "model": "vulnerability.country",
>                        "fields": {
>                            "is_visible": true,
>                            "region": 2,
>                            "name": "Algeria"
>                        }
>                    }
>                ],
>                "lon": null,
>                "address": "",
>                "lat": null
>            }
>        },
>        "authors": "Martins and Silva",
>        "use_case_information": "",
>        "structure_type": "Building",
>        "taxonomy_gem": "DX+D99/MAT99/L99/DY+D99/MAT99/L99/H99/Y99/OC99/BP99/PLF99/IR99/EW99/RSH99+RMT99+R99+RWC99/F99+FWC99/FOS99",
>        "year": 2018,
>        "type_of_assessment": "Vulnerability",
>        "taxonomy_type": {
>            "pk": 1,
>            "model": "vulnerability.taxonomytype",
>            "fields": {
>                "user_def": false,
>                "name": "GEM"
>            }
>        },
>        "taxonomy_text": "",
>        "vulnerability_func": {
>            "pk": 30,
>            "model": "vulnerability.vulnerabilityfunc",
>            "fields": {
>                "general_information": 670,
>                "predictor_var": {
>                    "pk": 602,
>                    "model": "vulnerability.predictorvar",
>                    "fields": {
>                        "type_of_period": "Telastic (s)",
>                        "minimum_im": 0.05,
>                        "intensity_measure_type": "Sa(T)",
>                        "fragility_func": null,
>                        "period": 0.3,
>                        "vulnerability_func": 30,
>                        "maximum_im": 8,
>                        "intensity_measure_unit": "g"
>                    }
>                },
>                "func_distr_vuln_discr": {
>                    "pk": 23,
>                    "model": "vulnerability.funcdistrvulndiscr",
>                    "fields": {
>                        "resp_var_mean_val": "0.000025;0.000033;0.000045;0.000059;0.000078;0.000101;0.000131;0.00017;0.000217;0.000277;0.00035;0.00044;0.000551;0.000684;0.000844;0.001036;0.001264;0.001532;0.001846;0.002212;0.002635;0.003121;0.003676;0.004306;0.005016;0.005812;0.006701;0.007685;0.008772;0.009966;0.01127;0.012691;0.014233;0.0159;0.0177;0.019639;0.021726;0.023972;0.026388;0.028991;0.0318;0.034838;0.038132;0.041714;0.04562;0.049893;0.05458;0.059733;0.065409;0.07167;0.078582;0.086213;0.094633;0.103913;0.114125;0.125335;0.13761;0.151007;0.165578;0.181366;0.1984;0.216699;0.236266;0.257091;0.279144;0.302379;0.326733;0.352124;0.378455;0.40561;0.433459;0.461862;0.490663;0.519703;0.548813;0.577824;0.606566;0.634874;0.662589;0.68956;0.715649;0.740732;0.764702;0.787467;0.808956;0.829114;0.847907;0.865317;0.881347;0.896012;0.909344;0.921388;0.932199;0.941842;0.950388;0.957913;0.964497;0.97022;0.975163;0.979405",
>                        "resp_var_val_coeff": "",
>                        "predictor_var_im_val": "0.05;0.05263;0.055398;0.058312;0.06138;0.064608;0.068007;0.071584;0.07535;0.079313;0.083485;0.087876;0.092499;0.097364;0.102486;0.107877;0.113551;0.119524;0.125811;0.132429;0.139395;0.146727;0.154445;0.162569;0.171121;0.180122;0.189596;0.199569;0.210067;0.221117;0.232748;0.24499;0.257877;0.271442;0.28572;0.300749;0.316569;0.333221;0.350749;0.369198;0.388619;0.40906;0.430577;0.453226;0.477066;0.502161;0.528575;0.556379;0.585645;0.61645;0.648876;0.683008;0.718935;0.756752;0.796558;0.838458;0.882561;0.928985;0.977851;1.029287;1.083429;1.140418;1.200405;1.263548;1.330012;1.399972;1.473613;1.551126;1.632717;1.7186;1.809;1.904156;2.004317;2.109746;2.220721;2.337534;2.460491;2.589915;2.726148;2.869546;3.020488;3.179369;3.346607;3.522643;3.707938;3.902979;4.108281;4.324381;4.551848;4.791281;5.043307;5.308591;5.587829;5.881755;6.191142;6.516804;6.859595;7.220418;7.60022;8",
>                        "vulnerability_func": 30,
>                        "func_distr_shape": null,
>                        "data_pts_num": 100
>                    }
>                },
>                "method_of_estimation": "Analytical",
>                "func_distr_type": "Discrete",
>                "resp_var": "Damage factor"
>            }
>        }
>    }
> }
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
> --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)
>
> Orcid ID: 0000-0002-7490-0066
>
> Department of Evolutionary Biology and Environmental Studies
> University of Zürich
> Office Y34-J-74
> Winterthurerstrasse 190
> 8075 Zürich
> Switzerland
>
> Office: +41 (0)44 635 47 64
> Cell:        +41 (0)78 630 66 57
> email:      Rainer.Krug using uzh.ch<mailto:Rainer.Krug using uzh.ch>
> Rainer using krugs.de<mailto:Rainer using krugs.de>
> Skype:     RMkrug
>
> PGP: 0x0F52F982
>
> --
> Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)
>
> Orcid ID: 0000-0002-7490-0066
>
> Department of Evolutionary Biology and Environmental Studies
> University of Zürich
> Office Y34-J-74
> Winterthurerstrasse 190
> 8075 Zürich
> Switzerland
>
> Office: +41 (0)44 635 47 64
> Cell:        +41 (0)78 630 66 57
> email:      Rainer.Krug using uzh.ch<mailto:Rainer.Krug using uzh.ch>
> Rainer using krugs.de<mailto:Rainer using krugs.de>
> Skype:     RMkrug
>
> PGP: 0x0F52F982
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list