Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically build a JSON object?

Tags:

python

json

I am new to Python and I am playing with JSON data. I would like to dynamically build a JSON object by adding some key-value to an existing JSON object.

I tried the following but I get TypeError: 'str' object does not support item assignment:

import json

json_data = json.dumps({})
json_data["key"] = "value"

print 'JSON: ', json_data
like image 740
Backo Avatar asked Apr 16 '14 13:04

Backo


People also ask

Can JSON be dynamic?

A dynamic JSON file will be created to store the array of JSON objects. Consider, we have a database named gfg, a table named userdata. Now, here is the PHP code to fetch data from database and store them into JSON file named gfgfuserdetails. json by converting them into an array of JSON objects.

How do you create a JSON object in Python?

In Python, you can create JSON string by simply assigning a valid JSON string literal to a variable, or convert a Python Object to JSON string using json. loads() function.

How do you create a JSON object from a string in Python?

To convert string to json in Python, use the json. loads() function. The json. loads() is a built-in Python function that accepts a valid json string and returns a dictionary to access all elements.

How to create a dynamic JSON array in JavaScript?

This is one of most common scenarios and you’ll see two ways of creating JSON array dynamically. The first method will use for loop for creating JSON array from an input array. The second method will make use of the JavaScript reduce method to create dynamic JSON array. How JavaScript Variables are Stored in Memory? What Does ...

What is a JSON object?

"JSON" stands for "JavaScript Object Notation" - it's just a specification for representing some data in a string that explicitly mimics JavaScript object (and array, string, number and boolean) literals. You're trying to build up a JavaScript object dynamically - so the word you're looking for is "object".

How do I convert JSON data to a Python object?

json.dumps take a dictionary as input and returns a string as output. If you need to convert JSON data into a python object, it can do so with Python3, in one line without additional installations, using SimpleNamespace and object_hook:

What is JSON and why should I Care?

First, I think you're calling it the wrong thing. "JSON" stands for "JavaScript Object Notation" - it's just a specification for representing some data in a string that explicitly mimics JavaScript object (and array, string, number and boolean) literals.


3 Answers

You build the object before encoding it to a JSON string:

import json

data = {}
data['key'] = 'value'
json_data = json.dumps(data)

JSON is a serialization format, textual data representing a structure. It is not, itself, that structure.

like image 129
Martijn Pieters Avatar answered Oct 20 '22 17:10

Martijn Pieters


You can create the Python dictionary and serialize it to JSON in one line and it's not even ugly.

my_json_string = json.dumps({'key1': val1, 'key2': val2})
like image 37
sscarduzio Avatar answered Oct 20 '22 15:10

sscarduzio


There is already a solution provided which allows building a dictionary, (or nested dictionary for more complex data), but if you wish to build an object, then perhaps try 'ObjDict'. This gives much more control over the json to be created, for example retaining order, and allows building as an object which may be a preferred representation of your concept.

pip install objdict first.

from objdict import ObjDict

data = ObjDict()
data.key = 'value'
json_data = data.dumps()
like image 23
innov8 Avatar answered Oct 20 '22 15:10

innov8