Python 2.7 Isolate multiple JSON objects in a string

2024/7/5 12:18:20

I've to parse a text file that contains different kind of data. The most challenging is a line that contains three different JSON object (strings) and other data between them. I've to divide the Json data from the rest. The good thing is this: every Json object start with a name. The issue I'm having with regex is isolate the first Json string obj from the others, and parse it using json. Here my solution (it works) but i bet there is something better... I'm not good in regex yet.

#This parse a string for isolate the first JSON serialized Object.
def get_json_first_end(text):ind_ret = 0ind1 = 0for i,v in enumerate(text):if v == '{':ind1 = ind1 + 1if v == '}':ind1 = ind1 - 1if ind1 == 0:ind_ret = ibreakreturn ind_ret#This return a string that contain the JSON object
def get_json_str(line,json_name):js_str = ''if re.match('(.*)' + json_name + '(.*)',line):#Removing all spurious data before and after the Json objdata = re.sub('(.*)'+ json_name,'',line)ind1 = data.find('{')ind2 = data.rfind('}')ind3 = get_json_first_end(data[ind1:ind2+1])js_str = data[ind1:ind3+2]return js_str

If i don't call get_json_first_end the ind2 can be wrong if there are multiple json strings in the same line. The get_json_str return a string with the JS string obj I want and I can parse it with json without issues. My question is: there is a better way to do this? get_json_first_end seems quite ugly. Thanks

Update: here an example line:

ConfigJSON ["CFG","VAR","1","[unused bit 2]","[unused bit 3]","[unused bit 4]","[unused bit 5]"] 2062195231AppTitle "Fsdn" 3737063363Bits ["RESET","QUICK","KILL","[unused bit 2]","[unused bit 3]","[unused bit 4]","[unused bit 5]"] 0837383711CRC 33628 0665393097ForceBits {"Auxiliary":[{"index":18,"name":"AUX1.INPUT"},{"index":19,"name":"AUX2.INPUT"}],"Network":[{"index":72,"name":"INPUT.1"}],"Physical":[]}
Answer

Your string is custom format. It may be possible to do with regex. I have tried with simple loop. You need to find open bracket [ or }, get corresponding closing bracket ] or }.

>>>string = '["CFG","VAR","1","[unused bit 2]","[unused bit 3]","[unused bit 4]","[unused bit 5]"] 2062195231AppTitle "Fsdn" 3737063363Bits ["RESET","QUICK","KILL","[unused bit 2]","[unused bit 3]","[unused bit 4]","[unused bit 5]"] 0837383711CRC 33628 0665393097ForceBits {"Auxiliary":[{"index":18,"name":"AUX1.INPUT"},{"index":19,"name":"AUX2.INPUT"}],"Network":[{"index":72,"name":"INPUT.1"}],"Physical":[]}'>>> def getjson(string):square = ['[',']']curly = ['{','}']count = 0json_list = []character = ''complement_character = ''start = 0end = 0for i in range(len(string)):if not character:if string[i] is square[0]:character = square[0] complement_character = square[1]start = icount += 1elif string[i] is curly[0]:character = curly[0]complement_character = curly[1]start = icount += 1else:# when character [ or { is found find corresponding ] or } using count.if string[i] is character:count += 1elif string[i] is complement_character:count -= 1if count == 0 and character :character = ''complement_character = ''end = i+1json_list.append(json.loads(string[start:end]))return json_list>>> print getjson(string)
[[u'CFG', u'VAR', u'1', u'[unused bit 2]', u'[unused bit 3]', u'[unused bit 4]', u'[unused bit 5]'], [u'RESET', u'QUICK', u'KILL', u'[unused bit 2]', u'[unused bit 3]', u'[unused bit 4]', u'[unused bit 5]'], {u'Physical': [], u'Auxiliary': [{u'index': 18, u'name': u'AUX1.INPUT'}, {u'index': 19, u'name': u'AUX2.INPUT'}], u'Network': [{u'index': 72, u'name': u'INPUT.1'}]}]
https://en.xdnf.cn/q/120094.html

Related Q&A

pass different C functions with pointer arrays as the function argument to a class

I am trying to pass different functions which have pointers as arguments to a python function. One example of the input function as input parameter is the given normal function: Sample.pyxfrom cpython …

dynamic filter choice field in django

I am using django-filter to filter results on a page. I am able to use static choice filters like this:filters.pyFILTER_CHOICES = ( (, All States), (AL, Alabama), (AK, Alaska), (AZ, Arizona), #and so f…

How to print a table from a text file and fill empty spaces?

I have the following table in a txt file:|Client | Container weight | Country of Origin | Product code ||S4378 | 450 Ton | China | 457841 || | 350 Ton | Japan…

Open a file in python from 2 directory back

I want to read a file from 2 folders back.. with open(../../test.txt, r) as file:lines = file.readlines()file.close()I want to read from ../../ two folders back. but not work.. How i can do that ?

Do string representations of dictionaries have order in Python 3.4?

I know dictionaries themselves in Python do not have order. However, Im rather curious if when you call str() on a dictionary if it is always in the same order. It appears to be sorted (by key), no mat…

BeautifulSoup Scraping Results not showing

I am playing around with BeautifulSoup to scrape data from websites. So I decided to scrape empireonlines website for 100 greatest movies of all time. Heres the link to the webpage: https://www.empireo…

How to verify username and password from CSV file in Python?

I am doing a Python project where I have to verify my username and password from a csv file where the first two rows and columns have the username and password as hi.Current Code: answer = input("…

adding validation to answer in quiz gives wrong answers

I am a complete novice with Python and working on a multiple choice quiz that reads questions from a file and keeps a score that then writes to a file. Everything was working perfectly until I added v…

Why do I get None as the output from a print statement? [duplicate]

This question already has answers here:Why is "None" printed after my functions output?(7 answers)Closed 2 years ago.def print_name(name):print(name)print(print_name(Annabel Lee))Why do I ge…

How to collect tweets about an event that are posted on specific date using python?

I wish to collect all tweets containing specific keywords(ex:manchesterattack,manchester) that are posted about the manchester attack from 22may. Can anyone provide me a code to collect tweets using py…