Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regex on json file
#11
(May-05-2020, 09:49 PM)senaint Wrote: I have no idea why I thought regex would be the solution to this.
it could, but str.split() looks simpler

as I said in my first post - iterate over it
import json
import os
   
rootfile = r'C:\\Users\\homer\\dumps\\cosmodb-sample.json'
with open(rootfile,'r') as file:
    json_data = json.load(file) 
    for item in json_data:
        item['id'] = item['id'].split('-$')[0]
        print(item['id'])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
(May-06-2020, 12:37 AM)senaint Wrote: so roughly speaking all the "id" keys are objects in a big array. I have tried various variation of the simple code below but I keep getting TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper
import json
import os
  
rootfile = r'C:\\Users\\homer\\dumps\\cosmodb-sample.json'
with open(rootfile,'r') as file:
    json_data = json.loads(file) 

There are two similarly-named loaders: json.load() and json.loads().

The first loads from a file, the second loads from a string or similar (think "loadS" for "load-string").
Reply
#13
(May-06-2020, 03:58 AM)bowlofred Wrote: There are two similarly-named loaders: json.load() and json.loads().
hahaha, I copied their code and didn't notice it was loads. Now fixed in my snippet.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 65 Yesterday, 05:16 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 669 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,346 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,962 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  search file by regex SamLiu 1 860 Feb-23-2023, 01:19 PM
Last Post: deanhystad
  Split pdf in pypdf based upon file regex standenman 1 1,975 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,173 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,222 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Trying to parse only 3 key values from json file cubangt 8 3,338 Jul-16-2022, 02:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020