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 8960503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:40:21+00:00 2026-06-15T15:40:21+00:00

Currently I’m doing this: def getJSONString(lst): join = rs = { for i in

  • 0

Currently I’m doing this:

def getJSONString(lst):
    join = ""
    rs = "{"
    for i in lst:
        rs += join + '"' + str(i[0]) + '":"' + str(i[1]) + '"'
        join = ","
    return rs + "}"

which I call like:

rs = getJSONString([("name", "value"), ("name2", "value2")])

It doesn’t need to be nested (it’s only ever going to be a simple list of name value pairs). But I am open to calling the function differently. It all seems a bit cludgy, is there a more elegant way? This needs to run under 2.x.

Note that this is not a duplicate of: Python – convert list of tuples to string (unless that answer can be modified to create a JSON string as output).

edit: would it be better to pass the name value pairs as a dictionary?

  • 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-15T15:40:22+00:00Added an answer on June 15, 2026 at 3:40 pm

    There is a much better way to generate JSON strings: the json module.

    import json
    rs = json.dumps(dict(lst))
    

    This takes advantage of the fact that dict() can take a sequence of key-value pairs (two-value tuples) and turn that into a mapping, which the json module directly translates to a JSON object structure.

    Demonstration:

    >>> import json
    >>> lst = [("name", "value"), ("name2", "value2")]
    >>> rs = json.dumps(dict(lst))
    >>> print rs
    {"name2": "value2", "name": "value"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, Django 1.2.3 User model unicode is def __unicode__(self): return self.username and I'd like
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);
Currently I'm doing some unit tests which are executed from bash. Unit tests are
Currently I do like this: IndexSearcher searcher = new IndexSearcher(lucenePath); Hits hits = searcher.Search(query);
Currently my WPF application imports a part like this [Import(typeof(ILedPanel)] public ILedPanel Panel {
Currently, my ListViews look like this: How can I achieve that Windows 7 native
Currently, devise has the URLs as /users/sign_in although I'd like to change this to
Currently I have 2 ways of displaying images in a cell, which way will
Currently I have the following Mapping in my Controller: @RequestMapping( value = /add.html, method
Currently, Enum.Parse supports only the comma as the value separator, so that MemberOne,MemberThree will

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.