Aiswarya Krishna
Aiswarya Krishna

Reputation: 89

Convert JSON array into individual variables using javascript

I have the following JSON array that is output from an API.

{
    "location": {
        "name": "Alanallur",
        "region": "Kerala",
        "country": "India",
        "lat": 11.01,
        "lon": 76.33,
        "tz_id": "Asia/Kolkata",
        "localtime_epoch": 1470998311,
        "localtime": "2016-08-12 10:38"
    },
    "current": {
        "last_updated_epoch": 1470997826,
        "last_updated": "2016-08-12 10:30",
        "temp_c": 28.0,
        "temp_f": 82.4,
        "is_day": 1,
        "condition": {
            "text": "Moderate rain",
            "icon": "//cdn.apixu.com/weather/64x64/day/302.png",
            "code": 1189
        },
        "wind_mph": 8.1,
        "wind_kph": 13.0,
        "wind_degree": 340,
        "wind_dir": "NNW",
        "pressure_mb": 1013.0,
        "pressure_in": 30.4,
        "precip_mm": 0.0,
        "precip_in": 0.0,
        "humidity": 79,
        "cloud": 0,
        "feelslike_c": 32.2,
        "feelslike_f": 89.9
    }
}

I want to split this array into separate variables using javascript

Upvotes: 0

Views: 1497

Answers (4)

Alok Patel
Alok Patel

Reputation: 8022

This is how you can get country and it's temparutre in separate JS variables.

var jsonobject=JSON.parse(json_string); // don't do this if you already have JSON Object instead of string.
var country=jsonobject.location.country. // Where location is another JSON Object
var temprature_c=jsonobject.current.temp_c; // current is another JSON Object
var temprature_f=jsonobject.current.temp_f; // current is another JSON Object

See this to debug your JSON. http://json-parser.com/6d51ab22

Where { } is a literal notation for an Object, access any property inside { } with . operator,
and [ ] is a literal notation for an Array. You can access array elements just like normal JS array using indices (e.g [0], [1] etc...)

Upvotes: 1

Aman Maurya
Aman Maurya

Reputation: 1325

Try this code : Use developer console to view the output in browser

<script>
var str = '{"location":{"name":"Alanallur","region":"Kerala","country":"India","lat":11.01,"lon":76.33,"tz_id":"Asia/Kolkata","localtime_epoch":1470998311,"localtime":"2016-08-12 10:38"},"current":{"last_updated_epoch":1470997826,"last_updated":"2016-08-12 10:30","temp_c":28,"temp_f":82.4,"is_day":1,"condition":{"text":"Moderate rain","icon":"//cdn.apixu.com/weather/64x64/day/302.png","code":1189},"wind_mph":8.1,"wind_kph":13,"wind_degree":340,"wind_dir":"NNW","pressure_mb":1013,"pressure_in":30.4,"precip_mm":0,"precip_in":0,"humidity":79,"cloud":0,"feelslike_c":32.2,"feelslike_f":89.9}}';

if(typeof(str) == 'string'){
        str = JSON.parse(str);
        console.log(str.current)//return object
        console.log(str.location)//return object
        console.log(str.current.temp_c)//return string
        console.log(str.current.temp_f)//return string
        console.log(str.location.country)//return string
}
</script>

JSON.parse(str) is used to convert string to object

enter image description here

Upvotes: 1

Haresh Vidja
Haresh Vidja

Reputation: 8496

if this response is in data

check alert(typeof data) // it will show object or string

if it is not object then

data= JSON.parse(data)

now you can access it as saperate variable

if you want to access localtime of location then

alert(data.location.localtime)

I hope it make sense for you Check two example on jsfiddle

https://jsfiddle.net/5ov827oc/1/

https://jsfiddle.net/5ov827oc/2/

Upvotes: 1

Aravind Pillai
Aravind Pillai

Reputation: 771

Json Code

{
posts:[
   "location": {
        "name": "Alanallur",
        "region": "Kerala",
        "country": "India",
        "lat": 11.01,
        "lon": 76.33,
        "tz_id": "Asia/Kolkata",
        "localtime_epoch": 1470998311,
        "localtime": "2016-08-12 10:38"
    },
    "current": {
        "last_updated_epoch": 1470997826,
        "last_updated": "2016-08-12 10:30",
        "temp_c": 28.0,
        "temp_f": 82.4,
        "is_day": 1,
        "condition": {
            "text": "Moderate rain",
            "icon": "//cdn.apixu.com/weather/64x64/day/302.png",
            "code": 1189
        },
        "wind_mph": 8.1,
        "wind_kph": 13.0,
        "wind_degree": 340,
        "wind_dir": "NNW",
        "pressure_mb": 1013.0,
        "pressure_in": 30.4,
        "precip_mm": 0.0,
        "precip_in": 0.0,
        "humidity": 79,
        "cloud": 0,
        "feelslike_c": 32.2,
        "feelslike_f": 89.9
    }
}

In javascript use

 json.parse()

PHP Code

<?php
// copy file content into a string var
$json_file = file_get_contents('posts.json');
// convert the string to a json object
$jfo = json_decode($json_file);
// read the title value
$title = $jfo->result->title;
// copy the posts array to a php var
$posts = $jfo->result->posts;
// listing posts
foreach ($posts as $post) {
    echo $post->title;
}
?>

Hope this helps. :)

Upvotes: 1

Related Questions