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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:19:06+00:00 2026-06-17T19:19:06+00:00

I have a JSON file with numerous entries like this: { area1: California, area2:

  • 0

I have a JSON file with numerous entries like this:

    {
    "area1": "California",
    "area2": "Sierra Eastside",
    "area3": "Bishop Area",
    "area4": "Volcanic Tablelands (Happy/Sad Boulders)",
    "area5": "Fish Slough Boulders",
    "grade": "V6 ",        
    "route": "The Orgasm",
    "type1": "Boulder",
    "type2": "NONE",
    "type3": "NONE",
    "type4": "NONE",
},

I want to take the area and type entries and turn them into arrays:

   {
    "area": ["California","Sierra Eastside","Bishop Area","Volcanic Tablelands (Happy/Sad Boulders)","Fish Slough Boulders"]
    "grade": "V6 ",        
    "route": "The Orgasm",
    "type": ["Boulder","NONE","NONE","NONE"]
},

I have this code which almost works:

json_data=open('../json/routes_test.json')
datas = json.load(json_data)
datas_arrays = []
area_keys = ['area1','area2','area3','area4','area5']
type_keys = ['type1','type2','type3','type4']

for data in datas:
    areaArray = []
    typeArray = []
    deleteArray = []
    for k, v in data.iteritems():
        for area_key in area_keys:
            if (k == area_key):
                areaArray.append(v)
                deleteArray.append(k)
        for type_key in type_keys:
            if (k == type_key):
                typeArray.append(v)
                deleteArray.append(k)
    for k in deleteArray:
        del data[k]
    data['area'] = areaArray
    data['type'] = typeArray
    datas_arrays.append(data)
    print datas_arrays
    print "********"

out = json.dumps(datas_arrays, sort_keys=True,indent=4, separators=(',', ': '))
print out
f_out= open('../json/toues_test_intoarrays.json', 'w')    
f_out.write(out)
f_out.close()   

The problem is that the area array is all out of order and the type array is backwards, which I can’t have. I find it strange that one is unordered and one is ordered but backwards. To me it seems like the iteration should assure they’re placed in order.

  • 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-17T19:19:08+00:00Added an answer on June 17, 2026 at 7:19 pm

    Python dictionaries have an arbitrary ordering, they are not sorted. You want to use your prebuilt lists of keys instead:

    with open('../json/routes_test.json') as json_data:
        datas = json.load(json_data)
        area_keys = ['area1','area2','area3','area4','area5']
        type_keys = ['type1','type2','type3','type4']
    
        for data in datas:
            data['area'] = [data[k] for k in area_keys]
            data['type'] = [data[k] for k in type_keys]
            for k in area_keys + type_keys:
                del data[k]
    
    out = json.dumps(datas, sort_keys=True, indent=4, separators=(',', ': '))
    print out
    with open('../json/toues_test_intoarrays.json', 'w') as f_out:
        f_out.write(out)
    

    which changes the dictionaries in-place.

    You could even determine the area and type keys from each entry:

        for data in datas:
            keys = sorted(data.keys())
    
            area_keys = [k for k in keys if k.startswith('area')]
            data['area'] = [data[k] for k in area_keys]
    
            type_keys = [k for k in keys if k.startswith('type')]
            data['type'] = [data[k] for k in type_keys]
    
            for k in area_keys + type_keys:
                del data[k]
    

    and omit the list literals with the 'area1', 'area2' etc. hardcoded lists altogether.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a json file exported from phpmyadmin, it looks like this(utf-8 file): [{user_email:
I have a Json file that looks like this: [ { field:val }, ....
I have a json file which contains questions and answers like this. { Questions:
I have a json file with contents like this: { aaa:{ status:available, classkey:dotnet },
I have a json file that looks like this: { status: 200, data: {
I have a JSON file like this: { wijken: { 11: { coords: 3.79073170001967,51.1717753664505,0
I have a json file like this {downloads:[ { url:arquivo1.pdf, descricao:árquivo 1 }, {
I have a JSON file which contains a pound symbol in it. This json
i have a json file with many elements like these: { code : hfuiew89,
I have this json file. Part of it is given below : { has_more

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.