Valar Morghulis
Valar Morghulis

Reputation: 25

How to add a string to JSON list in python

Here is my code sample:

    try: 
        REST_Call = Session.get(CC_URL_REST) #Getting the session for a particular url.
        REST_CALL = REST_Call.content #Retrieving the contents from the url.
        JSON_Data = json.loads(REST_CALL) #Loading data as JSON.
        Report_JSON.append(JSON_Data) #Appending the data to an empty list

The JSON data that is returned and appended to the 'Report_JSON' is:

             [
               {
                  "content": [
                     {
                        "id": 001,
                        "name": "Sample_Name",
                        "description": "Sample_description",
                        "status": "STARTED",
                        "source": null,
                        "user": "username"
                     }
                },
              ],

I just want to add the below data in string format to the above JSON list:

{
    "CronExpression": "0 1,30 4,5,6,7,8,9,10,11,12,13,14 ? * 2,3,4,5,6"
},

Sample code for the above string data:

        Cron_Call = Session.get(Cron_URL_REST)
        Cron_CALL = Cron_Call.content
        Cron_Data = json.loads(Cron_CALL)
        cron_value = Cron_Data["cronExpression"] 

        Report_JSON.append({
                    "CronExpression": cron_value
        })

When trying to append it to the 'Report_JSON' this is the output I get:

             [
               {
                  "content": [
                     {
                       "id": 001,
                       "name": "Sample_Name",
                       "description": "Sample_description",
                       "status": "STARTED",
                       "source": null,
                       "user": "username"
                     }
                },
              ],
              {
              "CronExpression": "0 1,30 4,5,6,7,8,9,10,11,12,13,14 ? * 2,3,4,5,6"
              },

I'm trying to show both the data's under the same "content" tab unlike it being separate.

This is the result i'm trying to get:

       {
            "id": 001,
            "name": "Sample_Name",
            "description": "Sample_description",
            "status": "STARTED",
            "source": null,
            "user": "username"
            "CronExpression": "0 1,30 4,5,6,7,8,9,10,11,12,13,14 ? * 2,3,4,5,6"
        },

Any ideas on how to implement it?

Upvotes: 1

Views: 868

Answers (2)

Barmar
Barmar

Reputation: 781706

Loop over JSON_Data['content'] and add the new key to each of them.

Cron_Call = Session.get(Cron_URL_REST)
Cron_CALL = Cron_Call.content
Cron_Data = json.loads(Cron_CALL)
cron_value = Cron_Data["cronExpression"] 
for x in JSON_DATA['content']:
    x['CronExpression'] = cron_value

Upvotes: 1

charlesreid1
charlesreid1

Reputation: 4841

Here, Report_JSON is loaded as a list type in Python (JSON data can be interpreted by Python as either a list, if it is surrounded by [] square brackets, or a dict if it is surrounded by {} curly brackets).

When you call Report_JSON.append(), it will append a new item to the list. You are creating a new dictionary with a single key-value pair (CronExpression) and adding it to the list, which is why the two dictionaries are side-by-side.

What you should do instead is get the first item in the Report_JSON list, which is the dictionary; then ask for the value corresponding to the content key, which will be a list; then ask for the first item in that list, which will be the dictionary you want to modify (with keys id, name, description, etc.)

Modify that dictionary, then put it back in the list. Here's the code that will do that:

# Get first item in Report_JSON list
content_dict = Report_JSON[0]

# Get the value corresponding to the 'content' key, which is a list
content_value = content_dict['content']

# Get the first item in the list, which is the dict you want to modify
dict_to_modify = content_value[0]

# Now add your new key-value pair
dict_to_modify['CronExpression'] = "0 1,30 4,5,6,7 ..."

Or, to do it in one shot:

Report_JSON[0]['content'][0]['CronExpression'] = "0 1,30 4,5,6,7 ..."

UPDATE: If the "content" list has multiple items, you can iterate over each item in that list:

for content_dict in Report_JSON[0]['content']:
    content_dict['CronExpression'] = "0 1,30 4,5,6,7 ..."

which will result in something like this:

             [
               {
                  "content": [
                     {
                       "id": 001,
                       "name": "Sample_Name",
                       "description": "Sample_description",
                       "status": "STARTED",
                       "source": null,
                       "user": "username",
                       "CronExpression": "0 1,30 4,5,6,7 ..."
                     },

                     {
                       "id": 002,
                       "name": "Another_Sample_Name",
                       "description": "Another_sample_description",
                       "status": "STARTED",
                       "source": null,
                       "user": "another_username",
                       "CronExpression": "0 1,30 4,5,6,7 ..."
                     },
                   ]
                },
              ],

UPDATE 2: If you are not interested in keeping the original structure and you want to strip everything up to and including the "content" key, you can just do this to start off:

Report_JSON = Report_JSON[0]['content']

and Report_JSON is now just the inner "content" list.

Upvotes: 1

Related Questions