Home > Blockchain >  How can I covert the string to list of dictionaries?
How can I covert the string to list of dictionaries?

Time:04-25

My json data looks like:

{
 "metadata": [
        "{'Node_id': 38,'Maxpressure': -10.97, 'Minimum_pressure': -22.03}", 
        "{'Node_id': 39,'Maxpressure': 23.83, 'Minimumpressure': 10.31,}"]    
 }

I want my data to look like:

{
 "metadata": [
        {'Node_id': 38,'Maxpressure': -10.97, 'Minimum_pressure': -22.03}, 
        {'Node_id': 39,'Maxpressure': 23.83, 'Minimumpressure': 10.31,}]    
 }

CodePudding user response:

import json

arr = [
 '{"Node_id": 38,"Maxpressure": -10.97, 
    "Minimum_pressure": -22.03}', 
 '{"Node_id": 39,"Maxpressure": 28.83, 
    "Minimum_pressure": 10.31}'
]

res_arr = []
for obj in arr:
   y = json.loads(obj)
   res_arr.append(y)

the result is a Python dictionary:

print(res_arr)

CodePudding user response:

You need to evaluate the strings in your metadata list which is easily done with:

from ast import literal_eval

J = {
    "metadata": [
        "{'Node_id': 38,'Maxpressure': -10.97, 'Minimum_pressure': -22.03}", 
        "{'Node_id': 39,'Maxpressure': 23.83, 'Minimumpressure': 10.31,}"]    
 }

P = {'metadata': [literal_eval(s) for s in J['metadata']]}

print(P)

Output:

{'metadata': [{'Node_id': 38, 'Maxpressure': -10.97, 'Minimum_pressure': -22.03}, {'Node_id': 39, 'Maxpressure': 23.83, 'Minimumpressure': 10.31}]}

CodePudding user response:

If your string looks like

s = """
{
 "metadata": [
        "{'Node_id': 38,'Maxpressure': -10.97, 'Minimum_pressure': -22.03}",
        "{'Node_id': 39,'Maxpressure': 23.83, 'Minimumpressure': 10.31,}"]
 }
"""

then you can get your desired output using literal_eval from the ast module.

from ast import literal_eval

d = literal_eval(s)
# iterate over keys of dictionary d
for key in d:
    d[key] = [literal_eval(item) for item in d[key]]

print(d)

Notes


  • literal_eval(s) returns a dictionary
{'metadata': ["{'Node_id': 38,'Maxpressure': -10.97, 'Minimum_pressure': "
              '-22.03}',
              "{'Node_id': 39,'Maxpressure': 23.83, 'Minimumpressure': "
              '10.31,}']}
  • We iterate over the keys of the dictionary d just in case your actual data has more than just one key in the dictionary. In the sample input provided, there is only one key, "metadata".
  • d[key] is currently a list of strings.
  • We replace d[key] with a new list formed by applying literal_eval to each string in d[key].

Output


{'metadata': [{'Maxpressure': -10.97,
               'Minimum_pressure': -22.03,
               'Node_id': 38},
              {'Maxpressure': 23.83, 
               'Minimumpressure': 10.31, 
               'Node_id': 39}]}

Warning


You may have noticed that another way to achieve the above would have been to replace all the literal_eval's with eval. You should not be using the built-in eval unless you can be sure that you trust the input source. For this particular application, there is absolutely no need to use eval and you can achieve what you want without it. The literal_eval option is a safe alternative. From the literal_eval documentation, the function is used to

[s]afely evaluate an expression node or a string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None and Ellipsis.

This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.

CodePudding user response:

if You want convert json data into python dicionary.

first you declear metadata as variable after then use

json.loads(metadata)

this is method in which you get your data from json to dictionary.

  • Related