-
-
Save DiegoPino/65d775d8bd40320e7ba6ff3307fcfe26 to your computer and use it in GitHub Desktop.
JSON Property keys flattener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?PHP | |
CONST JSON_INPUT = ' | |
{ | |
"@context": { | |
"ore": "http://www.openarchives.org/ore/terms/", | |
"skos": "http://www.w3.org/2004/02/skos/core#", | |
"dc": "http://purl.org/dc/elements/1.1/", | |
"edm": "http://www.europeana.eu/schemas/edm/", | |
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", | |
"dcterms": "http://purl.org/dc/terms/", | |
"foaf": "http://xmlns.com/foaf/0.1/", | |
"geo": "http://www.w3.org/2003/01/geo/wgs84_pos#" | |
}, | |
"@graph": [ | |
{ | |
"@id": "http://data.europeana.eu/aggregation/europeana/09102/_CM_0839888", | |
"@type": "edm:EuropeanaAggregation", | |
"dc:creator": "Europeana", | |
"edm:aggregatedCHO": { | |
"@id": "http://data.europeana.eu/item/09102/_CM_0839888" | |
}, | |
"edm:collectionName": "09102_Ag_EU_MIMO_ESE", | |
"edm:country": "Europe", | |
"edm:landingPage": { | |
"@id": "http://www.europeana.eu/portal/record/09102/_CM_0839888.html" | |
}, | |
"edm:language": "mul", | |
"edm:rights": { | |
"@id": "http://creativecommons.org/licenses/by-nc-sa/3.0/" | |
} | |
}, | |
{ | |
"@id": "http://data.europeana.eu/aggregation/provider/09102/_CM_0839888", | |
"@type": "ore:Aggregation" | |
}, | |
{ | |
"@id": "http://data.europeana.eu/item/09102/_CM_0839888", | |
"@type": "edm:ProvidedCHO" | |
}, | |
{ | |
"@id": "http://data.europeana.eu/proxy/europeana/09102/_CM_0839888", | |
"@type": "ore:Proxy" | |
}, | |
{ | |
"@id": "http://data.europeana.eu/proxy/provider/09102/_CM_0839888", | |
"@type": "ore:Proxy" | |
}, | |
{ | |
"@id": "http://mediatheque.cite-musique.fr/masc/play.asp?ID=0839888", | |
"@type": "edm:WebResource" | |
}, | |
{ | |
"@id": "http://semium.org/time/1910", | |
"@type": "edm:TimeSpan" | |
}, | |
{ | |
"@id": "http://semium.org/time/19xx_1_third", | |
"@type": "edm:TimeSpan" | |
}, | |
{ | |
"@id": "http://sws.geonames.org/2950159", | |
"@type": "edm:Place" | |
}, | |
{ | |
"@id": "http://www.geonames.org/2950159", | |
"@type": "edm:Place" | |
}, | |
{ | |
"@id": "http://www.mimo-db.eu/InstrumentsKeywords/4495", | |
"@type": "skos:Concept" | |
}, | |
{ | |
"@id": "http://www.mimo-db.eu/media/MF-GET/IMAGE/MFIM000024482.jpg", | |
"@type": "edm:WebResource" | |
} | |
] | |
} | |
'; | |
CONST JSON_PERSON = ' | |
{ | |
"@context": "http://schema.org", | |
"@type": "Person", | |
"address": { | |
"@type": "PostalAddress", | |
"addressLocality": "Colorado Springs", | |
"addressRegion": "CO", | |
"postalCode": "80840", | |
"streetAddress": "100 Main Street" | |
}, | |
"colleague": [ | |
"http://www.example.com/JohnColleague.html", | |
"http://www.example.com/JameColleague.html" | |
], | |
"email": "mailto:info@example.com", | |
"image": "janedoe.jpg", | |
"jobTitle": "Research Assistant", | |
"name": "Jane Doe", | |
"alumniOf": "Dartmouth", | |
"birthPlace": "Philadelphia, PA", | |
"birthDate": "1979.10.12", | |
"height": "72 inches", | |
"gender": "female", | |
"memberOf": "Republican Party", | |
"nationality": "African American", | |
"telephone": "(123) 456-6789", | |
"url": "http://www.example.com", | |
"sameAs" : [ "https://www.facebook.com/", | |
"https://www.linkedin.com/", | |
"http://twitter.com/", | |
"http://instagram.com/", | |
"https://plus.google.com/"] | |
} | |
'; | |
CONST MANY_NODES =' | |
{ | |
"@context": { | |
"foaf": "http://xmlns.com/foaf/0.1/" | |
}, | |
"@graph": [ | |
{ | |
"@type": "foaf:Person", | |
"foaf:homepage": "http://example.com/bob/", | |
"foaf:name": "Bob" | |
}, { | |
"@type": "foaf:Person", | |
"foaf:homepage": "http://example.com/eve/", | |
"foaf:name": "Eve" | |
}, { | |
"@type": "foaf:Person", | |
"foaf:homepage": "http://example.com/manu/", | |
"foaf:name": "Manu" | |
} | |
] | |
}'; | |
CONST NAMED_GRAPH = '{ | |
"@context": { | |
"generatedAt": { | |
"@id": "http://www.w3.org/ns/prov#generatedAtTime", | |
"@type": "http://www.w3.org/2001/XMLSchema#date" | |
}, | |
"Person": "http://xmlns.com/foaf/0.1/Person", | |
"name": "http://xmlns.com/foaf/0.1/name", | |
"knows": {"@id": "http://xmlns.com/foaf/0.1/knows", "@type": "@id"} | |
}, | |
"@id": "http://example.org/foaf-graph", | |
"generatedAt": "2012-04-09", | |
"@graph": [ | |
{ | |
"@id": "http://manu.sporny.org/about#manu", | |
"@type": "Person", | |
"name": "Manu Sporny", | |
"knows": "http://greggkellogg.net/foaf#me" | |
}, { | |
"@id": "http://greggkellogg.net/foaf#me", | |
"@type": "Person", | |
"name": "Gregg Kellogg", | |
"knows": "http://manu.sporny.org/about#manu" | |
} | |
] | |
}'; | |
function jsonIsList(array $sourcearray = []) { | |
return empty(array_filter(array_keys($sourcearray), 'is_string')); | |
} | |
function array_flatten($array, $prefix = '') { | |
$result = array(); | |
foreach ($array as $key=>$value) { | |
if (is_array($value)) { | |
$result = $result + array_flatten($value, $prefix.$key.'.'); | |
} | |
else { | |
$result[$prefix.$key] = $value; | |
} | |
} | |
return $result; | |
} | |
function array_flatten2($array, &$flat = array(), $jsonld = TRUE) { | |
if (($jsonld) && array_key_exists('@graph', $array)) { | |
$array = $array['@graph']; | |
} | |
foreach ($array as $key=>$value) { | |
if (is_array($value)) { | |
if (jsonIsList($value)) { | |
print_r('numeric index'); | |
} | |
array_flatten2($value, $flat); | |
} | |
else { | |
$flat[$key][] = $value; | |
} | |
} | |
return $flat; | |
} | |
print_r(json_decode(NAMED_GRAPH, TRUE)); | |
print_r(array_flatten(json_decode(NAMED_GRAPH, TRUE))); | |
$flat = array(); | |
print_r(array_flatten2(json_decode(NAMED_GRAPH, TRUE),$flat)); | |
print_r($flat); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment