How to import json file to mongodb using pyspark or python

0 votes

I'm try to import json in the file to mongodb using pyspark after connection pyspark with mongodb, I'm using Ubuntu, my file in (file:///home/user/Downloads/newdb/hale.json) I write it like this ('home/user/Downloads/newdb/hale.json'),

I want to try using this code but in JSON

df = spark.read.csv(path = '/home/ahmad/Downloads/newdb/hale.csv', header=True, inferSchema=True)

??

Sep 8, 2019 in Python by Ahmed
• 310 points

edited Sep 9, 2019 by Omkar 2,091 views

1 answer to this question.

0 votes

You can use the same format as in your question but replace csv with json. 

df = spark.read.json(path = '/home/ahmad/Downloads/newdb/hale.json', header=True, inferSchema=True)


Hope this helps!

To know more about Pyspark, it's recommended that you join PySpark online course.

Thanks.

answered Sep 9, 2019 by Karan
Thank you, I did this code but doesn't work :(
Hey @Ahmed. Can you share the error that you got?

Related Questions In Python

0 votes
0 answers

How to save the import csv file to mongodb using pyspark (or python)?

I have this code, and I want ...READ MORE

Oct 9, 2019 in Python by Ahmed
• 310 points
2,436 views
0 votes
1 answer

How to import an image into a csv file using python?

I am sure you are aware CSV ...READ MORE

answered Jun 25, 2020 in Python by Bhanu Kumar
7,978 views
0 votes
1 answer

How to download a file over HTTP using Python?

In Python 2, use urllib2 which comes ...READ MORE

answered Oct 18, 2018 in Python by SDeb
• 13,300 points
877 views
0 votes
1 answer

How to check latest change time of each file using glob in python?

You can get the changing time from ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,220 points
1,657 views
+2 votes
1 answer
0 votes
1 answer

How to sort json file ascending in pymongo?

Hey Ahmad, you can get the output ...READ MORE

answered Aug 14, 2019 in Python by Trisha
1,145 views
0 votes
2 answers
+1 vote
1 answer

How to read hdfs file using python?

subprocess.Popen(["hadoop", "fs", "-cat", "/path/to/myfile"], stdou ...READ MORE

answered Dec 7, 2018 in Python by Omkar
• 69,220 points
5,646 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,720 points
13,620 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP