HOW TO PARSE DIFFERENT TYPES OF NESTED JSON USING PYTHON | DATA FRAME | TRICKS

Поділитися
Вставка
  • Опубліковано 16 сер 2020
  • This video will show 4 𝐝𝐢𝐟𝐟𝐞𝐫𝐞𝐧𝐭 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐣𝐬𝐨𝐧 examples and how to 𝐩𝐚𝐫𝐬𝐞 them. There will be json normalize, 𝐩𝐚𝐧𝐝𝐚𝐬 explode, recursion, iteration, date time formatting. This should help in your first experiences with nested json parsing. Turn on the 🔔 notification
    Join this channel to get access to perks:
    / @mrfugudatascience
    ➡ Patreon: / mrfugudatasci
    ➡ Buy Me A Coffee: www.buymeacoff...
    ➡ Github: github.com/MrFuguDataScience
    ➡ Twitter: @MrFuguDataSci
    ➡ Instagram: @mrfugudatascience
    Code From This Video:
    github.com/MrF...
    𝗥𝗲𝗳𝗲𝗿 𝗮 𝗙𝗿𝗶𝗲𝗻𝗱 𝗟𝗶𝗻𝗸 𝗭𝗮𝘇𝘇𝗹𝗲: refer.zazzlere...
    I will receive a small fee if you make a purchase on Zazzle of $25 or more
    𝗣𝗿𝗶𝗻𝘁𝗶𝗳𝘆 𝗥𝗲𝗳𝗲𝗿𝗿𝗮𝗹 𝗢𝗳𝗳𝗲𝗿: I get a small commission if you make 3 purchases
    try.printify.c...
    𝐕𝐢𝐝𝐞𝐨𝐬 𝐘𝐨𝐮 𝐌𝐚𝐲 𝐀𝐥𝐬𝐨 𝐋𝐢𝐤𝐞:
    ▶️ HOW TO PARSE RAW NESTED JSON TO DATAFRAME | TWITTER API | PYTHON: • HOW TO PARSE RAW NESTE...
    ▶️ PARSING EXTREMELY NESTED JSON: USING PYTHON | RECURSION: • PARSING EXTREMELY NEST...
    ▶️ CREATE NESTED (JSON) DICTIONARY: PYTHON, with pitfalls: • HOW TO CREATE NESTED J...
    ▶️ CONVERT NESTED JSON TO DATA FRAME WITH PYTHON CREATE FUNCTION TO STORE NESTED, UN-NESTED DATA: • HOW TO CONVERT NESTED ...
    Thumbnail: pixabay.com/us...
    subscribe gif thumbs: Gif youtube with hands: RoyBuri pixabay.com
    other end screen Gif at end: Moos-media on pixabay.com
    Music &. Intro Pic: Special Thanks
    Pixabay: instagram (subscribe gif): @imotivationitas
    Music: Oshóva - Tidal Dance on
    Soundcloud: / osh-va ,
    youtube: / @oshova9190
    #parsejson, #json, #mrfugudatascience
  • Наука та технологія

КОМЕНТАРІ • 57

  • @MrFuguDataScience
    @MrFuguDataScience  4 роки тому +3

    Let me know if there are any topics you have interest in.
    Join this channel to get access to perks:
    ua-cam.com/channels/bni-TDI-Ub8VlGaP8HLTNw.htmljoin
    Code From This Video:
    github.com/MrFuguDataScience/JSON/blob/master/Nested_Json_Mult_Ex.ipynb
    𝐀𝐦𝐚𝐳𝐨𝐧 𝐀𝐟𝐟𝐢𝐥𝐢𝐚𝐭𝐞 𝐋𝐢𝐧𝐤𝐬: (I receive a small commission on purchases)
    * Prices & Availability Subject to change
    --------------------------------------------
    Apple AirTag: amzn.to/3dNAZHM
    30 Free Trial Amazon Prime: amzn.to/3RhCKf9 (End Date: Dec 31, 2022 at 10:59 PM PST)
    Prime Student 6 Month Free Trial: amzn.to/3wgMXQz (End Date: On going)
    Audible Gift Membership: amzn.to/3pAfw7W (End Date: On Going)
    Try Audible: amzn.to/3PETRWS (End Date: On Going)
    Apple Certified Type C Charger & USB Wall Charger 20W with 2 cables: amzn.to/3dMdqPA
    𝐕𝐢𝐝𝐞𝐨𝐬 𝐘𝐨𝐮 𝐌𝐚𝐲 𝐀𝐥𝐬𝐨 𝐋𝐢𝐤𝐞:
    Here are a couple videos that you may like:
    CONVERT NESTED JSON TO DATA FRAME WITH PYTHON CREATE FUNCTION TO STORE NESTED, UN-NESTED DATA: ua-cam.com/video/FVECTpahzCQ/v-deo.html
    CREATE NESTED JSON DICTIONARY: PYTHON, with pitfalls: ua-cam.com/video/zhwmmjq1Nqg/v-deo.html
    CREATE USER CREDENTIALS PYTHON to MYSQL & POSTGRESQL | CONFIG FILES | .INI FILES: ua-cam.com/video/kIS58p9m9Io/v-deo.html

  • @krist17860
    @krist17860 2 роки тому +1

    This is probably the most useful, no clutter, instructions on using pandas to normalize complex nesting.

  • @JohnSmith-nc7hc
    @JohnSmith-nc7hc 3 роки тому +5

    This is great. This video helped me solve something I'd been working on for 4 days straight.
    I'm working on a public API and there JSON data was nested in different ways but had the same data. Keys were in different indentations. Pulled my hair out (literally) trying to solve for it.
    Thanks Mr. Fugu!

    • @MrFuguDataScience
      @MrFuguDataScience  3 роки тому +1

      Thanks for the feedback. Nested json can be so frustrating.

    • @MrSirPicsou
      @MrSirPicsou 6 місяців тому

      Same here. Public API is killing me.

  • @ketanchhatbar3309
    @ketanchhatbar3309 3 роки тому +1

    This is what i have been looking for. I was struggling to get the solution for last 2 days straight . Amazing video .

  • @alwaysWannaFlai
    @alwaysWannaFlai 3 роки тому +1

    I find myself can't understand publications so I open youtube instead. I knew I still have a lot to learn but, I didn't expect that there are vastly more to learn to be a data scientist. Thank you for your tutorials, wish me luck!

    • @MrFuguDataScience
      @MrFuguDataScience  3 роки тому +1

      So what is your background and what are you working on currently or brought you to my channel?

    • @alwaysWannaFlai
      @alwaysWannaFlai 3 роки тому +1

      @@MrFuguDataScience a final year student of statistics diploma, who is competing in his 1st hackathon XD

  • @shanakaj007
    @shanakaj007 3 роки тому +1

    recently came across your channel. you are a Genuis! I had a complex json and your 3rd or 4th example is exactly what was needed. Simple to understand and more better, it's a video!

    • @MrFuguDataScience
      @MrFuguDataScience  3 роки тому +1

      Thank you for the feedback, I am not a genius but, glad to help

  • @MsABCIndia
    @MsABCIndia 3 роки тому +2

    This is awesome.. thanks a lot

  • @typeer
    @typeer 2 роки тому +1

    tyvm mr fugu

    • @MrFuguDataScience
      @MrFuguDataScience  2 роки тому +1

      tyvm: thank you very much, I think that is the meaning. And no problems, I appreciate the feedback

  • @healingsounds9960
    @healingsounds9960 3 роки тому +1

    This was excellent !!!! Thanks m8. Subscribed as well.

  • @axisi5989
    @axisi5989 4 роки тому +2

    This video was so stunning

  • @agutierr1
    @agutierr1 2 роки тому +1

    🙏🙏 thank you for your data Kung Fu

  • @saranrajv3
    @saranrajv3 3 роки тому +1

    Nice one !! keep it up

  • @sheebajohnson7798
    @sheebajohnson7798 3 роки тому +1

    I have unflatten json with nested list inside. Is have any script to take some columns only.Not needed all coloumn values and converted into parquet. Also the columns name is not hard-coded. To handle dynamic schema

  • @deepaknaidu6552
    @deepaknaidu6552 Рік тому +1

    hi, i am dealing with a problem where i have a set of json files and i need to delete some of the content in it, how do i do it in a complex nested dictionary?

    • @MrFuguDataScience
      @MrFuguDataScience  Рік тому +1

      can you share what how the data are nested or an example of the data? If not I can suggest two things: if the data are in a dataframe then use json_normalize to flatten dictionaries and pandas explode to flatten the nested lists. Without seeing the data I would have a hard time to go further. Feel free to email me or type the structure as an example. Are the files all the same content or different?

  • @gurnoorsingh4108
    @gurnoorsingh4108 3 роки тому +1

    thanks bro!

  • @nb9797
    @nb9797 4 роки тому +1

    Nice vid. In example 1, how's the best way to flatten and view those keys with lists of dictionaries?
    I didn't see what the dataframe would look like if there was such a list longer than 1 item

    • @MrFuguDataScience
      @MrFuguDataScience  4 роки тому +1

      Iterate, if longer than one item and dump into DF or file of your choice. If list of dictionaries: either iterate or do recursion. Depends, If the values can be put into a DF. Then do the DF.explode and then json.normalize.
      I did an example in previous videos and also check out code : github.com/MrFuguDataScience/JSON
      I have a few files. Otherwise, send me exactly what you need help with and I can look at it

    • @MrFuguDataScience
      @MrFuguDataScience  4 роки тому +1

      I'm gonna make a specific video and it will be ready tomorrow for you. Just on this topic. I will parse lists of dictionaries and json values that are the same thing.

    • @MrFuguDataScience
      @MrFuguDataScience  4 роки тому +1

      here is a video I just made, see if it may help: ua-cam.com/video/KTJ3AQfRpN4/v-deo.html

  • @gustavemuhoza4212
    @gustavemuhoza4212 3 роки тому +1

    The tricky thing I had not been able to figure out now for 1 year (no exaggeration) was how you solved the skills column (as well as other lists in another dictionaries). I had left a similar column in my use case thinking it would be straightforward not knowing how the fact that the lengths are different would be tricky. Thank you. Thank you! Question: Is there an easier way to just create a column based on the Skills column that has, for example, True when someone has just C++ and Java but nothing else. So if someone has Java skills, that would be a True. If someone has C++ that would also be true. If someone has no skills, that will be False. If someone has Ruby, that would be False. If someone has C++ and Java that will also be true. But if someone has Java, C++ and Python, that will be False. Now I can do this by using the already created columns with 1's and 0s, but I thought you may have a better way of doing this (I tried using regular expressions and couldn't find another way of doing it). In any case, thanks a million for this video and others.

    • @MrFuguDataScience
      @MrFuguDataScience  3 роки тому +1

      let me review the code and video tonight and I will get back to you.

    • @gustavemuhoza4212
      @gustavemuhoza4212 3 роки тому +1

      @@MrFuguDataScience thank you so much.

    • @MrFuguDataScience
      @MrFuguDataScience  3 роки тому +2

      Well, if you want to go that route; I would suggest building a function to call in the data and do the boolean and store as a data frame and then send of to your database with new column. Or, create a function in your database to deal with this. But, that may be challenging and I would have to think about how to do that. I could make a video if necessary: but, it won't be immediate because I have 2 videos I want to put out real soon. Let me know if you need suggestions or actual code.

    • @gustavemuhoza4212
      @gustavemuhoza4212 3 роки тому +1

      What you made so far solves my problem at the moment and I would be more than happy to wait whenever you get the chance for a video of what you just described.

  • @amanmann5107
    @amanmann5107 3 роки тому +1

    Hi Just came across your vid and was trying to use a recursive approach but there seems to be a problem with your code. Every time I run this code on different datasets, the final results include all the datasets on which i used the function. I tried making the g list local but then nothing comes up as output. Any fast help would really be appreciated.

    • @MrFuguDataScience
      @MrFuguDataScience  3 роки тому +1

      you can't assume code you see may work on every dataset. I try techniques to give an idea of how to think or work around a situation. Everything is case by case

  • @SwaLi440
    @SwaLi440 3 роки тому +1

    I'm lost and need help. Followed your instructions but still cant get this JSON returned API to wrangle the data into a Legible Dataframe. GitH = github.com/sirswali/Python-Help-Request/issues/1

  • @sumitbarde3677
    @sumitbarde3677 2 роки тому +1

    Hi fugu.. I have a similar problem. I have a hierarchical json(parent and child) structure.
    The number of childs can be dynamic. I have written a code using explode which will solve my problem, but i want a solution where child jsons can be dynamic.
    How can i explode same column multiple times till i get flatten values

    • @sumitbarde3677
      @sumitbarde3677 2 роки тому +1

      If you need i can post a json link here

    • @MrFuguDataScience
      @MrFuguDataScience  2 роки тому +2

      write a function to do so, reiterate or use recursion. Or create conditional statements based on the data type for example. Lets say you are wanting to expand lists, then do a conditional to find your lists and keep going from there.

    • @sumitbarde3677
      @sumitbarde3677 2 роки тому +1

      @@MrFuguDataScience okay will try to call recursion on it
      thanks for the reply.
      Btw have you done this kind of processing in any of the code, so that i can take a reference?

    • @MrFuguDataScience
      @MrFuguDataScience  2 роки тому +2

      @@sumitbarde3677 but, before recursion think about what is going on. You have something you are trying to find: for example lists to expand. Inside this list you have possibly more list and you need to contain either everything else or discard and continue searching. The good thing is you are creating more rows not columns.
      I don't remember if I have anything like that. But, that is how I would think about it initially and work from there. Every example is unique and there your tools/functions are ways to solve these problems.

    • @sumitbarde3677
      @sumitbarde3677 2 роки тому +1

      @@MrFuguDataScience i have created a approach, it works dynamically but its kind of brute force approach. Still working on it if I can think of something more optimised.
      Thanks for your reply fugu. Much appreciated.

  • @NORCupcake
    @NORCupcake 3 роки тому +1

    Hi, good video - nice and calm voice, keep up the good work. However, I am still struggling seeing how the examples can be transferred to my case where the response is a dictionary, which contains invoices, and on a deeper level, line_items. I wish to simply expand line items from invoice - how can I concatenate invoice with line_items? Would appreciate any tips.
    {
    "list": [
    {
    "invoice": {
    "id": "2732",
    "customer_id": "1321dsfdfs",
    "subscription_id": "21312adsdfsadfs",
    "recurring": true,
    "status": "paid",
    "price_type": "tax_exclusive",
    "date": 1600592228,
    "due_date": 1600592228,
    "net_term_days": 0,
    "exchange_rate": 1.0,
    "total": 676280,
    "amount_paid": 676280,
    "amount_adjusted": 0,
    "write_off_amount": 0,
    "credits_applied": 0,
    "amount_due": 0,
    "paid_at": 1600592230,
    "updated_at": 1600592232,
    "resource_version": 1600592232291,
    "deleted": false,
    "object": "invoice",
    "first_invoice": false,
    "amount_to_collect": 0,
    "round_off_amount": 0,
    "has_advance_charges": false,
    "currency_code": "NOK",
    "base_currency_code": "NOK",
    "is_gifted": false,
    "term_finalized": true,
    "tax": 2520,
    "line_items": [
    {
    "id": "fdsfsd3234211",
    "date_from": 1598232725,
    "date_to": 1598232725,
    "unit_amount": 800,
    "quantity": 1,
    "amount": 800,
    "pricing_model": "flat_fee",
    "is_taxed": false,
    "tax_amount": 0,
    "object": "line_item",
    "subscription_id": "21312adsdfsadfs",
    "customer_id": "1321dsfdfs",
    "description": "Autopass passering",
    "entity_type": "addon",
    "entity_id": "autopass-passering",
    "tax_exempt_reason": "region_non_taxable",
    "discount_amount": 0,
    "item_level_discount_amount": 0
    }, etc.

    • @MrFuguDataScience
      @MrFuguDataScience  3 роки тому +2

      see if this works:
      kk=your_data
      j=[]
      for i in kk:
      j.append(i['invoice'])
      c=pd.DataFrame(j)
      import json
      bb=c.explode('line_items')
      df_1=pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in bb['line_items'].items() ]))
      df_=df_1.T.rename(columns={'id':'line_items.id'})
      pd.concat([c,df_],axis=1)

    • @NORCupcake
      @NORCupcake 3 роки тому +1

      ​@@MrFuguDataScience Haha, you've coded that way easier than I have, but, still not achieving my problem.
      It manages to extract and combine, but I wish to expand the DataFrame (c,df_) so that I may have several line items per invoice "id".
      It should look like "bb", which expands line items, but it would also include the columns from df_, i.e. "amount", "customer_id", "description" etc.
      Thank you though

    • @MrFuguDataScience
      @MrFuguDataScience  3 роки тому +1

      @@NORCupcake , That should be taken care of: I renamed the line_item id name to not confuse anything and concat based on columns for both DF's. The ID in first column is the invoice ID, look at the columns: df.columns it is 49 or so columns of everything

    • @NORCupcake
      @NORCupcake 3 роки тому +1

      @@MrFuguDataScience I understand, but I am trying to expand the rows, so that I get access to information per invoice ID.
      The dataframe of bb is the correct idea of expanding on rows (with 383 rows), but as you know doesn't contain the columns from line items.
      I don't know how to explain it any better :)

    • @MrFuguDataScience
      @MrFuguDataScience  3 роки тому +1

      @@NORCupcake : the columns from line items are
      bb=c.explode('line_items')
      df_1=pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in bb['line_items'].items() ]))
      df_=df_1.T.rename(columns={'id':'line_items.id'})
      then the invoice will be the (c)
      I would look at the two pieces and take c.columns and df_.columns and compare! you have everything. df_ is the line_items. start from the beginning and look at original number of columns before doing anything
      len(c.columns)
      len(df_.columns)
      len(pd.concat([c,df_],axis=1).columns)