Skip to main content
sanjay10898
Silver ⭐️⭐️
Jan 23, 2020 2:51 PM UTC
Solved

Not able to parse text file to json format

Hi folks, i have problem in parsing json textfile.

what i have done is, I sent a text file which is in a json architecture to PutMongoRecord To parse that file into json but it is not getting parsed.

i have attached the configuration done by me.

please help me in this problem.

schemaschemaJsonTreeReaderJsonTreeReaderputmongoputmongo's configurationthe error which we gotthe error which we gottextfile senttextfile sent

stevenmatison
Platinum ⭐️⭐️
Jan 24, 2020 12:11 PM UTC

The FlowFile is not valid json.   

 

Try something like a basic json example found on the internet:

 

{"results":[{"term":"term1"},{"term":"term2"}..]}

 Notice the object results is the array of values and everything is "quoted".

 

Something like this based on your sample image:

{ "world_rank": "1", "country": "China", "population": "1388232694", "World": "0.185" }

  

If you have to use what you have in the sample image, then you will need to extracText (link here for examples of regex to match values) to get the values for world_rank, country, population, world.

 

Each values attribute regex like this:

 

 

.*world_rank :(.*?) .*
.*country: (.*?) .*
.*population: (.*?) .*
.*world: (.*?) .*

 

 

Then use attributesToJson to build the Json object of the attributes you defined above which you can then send to the reader and ultimately to MongoDb.

 

 

If my reply helps you solve your Use Case, please accept it as a solution to close the topic.  

5 replies

Lewis
Silver ⭐️⭐️
Jan 23, 2020 3:28 PM UTC

You can find the indices and matches of the header with the finditer of the re package. Then, use that to process the rest:

import reimport json

thefile
= open("file.txt")line = thefile.readline()iter = re.finditer("\w+\s+", line)columns = [(m.group(0), m.start(0), m.end(0)) for m in iter]records = []
while line: line = thefile.readline() record = {}
for col in columns: record[col[0]] = line[col[1]:col[2]] records.append(record)

print(json.dumps(records))

Translate
stevenmatison
Platinum ⭐️⭐️
Jan 24, 2020 12:11 PM UTC

The FlowFile is not valid json.   

 

Try something like a basic json example found on the internet:

 

{"results":[{"term":"term1"},{"term":"term2"}..]}

 Notice the object results is the array of values and everything is "quoted".

 

Something like this based on your sample image:

{ "world_rank": "1", "country": "China", "population": "1388232694", "World": "0.185" }

  

If you have to use what you have in the sample image, then you will need to extracText (link here for examples of regex to match values) to get the values for world_rank, country, population, world.

 

Each values attribute regex like this:

 

 

.*world_rank :(.*?) .*
.*country: (.*?) .*
.*population: (.*?) .*
.*world: (.*?) .*

 

 

Then use attributesToJson to build the Json object of the attributes you defined above which you can then send to the reader and ultimately to MongoDb.

 

 

If my reply helps you solve your Use Case, please accept it as a solution to close the topic.  

Translate
sanjay10898
Silver ⭐️⭐️
Jan 25, 2020 7:35 AM UTC

Hello,

I need to send 

{"value" : [{"id1":"10"}]}

for the above data what must be its schema ?so that it must store into database in such a way 

id1=10

thanks in advance!!

 

 

Translate
sanjay10898
Silver ⭐️⭐️
Jan 25, 2020 7:45 AM UTC

how to configure this  because i get "could not parse to json"how to configure this because i get "could not parse to json"

Translate