Pandas json_normalize with nested JSON - json

I have tried many alternatives and the better result was with this statement:
json_normalize(a['solution'][0]['tour'])
I can see just one block of tours at a time. vehicle_id 0 information and I need them all together. Thank you.
The JSON is:
{
"total_clusters": 6,
"solution": [
{
"vehicles_id": "0",
"vehicles_location": {
"lat": "",
"lng": ""
},
"tour": [
{
"shipping_id": "4a4b0750-63a7-11ea-8955-43fcb2cd860a",
"type": "dropoff",
"location_id": "797",
"coordinates": {
"lat": "-34.545736",
"lng": "-58.488340"
},
"cluster": 0
},
{
"shipping_id": "75e5a2c0-6314-11ea-b657-ddd473c629a3",
"type": "dropoff",
"location_id": "114",
"coordinates": {
"lat": "-34.568707",
"lng": "-58.452963"
},
"cluster": 0
}
]
},
{
"vehicles_id": "1",
"vehicles_location": {
"lat": "",
"lng": ""
},
"tour": [
{
"shipping_id": "c83ac7c0-51c4-11ea-9aef-973de7785221",
"type": "pickup",
"location_id": "687",
"coordinates": {
"lat": "-34.592824",
"lng": "-58.375457"
},
"cluster": 1
},
{
"shipping_id": "b5a295c0-51c4-11ea-b36d-651ee769ca89",
"type": "pickup",
"location_id": "687",
"coordinates": {
"lat": "-34.592824",
"lng": "-58.375457"
},
"cluster": 1
}
]
}
]
}
Desired Output

You need to pass the record_path parameter to json_normalize.
From the docs:
record_path : str or list of str, default None
Path in each object to list of records. If not passed, data will be assumed to be an array of records.
import pandas as pd
import json
raw_json_data = """{contents_of_your_json_here}"""
json_data = json.loads(raw_json_data)
df = pd.json_normalize(json_data, ["solution", "tour"])
Result:

Related

2 Nested Dictionaries Json format using Json_norminalize using python

Hi I am trying to retrieve this data, there are 2 nested dictionaries within it :
{
"metadata": {
"stations": [
{
"id": "S108",
"device_id": "S108",
"name": "Kuala Lumpur",
"location": {
"latitude": 3.1390,
"longitude": 101.6869
}
},
{
"id": "S118",
"device_id": "S118",
"name": "Bukit Bintang",
"location": {
"latitude":3.1468,
"longitude": 101.7113
}
}
],
"reading_type": "DBT 1M F",
"reading_unit": "deg C"
},
"items": [
{
"timestamp": "2021-06-20T15:05:00+08:00",
"readings": [
{
"station_id": "S108",
"value": 32.6
},
{
"station_id": "S118",
"value": 30.3
}
]
}
]
I wanted to get the result like this :
Result
I have tried a few ways :
data = airtemp.json()
df = pd.json_normalize(data,record_path=['metadata', 'stations'])
df
data = airtemp.json()
df1 = pd.json_normalize(data,record_path=['items','readings'])
df1
Is there a way that I can use json_norminalize to form one table with station_id, name, latitude, longtitude, timestamp and value without breaking into 2 tables ?
Many thanks!
You can merge the two dataframes you created:
import pandas as pd
data = {
"metadata": {
"stations": [
{
"id": "S108",
"device_id": "S108",
"name": "Kuala Lumpur",
"location": {
"latitude": 3.1390,
"longitude": 101.6869
}
},
{
"id": "S118",
"device_id": "S118",
"name": "Bukit Bintang",
"location": {
"latitude": 3.1468,
"longitude": 101.7113
}
}
],
"reading_type": "DBT 1M F",
"reading_unit": "deg C"
},
"items": [
{
"timestamp": "2021-06-20T15:05:00+08:00",
"readings": [
{
"station_id": "S108",
"value": 32.6
},
{
"station_id": "S118",
"value": 30.3
}
]
}
]
}
stations = pd.json_normalize(data, record_path=['metadata', 'stations'])
readings = pd.json_normalize(data, record_path=['items', 'readings'])
result = stations.merge(readings, left_on='id', right_on='station_id')
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print(result)
Outputs:
id device_id name location.latitude location.longitude \
0 S108 S108 Kuala Lumpur 3.1390 101.6869
1 S118 S118 Bukit Bintang 3.1468 101.7113
station_id value
0 S108 32.6
1 S118 30.3
There is only one timestamp in the data you provided so you will have to fetch that separately.

Malformed json fixing in pyspakr

I have a json file which i recieved from source in below format.
{
"api_version": "1.3",
"response_code": "200",
"id": "3237490513229753",
"lon": "38.969916127827",
"lat": "45.069889625267",
"page_url": null,
"name": "ATB",
"firm_group": {
"id": "3237499103085728",
"count": "1"
},
"city_name": "Krasnodar",
"city_id": "3237585002430511",
"address": "Turgeneva, 172/1",
"create_time": "2008-07-22 10:02:04 07",
"modification_time": "2013-08-09 20:04:36 07",
"see_also": [
{
"id": "3237491513434577",
"lon": 38.973110606808,
"lat": 45.029031222211,
"name": "Advance",
"hash": "5698hn745A8IJ1H86177uvgn94521J3464he26763737242Cf6e654G62J0I7878e",
"ads": {
"sponsored_article": {
"title:"aman",
"text": "Business.English."
},
"warning": null
}
}
]
}
I know there is syntax issue in title key not enclosed in double quotes.How can I fix this basic syntax issues of json file automatically? Any help will be much apprecited.

Invalid Json to valid json

How to convert below json to valid json
{
"attribute_id":"r2d2",
"attribute_values":[
WrappedArray( [
5 b1ed5df4a0330a13a3b6f4c,
{
"L0":{
"name":"ENTERTAINMENT",
"id":"20000"
},
"L1":{
"name":"VIDEO GAMES BOOKS AND OTHER MEDIA",
"id":"26000"
},
"L2":{
"name":"MEDIA",
"id":"26001"
},
"L3":{
"name":"BOOKS",
"id":"26100"
},
"L4":{
"name":"BOOKS MISC L4",
"id":"26800"
}
},
PRIORITY_OTHERS,
1536662873016,
26800
] )
],
"bu":"0",
"item_id":"705024754",
"last_updated_by":"QARTH",
"mart":"0",
"published_at":"1536662873017",
"source":"RAMP",
"timestamp":"1536662873016",
"vertical":"0",
"wpid":"7HX1KF1N9W9Y"
}
Valid json should be this
{
"attribute_id": "r2d2",
"attribute_values": [
[{
"L0": {
"name": "ENTERTAINMENT",
"id": "20000"
},
"L1": {
"name": "VIDEO GAMES BOOKS AND OTHER MEDIA",
"id": "26000"
},
"L2": {
"name": "MEDIA",
"id": "26001"
},
"L3": {
"name": "BOOKS",
"id": "26100"
},
"L4": {
"name": "BOOKS MISC L4",
"id": "26800"
}
},
"PRIORITY_OTHERS",
1536662873016,
26800
]
],
"bu": "0",
"item_id": "705024754",
"last_updated_by": "QARTH",
"mart": "0",
"published_at": "1536662873017",
"source": "RAMP",
"timestamp": "1536662873016",
"vertical": "0",
"wpid": "7HX1KF1N9W9Y"
}
Could you please post your sql if this does not answer your question?

Type error: undefined is not an object(evaluating 'this.state.departure.loc.map) in React native maps

I am fetching my data from Google Directions and because the way the JSON response was set up, like this:
Object {
"geocoded_waypoints": Array [
Object {
"geocoder_status": "OK",
"place_id": "ChIJ2YDFT9zweUgRgt5Tws6n3hs",
"types": Array [
"premise",
],
},
Object {
"geocoder_status": "OK",
"place_id": "ChIJW2PCihXxeUgRRWgA9kOzpjY",
"types": Array [
"establishment",
"food",
"lodging",
"point_of_interest",
"restaurant",
],
},
],
"routes": Array [
Object {
"bounds": Object {
"northeast": Object {
"lat": 52.9242042,
"lng": -1.4751044,
},
"southwest": Object {
"lat": 52.900484,
"lng": -1.4801554,
},
},
"copyrights": "Map data ©2018 Google",
"legs": Array [
Object {
"distance": Object {
"text": "3.1 km",
"value": 3137,
},
"duration": Object {
"text": "38 mins",
"value": 2309,
},
"end_address": "Full St, Derby DE1 3AF, UK",
"end_location": Object {
"lat": 52.9242042,
"lng": -1.4756769,
},
"start_address": "50 Chatham St, Derby DE23 8TH, UK",
"start_location": Object {
"lat": 52.900484,
"lng": -1.4801443,
},
"steps": Array [
Object {
"distance": Object {
"text": "3.1 km",
"value": 3137,
},
"duration": Object {
"text": "38 mins",
"value": 2309,
},
"end_location": Object {
"lat": 52.9242042,
"lng": -1.4756769,
},
"html_instructions": "Walk to Full St, Derby DE1 3AF, UK",
"polyline": Object {
"points": "_c{aIza`Hm#BEyHC_AyAk#iCw#KCGAGAMBSFE#iBz#C#E#QDe#TMFUHWJm#TIm#k#_Em#yDuE`BIDw#SEPCJGPINILEDGFWTcBbAwB~#WJ]PIDOHWNMJ?##??#?#?#A??#?#?#A??#?#A??#A?A??#AAA?A??AA??A?AA??AI#EBGBOHo#XGBIDKBGBG#G#G#OBu#JG#KBID?A?AA??AA??AA?A?A??#A??#A??#?#?#]UGG}AiAcAu#g#a#kAw#c#Wu#_#c#U}#e#y#g#WQKGGEEAWGc#Iq#EW?k#?M#g#FoBPq#J[HSDIBGB]Ja#RcAdAYX{AdAq#Ry#Hu#BU?yA#g#AYAAIAG?CAECGACCGEEEECCEAICK?C#A#A?C#C#EDCBABCDABABCDGYgBkBwBsBw#y#GCGAYe#cAqACCAECE?C?G#Kc#NC#gBj#MDKDaBdAq#^GHCFCFCNCFEHEFwCZi##MAMCIIMOSa#Yq#Oc#Qk#CEKc#EQWuAG?ICCIAKAKKFIDIFE#OPONMLa#h#?#",
},
"start_location": Object {
"lat": 52.900484,
"lng": -1.4801443,
},
"steps": Array [
Object {
"distance": Object {
"text": "26 m",
"value": 26,
},
"duration": Object {
"text": "1 min",
"value": 17,
},
"end_location": Object {
"lat": 52.9007148,
"lng": -1.4801554,
},
"html_instructions": "Head <b>north</b> on <b>Chatham St</b> toward <b>Randolph Rd</b>",
"polyline": Object {
"points": "_c{aIza`Hm#B",
},
"start_location": Object {
"lat": 52.900484,
"lng": -1.4801443,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.1 km",
"value": 126,
},
"duration": Object {
"text": "1 min",
"value": 88,
},
"end_location": Object {
"lat": 52.9007561,
"lng": -1.4782714,
},
"html_instructions": "Turn <b>right</b> onto <b>Randolph Rd</b>",
"maneuver": "turn-right",
"polyline": Object {
"points": "md{aI~a`HEyHC_A",
},
"start_location": Object {
"lat": 52.9007148,
"lng": -1.4801554,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.3 km",
"value": 332,
},
"duration": Object {
"text": "4 mins",
"value": 223,
},
"end_location": Object {
"lat": 52.90362829999999,
"lng": -1.4785171,
},
"html_instructions": "Turn <b>left</b> onto <b>St Thomas Rd</b>",
"maneuver": "turn-left",
"polyline": Object {
"points": "wd{aIdv_HyAk#iCw#KCGAGAMBSFE#iBz#C#E#QDe#TMFUHWJm#T",
},
"start_location": Object {
"lat": 52.9007561,
"lng": -1.4782714,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.2 km",
"value": 153,
},
"duration": Object {
"text": "2 mins",
"value": 104,
},
"end_location": Object {
"lat": 52.90412999999999,
"lng": -1.4763973,
},
"html_instructions": "Turn <b>right</b> to stay on <b>St Thomas Rd</b>",
"maneuver": "turn-right",
"polyline": Object {
"points": "uv{aIvw_HIm#k#_Em#yD",
},
"start_location": Object {
"lat": 52.90362829999999,
"lng": -1.4785171,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.1 km",
"value": 129,
},
"duration": Object {
"text": "2 mins",
"value": 103,
},
"end_location": Object {
"lat": 52.9052488,
"lng": -1.4769249,
},
"html_instructions": "Turn <b>left</b> onto <b>Portland St</b>",
"maneuver": "turn-left",
"polyline": Object {
"points": "yy{aInj_HuE`BID",
},
"start_location": Object {
"lat": 52.90412999999999,
"lng": -1.4763973,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "32 m",
"value": 32,
},
"duration": Object {
"text": "1 min",
"value": 24,
},
"end_location": Object {
"lat": 52.9055291,
"lng": -1.4768217,
},
"html_instructions": "Slight <b>right</b> at <b>Goodale St</b>",
"maneuver": "turn-slight-right",
"polyline": Object {
"points": "y`|aIvm_Hw#S",
},
"start_location": Object {
"lat": 52.9052488,
"lng": -1.4769249,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.4 km",
"value": 418,
},
"duration": Object {
"text": "5 mins",
"value": 325,
},
"end_location": Object {
"lat": 52.9089381,
"lng": -1.4789711,
},
"html_instructions": "Turn <b>left</b> onto <b>Pear Tree Rd</b><div style=\"font-size:0.9em\">Go through 1 roundabout</div>",
"maneuver": "turn-left",
"polyline": Object {
"points": "qb|aIbm_HEPCJGPINILEDGFWTcBbAwB~#WJ]PIDOHWNMJ?##??#?#?#A??#?#?#A??#?#A??#A?A??#AAA?A??AA??A?AA??AI#EBGBOHo#XGBIDKBGBG#G#G#OBu#JG#KBID",
},
"start_location": Object {
"lat": 52.9055291,
"lng": -1.4768217,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "1.0 km",
"value": 984,
},
"duration": Object {
"text": "13 mins",
"value": 760,
},
"end_location": Object {
"lat": 52.9173185,
"lng": -1.4781371,
},
"html_instructions": "At the roundabout, take the <b>1st</b> exit onto <b>Normanton Rd</b>",
"maneuver": "roundabout-left",
"polyline": Object {
"points": "{w|aIpz_H?A?AA??AA??AA?A?A??#A??#A??#?#?#]UGG}AiAcAu#g#a#kAw#c#Wu#_#c#U}#e#y#g#WQKGGEEAWGc#Iq#EW?k#?M#g#FoBPq#J[HSDIBGB]Ja#RcAdAYX{AdAq#Ry#Hu#BU?yA#g#AYA",
},
"start_location": Object {
"lat": 52.9089381,
"lng": -1.4789711,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.3 km",
"value": 272,
},
"duration": Object {
"text": "3 mins",
"value": 207,
},
"end_location": Object {
"lat": 52.9193378,
"lng": -1.4764257,
},
"html_instructions": "At the roundabout, take the <b>3rd</b> exit onto <b>Babington Ln</b>",
"maneuver": "roundabout-left",
"polyline": Object {
"points": "gl~aIju_HAIAG?CAECGACCGEEEECCEAICK?C#A#A?C#C#EDCBABCDABABCDGYgBkBwBsBw#y#GCGA",
},
"start_location": Object {
"lat": 52.9173185,
"lng": -1.4781371,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "82 m",
"value": 82,
},
"duration": Object {
"text": "1 min",
"value": 54,
},
"end_location": Object {
"lat": 52.91985400000001,
"lng": -1.4756332,
},
"html_instructions": "Slight <b>right</b> to stay on <b>Babington Ln</b>",
"maneuver": "turn-slight-right",
"polyline": Object {
"points": "{x~aItj_HYe#cAqACCAECE?C?G#K",
},
"start_location": Object {
"lat": 52.9193378,
"lng": -1.4764257,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.2 km",
"value": 217,
},
"duration": Object {
"text": "2 mins",
"value": 144,
},
"end_location": Object {
"lat": 52.9216227,
"lng": -1.4768472,
},
"html_instructions": "Turn <b>left</b> onto <b>St Peters St</b>",
"maneuver": "turn-left",
"polyline": Object {
"points": "a|~aIte_Hc#NC#gBj#MDKDaBdAq#^GHCFCFCNCFEHEF",
},
"start_location": Object {
"lat": 52.91985400000001,
"lng": -1.4756332,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.3 km",
"value": 266,
},
"duration": Object {
"text": "3 mins",
"value": 186,
},
"end_location": Object {
"lat": 52.9234756,
"lng": -1.4752851,
},
"html_instructions": "Slight <b>right</b> onto <b>Corn Market</b>",
"maneuver": "turn-slight-right",
"polyline": Object {
"points": "cg_bIhm_HwCZi##MAMCIIMOSa#Yq#Oc#Qk#CEKc#EQWuA",
},
"start_location": Object {
"lat": 52.9216227,
"lng": -1.4768472,
},
"travel_mode": "WALKING",
},
Object {
"distance": Object {
"text": "0.1 km",
"value": 100,
},
"duration": Object {
"text": "1 min",
"value": 74,
},
"end_location": Object {
"lat": 52.9242042,
"lng": -1.4756769,
},
"html_instructions": "At the roundabout, take the <b>2nd</b> exit onto <b>Full St</b><div style=\"font-size:0.9em\">Destination will be on the right</div>",
"maneuver": "roundabout-left",
"polyline": Object {
"points": "wr_bIpc_HG?ICCIAKAKKFIDIFE#OPONMLa#h#?#",
},
"start_location": Object {
"lat": 52.9234756,
"lng": -1.4752851,
},
"travel_mode": "WALKING",
},
],
"travel_mode": "WALKING",
},
],
"traffic_speed_entry": Array [],
"via_waypoint": Array [],
},
],
"overview_polyline": Object {
"points": "_c{aIza`Hm#BEyHC_AyAk#uC{#OCa#JoB|#IBw#ZiBr#u#mFm#yDuE`BIDw#SI\\Q`#OR_#\\cBbAwB~#u#\\YNe#\\#??BA#?DCBE#EEAAg#RmAf#yB^IBAAAACACBA#?#e#[uGyE{DsBeBgA]IuAOcA?u#HoBPq#Jo#NQF]Ja#R}A~A{AdAq#RoBLoB#aACCQGUSWOEO#KDORCFCDGY_F_F_A}#GAYe#gAuAEW#Kc#NkBl#YJsCdBKPGVIPEFwCZw#?WMa#q#i#uAg#gBWuAG?ICCICWULOH_#`#o#x#",
},
"summary": "Normanton Rd",
"warnings": Array [
"Walking directions are in beta. Use caution – This route may be missing sidewalks or pedestrian paths.",
],
"waypoint_order": Array [],
},
],
"status": "OK",
}
I am trying to get data from transit details which has the latitudes and longitude values which i am trying to acquire to be able to view this on in the map using MapView in React native and i believe i am successful of on doing this as this is my attempt on fetching the data setting the state:
fetch('https://maps.googleapis.com/maps/api/directions/json?origin=' + latitude + ',' + longitude + '&destination=' + goingto + '&mode=transit&transit_mode=bus&key=' + apiDirectionskey)
.then((resdirections) => resdirections.json())
.then((responseJson4) => {
//const departureloc1 = responseJson4.routes[0].legs[0].steps[1];
//console.log(departureloc1)
const departureloc = responseJson4.routes[0].legs[0].steps[1].transit_details.departure_stop.location;
console.log(departureloc);
const stopmarkers = Object.keys(departureloc).map(value => {
// noinspection JSAnnotator
if (typeof departureloc['lat'] === undefined && typeof departureloc['lng'] === undefined) {
console.log('null');
} else {
return {
latlng: {
latitude: departureloc['lat'],
longitude: departureloc['lng']
}
}
}
this.setState({ departureloc: stopmarkers });
});
})
}
printing the fetch call above will result in this JSON result
Object {
"lat": 52.90736,
"lng": -1.453532,
}
The next section is my render function below which has a mapview in my attempt to set the markers.
<View style={styles.container}>
<Mainlogo/>
<TextInput style={styles.boxInput} underlineColorAndroid='rgba(0,0,0,0)' placeholder="Going To?"
underlineColorAndroid='transparent'
onChangeText={(dest) => this.setState({goingto : dest})}
/>
<TouchableOpacity style={styles.button} onPress={this.fetchDirections.bind(this)}>
<Text style={styles.textButton}> Go {this.props.type}</Text>
</TouchableOpacity>
{this.state.departureloc.map(marker => (
<MapView.Marker
coordinate={marker.latlng}
image={require('../img/busstop1.png')}
/>
))}
</MapView> : <Text style={{fontWeight: 'bold', color: '#546e7a', textAlign: 'center'}}>Please wait.. loading</Text>}
</View>
I am unsuccessful at this stage and instead i cannot get around with this error.
I am very new to this and anyone that can guide and give me pointers on the correct way would be greatly appreciated thanks
At render time, this.state.departureloc is unset, likely because render() gets called before your API calls return. Try setting this.state.departureloc = [] in your constructor(props) function.
Also it's not good to iterate within your JSX rendering. Consider extracting that rendering to before your return statement and storing it in a variable.

Why is JSON.parse not outputing the correct result?

Here's my code:
var jsonStr = (JSON.stringify(data, ['flightPositions', 'flightId', 'positions', 'lat', 'lon', 'date'], 4));
alert(jsonStr);
var jsonObj = JSON.parse(jsonStr);
alert(jsonObj.flightPositions);
},
The output from JSON.stringify seems to be correct. I've run it through JSLint and gives me no error:
{
"flightPositions": [
{
"flightId": 621650693,
"positions": [
{
"lat": 40.4272,
"lon": -74.3395,
"date": "2015-10-31T13:31:55.000Z"
},
{
"lat": 40.6833,
"lon": -74.1667,
"date": "2015-10-31T13:32:00.000Z"
},
{
"lat": 40.6133,
"lon": -74.0122,
"date": "2015-10-31T13:32:55.000Z"
},
{
"lat": 40.7538,
"lon": -74.0989,
"date": "2015-10-31T13:33:55.000Z"
},
{
"lat": 40.8125,
"lon": -74.1044,
"date": "2015-10-31T13:34:55.000Z"
},
{
"lat": 40.8147,
"lon": -74.1832,
"date": "2015-10-31T13:35:55.000Z"
},
{
"lat": 40.7624,
"lon": -74.2316,
"date": "2015-10-31T13:36:55.000Z"
},
{
"lat": 40.6829,
"lon": -74.2523,
"date": "2015-10-31T13:37:55.000Z"
}
]
},
{
"flightId": 621650363,
"positions": [
{
"lat": 40.7859,
"lon": -74.0978,
"date": "2015-10-31T13:29:21.000Z"
},
{
"lat": 40.7738,
"lon": -74.143,
"date": "2015-10-31T13:30:21.000Z"
},
{
"lat": 40.7169,
"lon": -74.1948,
"date": "2015-10-31T13:31:21.000Z"
},
{
"lat": 40.6602,
"lon": -74.2524,
"date": "2015-10-31T13:32:21.000Z"
},
{
"lat": 40.5888,
"lon": -74.3109,
"date": "2015-10-31T13:33:21.000Z"
},
{
"lat": 40.5004,
"lon": -74.3464,
"date": "2015-10-31T13:34:21.000Z"
},
{
"lat": 40.4213,
"lon": -74.3074,
"date": "2015-10-31T13:35:21.000Z"
},
{
"lat": 40.3152,
"lon": -74.2497,
"date": "2015-10-31T13:36:21.000Z"
},
{
"lat": 40.2224,
"lon": -74.1577,
"date": "2015-10-31T13:37:21.000Z"
},
{
"lat": 40.163,
"lon": -74.0203,
"date": "2015-10-31T13:38:21.000Z"
}
]
},
{
"flightId": 621650734,
"positions": [
{
"lat": 40.6742,
"lon": -74.1728,
"date": "2015-10-31T13:29:15.000Z"
},
{
"lat": 40.6369,
"lon": -74.1976,
"date": "2015-10-31T13:30:15.000Z"
},
{
"lat": 40.7258,
"lon": -74.1342,
"date": "2015-10-31T13:30:22.000Z"
},
{
"lat": 40.8227,
"lon": -74.0561,
"date": "2015-10-31T13:32:15.000Z"
},
{
"lat": 40.7436,
"lon": -74.2853,
"date": "2015-10-31T13:33:15.000Z"
},
{
"lat": 40.6199,
"lon": -74.2754,
"date": "2015-10-31T13:34:15.000Z"
},
{
"lat": 40.5328,
"lon": -74.2924,
"date": "2015-10-31T13:35:15.000Z"
},
{
"lat": 40.4304,
"lon": -74.2721,
"date": "2015-10-31T13:36:15.000Z"
},
{
"lat": 40.3341,
"lon": -74.2384,
"date": "2015-10-31T13:37:15.000Z"
},
{
"lat": 40.2547,
"lon": -74.173,
"date": "2015-10-31T13:38:15.000Z"
}
]
},
{
"flightId": 621633505,
"positions": [
{
"lat": 40.3481,
"lon": -74.7084,
"date": "2015-10-31T13:29:35.000Z"
},
{
"lat": 40.3788,
"lon": -74.6202,
"date": "2015-10-31T13:30:35.000Z"
},
{
"lat": 40.4126,
"lon": -74.5206,
"date": "2015-10-31T13:31:35.000Z"
},
{
"lat": 40.4436,
"lon": -74.4448,
"date": "2015-10-31T13:32:35.000Z"
},
{
"lat": 40.45,
"lon": -74.4333,
"date": "2015-10-31T13:32:35.000Z"
},
{
"lat": 40.481,
"lon": -74.3348,
"date": "2015-10-31T13:33:47.000Z"
},
{
"lat": 40.5225,
"lon": -74.2702,
"date": "2015-10-31T13:34:47.000Z"
},
{
"lat": 40.5691,
"lon": -74.2228,
"date": "2015-10-31T13:35:47.000Z"
},
{
"lat": 40.6234,
"lon": -74.1737,
"date": "2015-10-31T13:36:47.000Z"
},
{
"lat": 40.6804,
"lon": -74.1765,
"date": "2015-10-31T13:37:47.000Z"
}
]
},
{
"flightId": 621650749,
"positions": [
{
"lat": 40.6906,
"lon": -74.2433,
"date": "2015-10-31T13:34:54.000Z"
},
{
"lat": 40.631,
"lon": -74.5142,
"date": "2015-10-31T13:35:54.000Z"
},
{
"lat": 39.6624,
"lon": -74.6148,
"date": "2015-10-31T13:36:54.000Z"
},
{
"lat": 40.6833,
"lon": -74.1667,
"date": "2015-10-31T13:37:00.000Z"
},
{
"lat": 40.7207,
"lon": -74.1416,
"date": "2015-10-31T13:37:54.000Z"
}
]
}
]
}
alert output from JSON.stringify
However, I am getting the following for the JSON.parse:
[object Object],[object Object],[object Object],[object Object],[object Object]
What could be wrong here?
UPDATE
Changed alert to console.log (without concatenating string) and it gives me the following output:
Array [ Object, Object, Object, Object, Object ]
console
Your code to stringify and parse JSON is correct, the behavior you are seeing is due to the alert function. alert automatically tries to convert the parameter to a human-readable string (not a JSON string), in your case it converts every item inside the flightPositions array to string.
To debug JS you should use the console.log method (docs) and the browser developer's tools (use F12 to open the tools).
var jsonStr = (JSON.stringify(data, ['flightPositions', 'flightId', 'positions', 'lat', 'lon', 'date'], 4));
console.log(jsonStr);
var jsonObj = JSON.parse(jsonStr);
console.log(jsonObj.flightPositions);