Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:33:45+00:00 2026-06-15T03:33:45+00:00

I have written a Python script that outputs a long data structure (dictionary i

  • 0

I have written a Python script that outputs a long data structure (dictionary i called “celldict”) in Json format. Here’s a small part of it :

{
    "1224": {
        "OUT3FA_5": 12,
        "IN1": 37,
        "Total_IN1": 37
    },
    "1225": {
        "OUT3FA_5": 24,
        "IN1": 59,
        "Total_IN1": 22
    }
}

But what I would like to do is have something like this :

{
    "success": true,
    "data": [
        {
            "Week":"1224",
            "OUT3FA_5": 65,
            "IN1": 85,
            "Total_IN1": 100
        },
        {
            "Week":"1225",
            "OUT3FA_5": 30,
            "IN1": 40,
            "Total_IN1": 120
        }
    ]
}

Is there a way to format the json output with Python to get I what I want?
I do:

print json.dumps(celldict)

to get my output.
Any help would be much much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:33:46+00:00Added an answer on June 15, 2026 at 3:33 am

    Just put celldict inside another dict:

    json.dumps({'success': True, 'data': celldict.values()})
    

    You’ll have to add the Week key to the celldict dictionaries first:

    for d in celldict.itervalues():
        celldict['Week'] = '1238'
    

    or use create a copy of each dict on-the-fly:

    json.dumps({'success': True, 'data': [dict(d, Week='1238') for d in celldict.values()]})
    

    The latter method, with some indentation, produces:

    >>> print json.dumps({'success': True, 'data': [dict(d, Week='1238') for d in celldict.values()]}, indent=4)
    {
        "data": [
            {
                "OUT3FA_5": 24, 
                "Week": "1238", 
                "Total_IN1": 22, 
                "IN1": 59
            }, 
            {
                "OUT3FA_5": 12, 
                "Week": "1238", 
                "Total_IN1": 37, 
                "IN1": 37
            }
        ], 
        "success": true
    }
    

    Reading between the lines, it seems as if the 1224 and 1225 keys in your input example are actually the week numbers you are referring to. If so, they are easily incorporated:

    json.dumps({'success': True, 'data': [dict(d, Week=k) for k, d in celldict.iteritems()]})
    

    would produce:

    {
        "data": [
            {
                "OUT3FA_5": 24, 
                "Week": "1225", 
                "Total_IN1": 22, 
                "IN1": 59
            }, 
            {
                "OUT3FA_5": 12, 
                "Week": "1224", 
                "Total_IN1": 37, 
                "IN1": 37
            }
        ], 
        "success": true
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Python script that will generate a series of graphs and
I have a mechanize script written in python that fills out a web form
I have written a Python script for merging many data files in a few
I have written a short python script that opens Google music in web view
I have written up a python script that allows a user to input a
I have written a Python script that checks a certain e-mail address and passes
I have written a Python script that currently is designed to handle traditional CGI
I have written a Python script that takes a 1.5 G XML file, parses
I have written a python script (my very first) that accepts input of a
I am new to python and i have written a script that converts a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.