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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:12:32+00:00 2026-06-10T12:12:32+00:00

I have the following string: ‘{ key1: val1, key2: [a,b, 3], key3: {foo: 27,

  • 0

I have the following string:

'{
    "key1": "val1",
    "key2": ["a","b", 3],
    "key3": {"foo": 27, "bar": [1, 2, 3]}
}'

I want to parse only one level so result should be a one level dictionary with key, and value should be just a string(don’t need to parse it)

For given string it should return following dictionary:

{
    "key1": "val1",
    "key2": "['a','b', 3]",
    "key3": "{'foo': 27, 'bar': [1, 2, 3]}"
}

Is there a fast way to do it? Without parsing whole string to json and convert all values back to strings.

  • 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-10T12:12:33+00:00Added an answer on June 10, 2026 at 12:12 pm

    I think you can solve this using regex, it is working for me:

    import re
    pattern = re.compile('"([a-zA-Z0-9]+)"\s*:\s*(".*"|\[.*\]|\{.*\})')    
    dict(re.findall(pattern, json_string))
    

    But I dont know if this is faster, you need try using your data.

    [EDIT]

    Yes, it is faster. I tried the scripts below and the regex version is 5 times faster.

    using json module:

    import json
    
    val='''
    {
        "key1": "val1",
        "key2": ["a","b", 3],
        "key3": {"foo": 27, "bar": [1, 2, 3]}
    }
    '''
    
    for n in range(100000):
        dict((k,json.dumps(v)) for k,v in json.loads(val).items())
    

    using regex:

    import re
    
    val='''{
        "key1": "val1",
        "key2": ["a","b", 3],
        "key3": {"foo": 27, "bar": [1, 2, 3]}
    }'''
    
    pattern = re.compile('"([a-zA-Z0-9]+)"\s*:\s*(".*"|\[.*\]|\{.*\})')    
    for n in range(100000):
        dict(re.findall(pattern, val))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following: string value = 9223372036854775807; double parsedVal = double.Parse(value, CultureInfo.InvariantCulture); ...
I have the following String that I need to parse. I am looking for
I have following string task BLABLA@{taskId} @{BLABLA.title} and want to extract all placeholders from
I have following string: xxxxx GL=>G0 yyyyyy I want to extract GL and G0
I have following code: string date = 13.04.2012; string date2 = (DateTime.Parse(date).AddDays(1)).ToString(); This is
I have the following string and i want to derive the number ( 104321
I have following string adId:4028cb901dd9720a011e1160afbc01a3;siteId:8a8ee4f720e6beb70120e6d8e08b0002;userId:5082a05c-015e-4266-9874-5dc6262da3e0 I need only the value of adId,siteId and userId.
I have following string hello[code:1], world [code:2], hello world I want to replace this
I have following string, I want to convert it to DataTable Id,Name ,Dept\r\n1,Mike,IT\r\n2,Joe,HR\r\n3,Peter,IT\r\n I
I have following string content \ content <tag attr1=\val1\ attr2=\val2\>content \ content</tag> content \

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.