Python convert Json string values to int float boolean

by django   Last Updated April 15, 2019 10:26 AM

Objective: I am coming from javascript background. I am try to parse a json. json.loads is supposed to convert stringfied values into their relevant type.

How can it be done with python 3? Purpose is eval all values with relevant type

Code:

import json
x = '{ "name":"John", "age":30, "dev":"true", "trig":"1.0E-10", "res":"0.1"}'
y = json.loads(x)
print(y)

Current Output:

{
  "name": "John",
  "age": "30", 
  "dev": "true", 
  "trig": "1.0E-10", 
  "res": "0.1"
}

Expected output:

{
  "name": "John",
  "age": 30,       // int 
  "dev": true,     //  bool
  "trig": 1.0E-10, // real number
  "res": 0.1       // float
}
Tags : python json


Related Questions


Updated March 15, 2019 08:26 AM

Updated April 19, 2019 17:26 PM

Updated November 10, 2017 19:26 PM

Updated May 03, 2018 20:26 PM

Updated July 24, 2018 00:26 AM