I started with JSON and Python 3.x... So there are still some problems. This is now my first important problem I have.
I'm looking for a function which can insert (for example) this:
{
'Current': '3A',
'Voltage': '11V'
}
In to here:
{
'Measure': [
{'Current': '2A', 'Voltage': '11V'}
]
}
...so that my result is:
{
'Measure': [
{'Current': '2A', 'Voltage': '11V'},
{'Current': '3A', 'Voltage': '11V'}
]
}
I only found functions in JavaScript to solve my problem, in Python not. Till now, I saw only a hierarchical creation method for JSON and nothing like a class and instance build up.
I hope, there are opportunities to do it. Any ideas?
My solution for your problem is to Keep the data as Python- Dictionary then do operations on it, later you can get JSON Objects.
import json
j = {'Measure': [ {'Current': '2A', 'Voltage': '11V'}]}
d = {'Current': '3A', 'Voltage': '11V'}
j['Measure'].append(d) # as it is list use append otherwise update
json.dumps(j)
Output:
'{"Measure": [
{"Current": "2A", "Voltage": "11V"},
{"Current": "3A", "Voltage": "11V"}
]}'
EDIT-1:
First append data to child dictionary then use dict.update to parent dictionary Hence solved.
EDIT-2
Exact answer is
j['Device']['Measure'].append({'Current': '3A', 'Voltage': '11V'})
it has to work.
Hope it is helpful ..!!