Forming a JSON Arrays with Objects using a Python object

rgamber

I have a some variables that I need to dump as a json of the format:

{  
  "elements":[  
      {  
          "key":"foo",
          "value":"7837"
      },
      {  
          "key":"bar",
          "value":"3423"
      }
  ]
}

I am trying to figure out the right object which would give the above structure upon usin json.dumps(). I see that in python, lists give a json array where as dictionaries give a json object while using json dumps.

I am trying something like:

x={}
x["elements"]={}
x["elements"]["key"]="foo"
x["elements"]["value"]="7837"
x["elements"]["key"]="bar"
x["elements"]["value"]="3423"
json_x=json.dumps(x)

But this still gives me:

{"elements": {"key": "bar", "value": "3423"}} 

which is obviously incorrect.

How to I incorporate the correct dictionary and list structure to get to the above json?

falsetru

Why don't you just use literal?

x = {
    "elements": [
        {"key":"foo", "value":"7837"},
        {"key":"bar", "value":"3423"}
    ]
}

To fix your code, you need to use a list literal ([]) when assigning to elements dictionary entry:

>>> x = {}
>>> x["elements"] = []  # <--- 
>>> x["elements"].append({})
>>> x["elements"].append({})
>>> x["elements"][0]["key"]="foo"
>>> x["elements"][0]["value"]="7837"
>>> x["elements"][1]["key"]="bar"
>>> x["elements"][1]["value"]="3423"
>>> json.dumps(x)
'{"elements": [{"value": "7837", "key": "foo"}, {"value": "3423", "key": "bar"}]}'

But, it's hard to read, maintain.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Forming json with nested objects using STUFF function

Issue with forming a Json Model in Python

How to merge multiple json objects into a single json object using python

Dynamically forming array of object using given List

Explode Complex JSON Object (with Objects and Arrays) with jq

Forming a cURL command using string concatenation in Python

Using JSON.Simple with nested objects and arrays

Forming an array with elements in specific positions from multiple arrays in Python

How to sort JSON objects of arrays in python?

Picking data objects out of json arrays in Python

Using python range objects to index into numpy arrays

Avoid using arrays (and [0]) in multidimensional JSON object

Duplicate object values while merging the JSON Objects arrays in Ansible

How to use Jackson to deserialise arrays of objects from json object

How to fetch specific object from json containing arrays of objects

Array of Objects or Object with arrays?

Nested Objects into Arrays into an Object

Object of arrays into array of objects

Forming nested JSON from POJO's using Jackson

Conversion of JSON object of objects using GSON

Combine multiple arrays containing objects using object property values

JSON - array of objects into objects of arrays

Serializing object references inside objects in python json

How to access Object of Objects as JSON in Python?

Forming Json Format String

forming letters out of binary arrays

How to read a single object from a json file containing multiple objects using python?

How to only interact with objects that are after specific object value in Json using Python?

Using JSON.simple to create objects and arrays with key and value