0

I am using a local JSON file to get a country name from a country code.

I don't want to read the entire file to a string, just the part that matches to the country code given.

This is the JSON file pattern:

{
    "name": {
        "common": "Austria",
        "official": "Republic of Austria",
        "native": {
            "bar": {
                "official": "Republik Österreich",
                "common": "Österreich"
            }
        }
    },
    "tld": [".at"],
    "cca2": "AT",
    "ccn3": "040",
    "cca3": "AUT",
    "cioc": "AUT",
    "currency": ["EUR"],
    "callingCode": ["43"],
    "capital": "Vienna",
    "altSpellings": ["AT", "Osterreich", "Oesterreich"],
    "region": "Europe",
    "subregion": "Western Europe",
    "languages": {
        "bar": "Austro-Bavarian German"
    },
    "translations": {
        "cym": {"official": "Republic of Austria", "common": "Awstria"},
        "deu": {"official": "Republik Österreich", "common": "Österreich"},
        "fra": {"official": "République d'Autriche", "common": "Autriche"},
        "hrv": {"official": "Republika Austrija", "common": "Austrija"},
        "ita": {"official": "Repubblica d'Austria", "common": "Austria"},
        "jpn": {"official": "オーストリア共和国", "common": "オーストリア"},
        "nld": {"official": "Republiek Oostenrijk", "common": "Oostenrijk"},
        "por": {"official": "República da Áustria", "common": "Áustria"},
        "rus": {"official": "Австрийская Республика", "common": "Австрия"},
        "spa": {"official": "República de Austria", "common": "Austria"}
    },
    "latlng": [47.33333333, 13.33333333],
    "demonym": "Austrian",
    "landlocked": true,
    "borders": ["CZE", "DEU", "HUN", "ITA", "LIE", "SVK", "SVN", "CHE"],
    "area": 83871
}

Any suggestions on how to read only that important bit to a string?

Daniela
  • 471
  • 7
  • 25
  • Without having to change this much? Just give in and leave it as is. The builtin JSON parser loads everything in memory. If you don't want that you need to find or write another JSON parser. – Álvaro González Sep 28 '16 at 14:28
  • 1
    "I don't want to read the entire file to a string" — While you could, in theory, write a custom parser that reads it token by token … that would be a **really** complicated approach to the problem. You would have to be ludicrously memory constrained for that to be worthwhile … in which case you wouldn't be using PHP for this! – Quentin Sep 28 '16 at 14:41

0 Answers0