Vignesh Iyer
Vignesh Iyer

Reputation: 3

How will i get the co-ordinate value from the below JSON using python json into a pandas dataframe?

{ "Type" : "type1" "Results" : [ { "Type" : "type1" "Results" : [ {"Type":"type1","Coordinate":-0.3,"Value":123123}, {"Type":"type1","Coordinate":-0.2,"Value":123123}, {"Type":"type1","Coordinate":-0.1,"Value":123123}, {"Type":"type1","Coordinate":0,"Value":123123}, {"Type":"type1","Coordinate":0.1,"Value":123123}, {"Type":"type1","Coordinate":0.2,"Value":123123}, {"Type":"type1","Coordinate":0.3,"Value":123123} ] } ] }

Upvotes: 0

Views: 24

Answers (2)

J_H
J_H

Reputation: 20450

>>> d = json.load(open('results.json'))
>>> d['Results'][0]['Results'][0]['Coordinate']
-0.3
>>> d['Results'][0]['Results'][1]['Coordinate']
-0.2

You can iterate through those, or simply pass pandas that final Results:

>>> df = pd.DataFrame(d['Results'][0]['Results'])
>>> df.dtypes
Type           object
Coordinate    float64
Value           int64

You apparently intended to use the following JSON input, which is a little different from what you posted.

{"Results": [{"Results": [{"Coordinate": -0.3, "Type": "type1", "Value": 123123},
                          {"Coordinate": -0.2, "Type": "type1", "Value": 123123},
                          {"Coordinate": -0.1, "Type": "type1", "Value": 123123},
                          {"Coordinate": 0, "Type": "type1", "Value": 123123},
                          {"Coordinate": 0.1, "Type": "type1", "Value": 123123},
                          {"Coordinate": 0.2, "Type": "type1", "Value": 123123},
                          {"Coordinate": 0.3, "Type": "type1", "Value": 123123}],
              "Type": "type1"}],
 "Type": "type1"}

Upvotes: 1

MacOS
MacOS

Reputation: 1159

First, the JSON you have posted is not valid JSON. The correct version is

{
    "Type" : "type1",
    "Results" :
        [
            { "Type" : "type1", "Results" :
             [
                 {"Type":"type1","Coordinate":-0.3,"Value":123123},
                 {"Type":"type1","Coordinate":-0.2,"Value":123123},
                 {"Type":"type1","Coordinate":-0.1,"Value":123123},
                 {"Type":"type1","Coordinate":0,"Value":123123},
                 {"Type":"type1","Coordinate":0.1,"Value":123123},
                 {"Type":"type1","Coordinate":0.2,"Value":123123}, 
                 {"Type":"type1","Coordinate":0.3,"Value":123123} 
             ] 
            } 
        ] 
}

Please note the missing , that are missing in you post. I don't know exactly what you want, but please have a look at the following.

import pandas as pd

json = {
    "Type" : "type1",
    "Results" :
        [
            { "Type" : "type1", "Results" :
             [
                 {"Type":"type1","Coordinate":-0.3,"Value":123123},
                 {"Type":"type1","Coordinate":-0.2,"Value":123123},
                 {"Type":"type1","Coordinate":-0.1,"Value":123123},
                 {"Type":"type1","Coordinate":0,"Value":123123},
                 {"Type":"type1","Coordinate":0.1,"Value":123123},
                 {"Type":"type1","Coordinate":0.2,"Value":123123}, 
                 {"Type":"type1","Coordinate":0.3,"Value":123123} 
             ] 
            } 
        ] 
}

df = pd.DataFrame(json["Results"][0]["Results"])

which gives you

    Type    Coordinate  Value
0   type1   -0.3        123123
1   type1   -0.2        123123
2   type1   -0.1        123123
3   type1   0.0         123123
4   type1   0.1         123123

Upvotes: 0

Related Questions