Query Nested JSON Data - python

I am trying to query using the code below to get the value etoday but it does not return a result.
Result = json.loads(json_string)
# Next Line works perfect
print("The value of msg", "msg", "is: ", Result["msg"])
#Next Line does not (I have a few variations here that I tried
print(Result['data']['page']['records']['etoday'])
print(Result["data"][0]["page"][0]["etoday"])
print(Result['page']['records'][0]['etoday'])
Print("The value of", "etoday", "is: ", Result["etoday"])
File I am querying
{
"success": true,
"code": "0",
"msg": "success",
"data": {
"inverterStatusVo": {
"all": 1,
"normal": 1,
"fault": 0,
"offline": 0,
"mppt": 0
},
"page": {
"records": [
{
"id": "zzz",
"sn": "xxx",
"collectorSn": "vvv",
"userId": "ttt",
"productModel": "3105",
"nationalStandards": "68",
"inverterSoftwareVersion": "3d0037",
"dcInputType": 2,
"acOutputType": 0,
"stationType": 0,
"stationId": "1298491919448828419",
"rs485ComAddr": "101",
"simFlowState": -1,
"power": 6.000,
"powerStr": "kW",
"pac": 0.001,
"pac1": 0,
"pacStr": "kW",
"state": 1,
"stateExceptionFlag": 0,
"fullHour": 1.82,
"totalFullHour": 191.83,
"maxDcBusTime": "1675460767377",
"maxUac": 251.5,
"maxUacTime": "1664022228160",
"maxUpv": 366.3,
"maxUpvTime": "1663309960961",
"timeZone": 0.00,
"timeZoneStr": "(UTC+00:00)",
"timeZoneName": "(UTC+00:00)Europe/Dublin",
"dataTimestamp": "1675460767377",
"dataTimestampStr": "2023-02-03 21:46:07 (UTC+00:00)",
"fisTime": "1663189116371",
"inverterMeterModel": 5,
"updateShelfBeginTime": 1671292800000,
"updateShelfEndTime": 1829059200000,
"updateShelfEndTimeStr": "2027-12-18",
"updateShelfTime": "5",
"collectorId": "1306858901387600551",
"dispersionRate": 0.0,
"currentState": "3",
"pow1": 0.0,
"pow2": 0.0,
"pow3": 0.0,
"pow4": 0.0,
"pow5": 0.0,
"pow6": 0.0,
"pow7": 0.0,
"pow8": 0.0,
"pow9": 0.0,
"pow10": 0.0,
"pow11": 0.0,
"pow12": 0.0,
"pow13": 0.0,
"pow14": 0.0,
"pow15": 0.0,
"pow16": 0.0,
"pow17": 0.0,
"pow18": 0.0,
"pow19": 0.0,
"pow20": 0.0,
"pow21": 0.0,
"pow22": 0.0,
"pow23": 0.0,
"pow24": 0.0,
"pow25": 0.0,
"pow26": 0.0,
"pow27": 0.0,
"pow28": 0.0,
"pow29": 0.0,
"pow30": 0.0,
"pow31": 0.0,
"pow32": 0.0,
"gridPurchasedTodayEnergy": 3.800,
"gridPurchasedTodayEnergyStr": "kWh",
"gridSellTodayEnergy": 2.400,
"gridSellTodayEnergyStr": "kWh",
"psumCalPec": "1",
"batteryPower": 0.099,
"batteryPowerStr": "kW",
"batteryPowerPec": "1",
"batteryCapacitySoc": 20.000,
"parallelStatus": 0,
"parallelAddr": 0,
"parallelPhase": 0,
"parallelBattery": 0,
"batteryTodayChargeEnergy": 4.800,
"batteryTodayChargeEnergyStr": "kWh",
"batteryTotalChargeEnergy": 449.000,
"batteryTotalChargeEnergyStr": "kWh",
"batteryTodayDischargeEnergy": 5.500,
"batteryTodayDischargeEnergyStr": "kWh",
"batteryTotalDischargeEnergy": 627.000,
"batteryTotalDischargeEnergyStr": "kWh",
"bypassLoadPower": 0.000,
"bypassLoadPowerStr": "kW",
"backupTodayEnergy": 0.000,
"backupTodayEnergyStr": "kWh",
"backupTotalEnergy": 0.000,
"backupTotalEnergyStr": "kWh",
"etotal": 1.153,
"etoday": 10.900,
"psum": -1.756,
"psumCal": -1.756,
"etotal1": 1153.000,
"etoday1": 10.900000,
"offlineLongStr": "--",
"etotalStr": "MWh",
"etodayStr": "kWh",
"psumStr": "kW",
"psumCalStr": "kW"
}
],
"total": 1,
"size": 20,
"current": 1,
"orders": [
],
"optimizeCountSql": false,
"searchCount": true,
"pages": 1
},
"mpptSwitch": 0
}
}

The records key is the only array in your path, that's the only one where you need to select with an index as below:
Result['data']['page']['records'][0]['etoday']

The issue might be that the key 'etoday' is not present at the location you are trying to access it from. Try using the following code to see if the key exists in the JSON object:
if 'etoday' in Result['data']['page']['records']:
print("The value of etoday is: ", Result['data']['page']['records']['etoday'])
else:
print("The key 'etoday' was not found in the Result['data']['page']['records'] dictionary.")

Related

Save and Load trajectory generated with moveit

I want to record and play a pre-planned trajectory using Move-It,
My Setup:
Ubuntu 20.04, ROS Noetic.
Python 3.8.10
I have tried the solution mentioned here but I get the error:
could not determine a constructor for the tag \'tag:yaml.org,2002:python/tuple\'\n
in "/home/saved_trajectories/test.yaml", line 1, column 1
Here is the YAML file that I have saved:
!!python/tuple [true, !!python/object/new:moveit_msgs.msg._RobotTrajectory.RobotTrajectory {
state: [!!python/object/new:trajectory_msgs.msg._JointTrajectory.JointTrajectory {
state: [!!python/object/new:std_msgs.msg._Header.Header {state: [0, !!python/object/new:genpy.rostime.Time {
state: [0, 0]}, world]}, [yumi_robr_joint_1, yumi_robr_joint_2, yumi_robr_joint_3,
yumi_robr_joint_4, yumi_robr_joint_5, yumi_robr_joint_6, yumi_robr_joint_7,
gripper_r_joint], [!!python/object/new:trajectory_msgs.msg._JointTrajectoryPoint.JointTrajectoryPoint {
state: [!!python/tuple [0.6070651736385813, 0.29742469343822114, -1.0670289134119273,
0.5490030687107282, 1.0947504229888345, -0.2727256386719584, 3.7517497572539567,
0.0], !!python/tuple [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], !!python/tuple [
-0.09083825158724707, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], !!python/tuple [],
!!python/object/new:genpy.rostime.Duration {state: [0, 0]}]}, !!python/object/new:trajectory_msgs.msg._JointTrajectoryPoint.JointTrajectoryPoint {
state: [!!python/tuple [0.6015542781334869, 0.35733262252205716, -1.0699803518099047,
0.5145828872943146, 1.1295083723579553, -0.24630768917984783, 3.758266111374755,
0.0006667045908980072], !!python/tuple [-0.0269508869304581, 0.2929781233347364,
-0.014433930469661484, -0.16833097572292724, 0.16998282085294777,
0.12919627473216633, 0.031868055372510196, 0.003260500952830533],
!!python/tuple [-0.09029591410123787, 0.9815902358403596, -0.04835925990839515,
-0.5639739932743149, 0.5695083145140482, 0.4328575811073439, 0.10677033367824533,
0.010923941565391214], !!python/tuple [], !!python/object/new:genpy.rostime.Duration {
state: [0, 348330660]}]}, !!python/object/new:trajectory_msgs.msg._JointTrajectoryPoint.JointTrajectoryPoint {
state: [!!python/tuple [0.5960433826283926, 0.4172405516058932, -1.072931790207882,
0.4801627058779009, 1.1642663217270761, -0.21988973968773723, 3.7647824654955535,
0.0013334091817960145], !!python/tuple [-0.04391971455822303, 0.47744312021595553,
-0.02352182723397893, -0.2743155883564974, 0.2770074688423648, 0.2105408821187911,
0.051932832455485835, 0.005313378796572893], !!python/tuple [-0.0914213742266345,
0.9938249053811138, -0.0489620160714013, -0.571003439152396, 0.5766067408984992,
0.4382527748141437, 0.10810113313160441, 0.011060099006913317],
!!python/tuple [], !!python/object/new:genpy.rostime.Duration {state: [
0, 493046135]}]}, !!python/object/new:trajectory_msgs.msg._JointTrajectoryPoint.JointTrajectoryPoint {
state: [!!python/tuple [0.5905324871232982, 0.47714848068972915, -1.0758832286058593,
0.4457425244614872, 1.1990242710961971, -0.1934717901956266, 3.7712988196163515,
0.0020001137726940216], !!python/tuple [-0.043958350827791745, 0.4778631279797113,
-0.023542519437170645, -0.27455690438337166, 0.27725115292242763,
0.21072609539621331, 0.05197851788250647, 0.005318052987595537],
!!python/tuple [0.0909207245758629, -0.988382435324015, 0.04869388603666274,
0.5678764606441491, -0.5734490771037308, -0.43585277699616626, -0.10750914033989453,
-0.010999530734426131], !!python/tuple [], !!python/object/new:genpy.rostime.Duration {
state: [0, 603798916]}]}, !!python/object/new:trajectory_msgs.msg._JointTrajectoryPoint.JointTrajectoryPoint {
state: [!!python/tuple [0.5850215916182039, 0.5370564097735652, -1.0788346670038367,
0.41132234304507354, 1.233782220465318, -0.167053840703516, 3.77781517373715,
0.002666818363592029], !!python/tuple [-0.02686357658598151, 0.29202898867588584,
-0.01438717017399073, -0.16778564912516547, 0.1694321429223079,
0.12877772927646153, 0.03176481532332174, 0.0032499382035565505],
!!python/tuple [0.09064887662683936, -0.9854272263863499, 0.04854829400460408,
0.566178541365123, -0.5717344960089998, -0.43454960124550995, -0.10718769394320854,
-0.010966642744540041], !!python/tuple [], !!python/object/new:genpy.rostime.Duration {
state: [0, 748221334]}]}, !!python/object/new:trajectory_msgs.msg._JointTrajectoryPoint.JointTrajectoryPoint {
state: [!!python/tuple [0.5795106961131096, 0.5969643388574012, -1.081786105401814,
0.37690216162865986, 1.2685401698344387, -0.1406358912114054, 3.7843315278579484,
0.003333522954490036], !!python/tuple [0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0], !!python/tuple [0.08796870275503225, -0.9562915503249647,
0.04711288880209301, 0.5494385994065332, -0.5548302836807862, -0.42170147195145546,
-0.10401852442477695, -0.010642397034729915], !!python/tuple [],
!!python/object/new:genpy.rostime.Duration {state: [1, 102187696]}]}]]},
!!python/object/new:trajectory_msgs.msg._MultiDOFJointTrajectory.MultiDOFJointTrajectory {
state: [!!python/object/new:std_msgs.msg._Header.Header {state: [0, !!python/object/new:genpy.rostime.Time {
state: [0, 0]}, '']}, [], []]}]}, 0.316400488, !!python/object/new:moveit_msgs.msg._MoveItErrorCodes.MoveItErrorCodes {
state: [1]}]
I have tried to read/write JSON instead of YAML, but I have got the following Error:
ERROR: b'error processing request: Object of type RobotTrajectory is not JSON serializable'
I have also tried the yaml.safe_load(stream) approach as mentioned here, but the problem was not solved!
Can you please tell me how I can save/load the trajectory correctly? thanks in advance.

Tkinter Labels not displaying correctly

I am practicing API integration into my some of my projects and I wanted to try and practice some dictionaries.
Pulled weather data and long story short: trying to create simple tkinter app that will show me things from this dictionary.
import tkinter as tk
window = tk.Tk()
sample = {"location": {"name": "Belgrade", "region": "Central Serbia", "country": "Serbia", "lat": 44.8, "lon": 20.47, "tz_id": "Europe/Belgrade", "localtime_epoch": 1643798038, "localtime": "2022-02-02 11:33"}, "current": {"last_updated_epoch": 1643796900, "last_updated": "2022-02-02 11:15", "temp_c": 2.0, "temp_f": 35.6, "is_day": 1, "condition": {"text": "Sunny", "icon": "//cdn.weatherapi.com/weather/64x64/day/113.png", "code": 1000}, "wind_mph": 0.0, "wind_kph": 0.0, "wind_degree": 228, "wind_dir": "SW", "pressure_mb": 1009.0, "pressure_in": 29.8, "precip_mm": 0.0, "precip_in": 0.0, "humidity": 100, "cloud": 0, "feelslike_c": -1.4, "feelslike_f": 29.6, "vis_km": 10.0, "vis_miles": 6.0, "uv": 1.0, "gust_mph": 9.2, "gust_kph": 14.8, "air_quality": {"co": 317.1000061035156, "no2": 13.399999618530273, "o3": 45.400001525878906, "so2": 10.5, "pm2_5": 11.399999618530273, "pm10": 15.600000381469727, "us-epa-index": 1, "gb-defra-index": 1}}}
for j, k in sample['location'].items():
tk.Label(window, text=j).grid(column=0)
tk.Label(window, text=k).grid(column=1)
window.mainloop()
However, when I make it I am getting weird thing: key: value pairs are added in zig-zag order, not in one next to other.
For example:
Location of 'name' key is (0,0). However value of it is put at (1, 1).
I could put all in single column and than it would get sort-of sorted and readable, however, I would love to understand why is output as is.
Strange pattern
You can set row, using a variable.
I have called it row in this example:
import tkinter as tk
window = tk.Tk()
sample = {"location": {"name": "Belgrade", "region": "Central Serbia", "country": "Serbia", "lat": 44.8, "lon": 20.47, "tz_id": "Europe/Belgrade", "localtime_epoch": 1643798038, "localtime": "2022-02-02 11:33"}, "current": {"last_updated_epoch": 1643796900, "last_updated": "2022-02-02 11:15", "temp_c": 2.0, "temp_f": 35.6, "is_day": 1, "condition": {"text": "Sunny", "icon": "//cdn.weatherapi.com/weather/64x64/day/113.png", "code": 1000}, "wind_mph": 0.0, "wind_kph": 0.0, "wind_degree": 228, "wind_dir": "SW", "pressure_mb": 1009.0, "pressure_in": 29.8, "precip_mm": 0.0, "precip_in": 0.0, "humidity": 100, "cloud": 0, "feelslike_c": -1.4, "feelslike_f": 29.6, "vis_km": 10.0, "vis_miles": 6.0, "uv": 1.0, "gust_mph": 9.2, "gust_kph": 14.8, "air_quality": {"co": 317.1000061035156, "no2": 13.399999618530273, "o3": 45.400001525878906, "so2": 10.5, "pm2_5": 11.399999618530273, "pm10": 15.600000381469727, "us-epa-index": 1, "gb-defra-index": 1}}}
row = 0
for j, k in sample['location'].items():
tk.Label(window, text=j).grid(column=0,row=row)
tk.Label(window, text=k).grid(column=1,row=row)
row += 1
window.mainloop()

I have a data file in json format. How can I find and print the top 20 eij_max values and the associated pretty_formula? I am using python

Here is part of the data file in json format, containing 1 entry. I need to find the 20 compounds (key:pretty_formula) with the highest eij_max values. I appreciate the help, I apologise if this is a basic question, I am new to all this.
{
"num_results": 3402,
"valid_response": true,
"criteria": "{\"piezo\": {\"$ne\": null}}",
"properties": "[\"pretty_formula\", \"piezo\", \"eij_max\", \"band_gap\", \"energy\"]",
"response": [
{
"pretty_formula": "KMg(PO3)3",
"piezo": {
"eij_max": 0.09337344787836212,
"piezoelectric_tensor": [
[
0.0,
0.0,
0.0,
0.0,
0.0,
0.0660250018229951
],
[
0.06602500182299509,
-0.06602499453211871,
0.0,
0.0,
0.0,
0.0
],
[
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
]
],
"v_max": [
-0.0,
1.0,
0.0
]
},
"eij_max": null,
"band_gap": 4.9474,
"energy": -193.18215352
},
{
"pretty_formula": "Sr2CuSi2O7",
You can easily do something like this to access the eij_max:
import json
# Opening JSON file
f = open('data.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data['response']['piezo']['eij_max']
print(i)
# Closing file
f.close()

How to calculate some data with python?

I have JSON file. I have parsed it and I have extracted some data which are classes and code smells. Now I should calculate the number of smells on each class. I tried this with an example of code smells and it return for me the number of this smell in all the json file.
this is a part of the Json file beacause it's too long
{
"methods": [
{
"parametersTypes": [
"Bundle"
],
"sourceFile": {
"file": {
"path": "/mnt/c/shortrain-master/app/src/main/java/com/nirhart/shortrain/MainActivity.java"
}
},
"metricsValues": {
"ParameterCount": 1.0,
"NumberOfAccessedVariables": 9.0,
"ChangingClasses": 0.0,
"CouplingDispersion": 0.5,
"MethodLinesOfCode": 21.0,
"MaxNesting": 0.0,
"CyclomaticComplexity": 1.0,
"MaxCallChain": 2.0,
"ChangingMethods": 0.0,
"CouplingIntensity": 4.0
},
"fullyQualifiedName": "com.nirhart.shortrain.MainActivity.onCreate",
"smells": []
},
{
"parametersTypes": [],
"sourceFile": {
"file": {
"path": "/mnt/c/shortrain-master/app/src/main/java/com/nirhart/shortrain/MainActivity.java"
}
},
"metricsValues": {
"ParameterCount": 0.0,
"NumberOfAccessedVariables": 2.0,
"ChangingClasses": 1.0,
"CouplingDispersion": 0.0,
"MethodLinesOfCode": 6.0,
"MaxNesting": 0.0,
"CyclomaticComplexity": 1.0,
"MaxCallChain": 6.0,
"ChangingMethods": 3.0,
"CouplingIntensity": 0.0
},
"fullyQualifiedName": "com.nirhart.shortrain.MainActivity.finishActivity",
"smells": [
{
"name": "MessageChain",
"reason": "MAX_CALL_CHAIN = 6.0",
"startingLine": 54,
"endingLine": 66
}
]
},
{
"parametersTypes": [
"View"
],
"sourceFile": {
"file": {
"path": "/mnt/c/shortrain-master/app/src/main/java/com/nirhart/shortrain/MainActivity.java"
}
},
"metricsValues": {
"ParameterCount": 1.0,
"NumberOfAccessedVariables": 4.0,
"ChangingClasses": 0.0,
"CouplingDispersion": 1.0,
"MethodLinesOfCode": 6.0,
"MaxNesting": 1.0,
"CyclomaticComplexity": 3.0,
"MaxCallChain": 1.0,
"ChangingMethods": 0.0,
"CouplingIntensity": 2.0
},
"fullyQualifiedName": "com.nirhart.shortrain.MainActivity.onClick",
"smells": []
},
{
"parametersTypes": [],
"sourceFile": {
"file": {
"path": "/mnt/c/shortrain-master/app/src/main/java/com/nirhart/shortrain/MainActivity.java"
}
},
"metricsValues": {
"ParameterCount": 0.0,
"NumberOfAccessedVariables": 0.0,
"ChangingClasses": 0.0,
"CouplingDispersion": 1.0,
"MethodLinesOfCode": 3.0,
"MaxNesting": 0.0,
"CyclomaticComplexity": 1.0,
"MaxCallChain": 1.0,
"ChangingMethods": 0.0,
"CouplingIntensity": 1.0
},
"fullyQualifiedName": "com.nirhart.shortrain.MainActivity.onBackPressed",
"smells": []
}
],
"sourceFile": {
"file": {
"path": "/mnt/c/shortrain-master/app/src/main/java/com/nirhart/shortrain/MainActivity.java"
}
},
"metricsValues": {
"ClassLinesOfCode": 40.0,
"OverrideRatio": null,
"WeighOfClass": 1.0,
"LCOM2": 0.5,
"TightClassCohesion": 0.0,
"LCOM3": 0.6666666666666666,
"NumberOfAccessorMethods": 0.0,
"WeightedMethodCount": 6.0,
"IsAbstract": 0.0,
"PublicFieldCount": 0.0
},
"fullyQualifiedName": "com.nirhart.shortrain.MainActivity",
"smells": []
},
]
This is my code:
import pandas as pd
import json
all_smells=['LazyClass','ComplexClass','LongParameterList','FeatureEnvy','LongMethod','GodClass','MessageChain']
with open('/content/result_smells.json') as handle:
dictdump = json.loads(handle.read())
my_map = {}
for elem in dictdump :
my_map[elem["fullyQualifiedName"]] = []
#adding all class smells
for class_smell in elem["smells"] :
my_map[elem["fullyQualifiedName"]].append(class_smell)
#adding all methods smells
for method in elem["methods"] :
for method_smell in method["smells"] :
my_map[elem["fullyQualifiedName"]].append(method_smell)
for elem in my_map :
print(elem)
for smell in my_map[elem] :
print(smell["name"])
This is the result : the name of class and the smells on it
com.nirhart.shortrain.MainActivity
MessageChain
com.nirhart.shortrain.path.PathParser
ComplexClass
FeatureEnvy
LongParameterList
LongParameterList
LongMethod
com.nirhart.shortrain.path.PathPoint
LazyClass
LongParameterList
com.nirhart.shortrain.path.TrainPath
FeatureEnvy
com.nirhart.shortrain.rail.RailActionActivity
FeatureEnvy
LongMethod
com.nirhart.shortrain.rail.RailInfo
com.nirhart.shortrain.train.TrainActionActivity
ComplexClass
SpaghettiCode
LongMethod
LongMethod
IntensiveCoupling
I try to calculate the number of the MessageChain smell in com.nirhart.shortrain.MainActivity class which is one it return for me 5 which is the MessageChain smell in all the json file
this is my code:
x=0
for elem in my_map :
print(elem)
for smell in my_map[elem] :
if smell["name"]=='MessageChain':
x+=1
Then I need to put all results on a CSV to analyse it.
this is an exemple of csv file with one smell
A Python Counter() can be used to simplyfy counting the smells, and a csv.DictWriter() can be used to then write the resulting dictionary holding all of the counts. For example:
from collections import Counter
import csv
import json
all_smells = ['LazyClass', 'ComplexClass', 'LongParameterList', 'FeatureEnvy', 'LongMethod', 'GodClass', 'MessageChain']
my_map = {}
with open('result_smells.json') as f_json:
json_data = json.load(f_json)
for entry in json_data:
my_map[entry["fullyQualifiedName"]] = []
#adding all class smells
for class_smell in entry["smells"] :
my_map[entry["fullyQualifiedName"]].append(class_smell)
#adding all methods smells
for method in entry["methods"] :
for method_smell in method["smells"] :
my_map[entry["fullyQualifiedName"]].append(method_smell)
with open('output.csv', 'w', newline='') as f_output:
csv_output = csv.DictWriter(f_output, fieldnames=["NameOfClass", *all_smells], extrasaction='ignore', restval='NaN')
csv_output.writeheader()
for elem in my_map :
smell_counts = Counter()
for smell in my_map[elem] :
smell_counts[smell["name"]] += 1
smell_counts['NameOfClass'] = elem
csv_output.writerow(smell_counts)
Giving you an output CSV file looking like:
NameOfClass,LazyClass,ComplexClass,LongParameterList,FeatureEnvy,LongMethod,GodClass,MessageChain
com.nirhart.shortrain.MainActivity,NaN,NaN,NaN,NaN,NaN,NaN,1
com.nirhart.shortrain.path.PathParser,NaN,1,2,1,1,NaN,NaN
com.nirhart.shortrain.path.PathPoint,1,NaN,1,NaN,NaN,NaN,NaN
com.nirhart.shortrain.path.TrainPath,NaN,NaN,NaN,1,NaN,NaN,NaN
com.nirhart.shortrain.rail.RailActionActivity,NaN,NaN,NaN,1,1,NaN,NaN
com.nirhart.shortrain.rail.RailInfo,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.train.TrainActionActivity,NaN,1,1,1,2,NaN,2
com.nirhart.shortrain.train.TrainDirection,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.train.TrainView,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.tutorial.TutorialFragment,NaN,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.tutorial.TutorialFragment.OnNextSlideClicked,1,NaN,NaN,NaN,NaN,NaN,NaN
com.nirhart.shortrain.tutorial.TutorialViewPagerAdapter,1,NaN,NaN,1,NaN,NaN,NaN
com.nirhart.shortrain.utils.ShortcutsUtils,NaN,NaN,1,NaN,NaN,NaN,2

JSON file to dataframe conversion-ValueError: Unexpected character found when decoding array value (2)

I have a huge json file having the format of the form:
[{"faceId": "2cb5a26a-1acc-4eb2-8c39-8e05e604f057", "faceRectangle": {"top":
54, "left": 125, "width": 78, "height": 78}, "faceAttributes": {"smile":
0.584, "headPose": {"pitch": 0.0, "roll": -2.4, "yaw": 1.4}, "gender":
"male", "age": 34.4, "facialHair": {"moustache": 0.5, "beard": 0.6,
"sideburns": 0.3}, "glasses": "NoGlasses", "emotion": {"anger": 0.0,
"contempt": 0.003, "disgust": 0.0, "fear": 0.0, "happiness": 0.584,
"neutral": 0.413, "sadness": 0.0, "surprise": 0.0}, "blur": {"blurLevel":
"high", "value": 1.0}, "exposure": {"exposureLevel": "goodExposure",
"value": 0.61}, "noise": {"noiseLevel": "low", "value": 0.0}, "makeup":
{"eyeMakeup": false, "lipMakeup": false}, "accessories": [{"type":
"headwear", "confidence": 1.0}], "occlusion": {"foreheadOccluded": true,
"eyeOccluded": false, "mouthOccluded": false}, "hair": {"bald": 0.0,
"invisible": true, "hairColor": []}}}]
I am trying to convert this to a dataframe by using the following code:
import pandas as pd
ent_json_file = 'ent.json'
reading_json = pd.read_json(ent_json_file,convert_dates=True,lines=True)
I am getting the following error:
ValueError: Unexpected character found when decoding array value (2)
Any help is greatly appreciated!
Thanks!
Your error seems to be coming from an invalid JSON format.
I've found that online JSON lints are a quick and easy way to figure out what's going on with a JSON file:
https://jsonlint.com/

Categories