Dataframe from JSON formatted as text?









up vote
0
down vote

favorite












Is it possible to create a dataframe from JSON formatted as text, not as Python object?



With Python object, I could for example do:



from pandas.io.json import json_normalize
import requests

response = requests.get(url, params).json()
df = json_normalize(response)


but I want to achieve the same with response = requests.get(url,params).text (flattening is not required though).










share|improve this question





















  • Can you put few lines of the data?
    – pygo
    Nov 9 at 17:21










  • @pygo why the list comprehension?
    – roganjosh
    Nov 9 at 17:24










  • @pygo I won't be reading JSON from file, I want to put requests.get(url,params).text response into a dataframe.
    – barciewicz
    Nov 9 at 17:26











  • @barciewicz, yes got it thats y i said i misread tehe question sorry :-)
    – pygo
    Nov 9 at 17:28










  • Is the .text guaranteed to be valid JSON string?
    – Idlehands
    Nov 9 at 18:37














up vote
0
down vote

favorite












Is it possible to create a dataframe from JSON formatted as text, not as Python object?



With Python object, I could for example do:



from pandas.io.json import json_normalize
import requests

response = requests.get(url, params).json()
df = json_normalize(response)


but I want to achieve the same with response = requests.get(url,params).text (flattening is not required though).










share|improve this question





















  • Can you put few lines of the data?
    – pygo
    Nov 9 at 17:21










  • @pygo why the list comprehension?
    – roganjosh
    Nov 9 at 17:24










  • @pygo I won't be reading JSON from file, I want to put requests.get(url,params).text response into a dataframe.
    – barciewicz
    Nov 9 at 17:26











  • @barciewicz, yes got it thats y i said i misread tehe question sorry :-)
    – pygo
    Nov 9 at 17:28










  • Is the .text guaranteed to be valid JSON string?
    – Idlehands
    Nov 9 at 18:37












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Is it possible to create a dataframe from JSON formatted as text, not as Python object?



With Python object, I could for example do:



from pandas.io.json import json_normalize
import requests

response = requests.get(url, params).json()
df = json_normalize(response)


but I want to achieve the same with response = requests.get(url,params).text (flattening is not required though).










share|improve this question













Is it possible to create a dataframe from JSON formatted as text, not as Python object?



With Python object, I could for example do:



from pandas.io.json import json_normalize
import requests

response = requests.get(url, params).json()
df = json_normalize(response)


but I want to achieve the same with response = requests.get(url,params).text (flattening is not required though).







python json pandas dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 17:16









barciewicz

426310




426310











  • Can you put few lines of the data?
    – pygo
    Nov 9 at 17:21










  • @pygo why the list comprehension?
    – roganjosh
    Nov 9 at 17:24










  • @pygo I won't be reading JSON from file, I want to put requests.get(url,params).text response into a dataframe.
    – barciewicz
    Nov 9 at 17:26











  • @barciewicz, yes got it thats y i said i misread tehe question sorry :-)
    – pygo
    Nov 9 at 17:28










  • Is the .text guaranteed to be valid JSON string?
    – Idlehands
    Nov 9 at 18:37
















  • Can you put few lines of the data?
    – pygo
    Nov 9 at 17:21










  • @pygo why the list comprehension?
    – roganjosh
    Nov 9 at 17:24










  • @pygo I won't be reading JSON from file, I want to put requests.get(url,params).text response into a dataframe.
    – barciewicz
    Nov 9 at 17:26











  • @barciewicz, yes got it thats y i said i misread tehe question sorry :-)
    – pygo
    Nov 9 at 17:28










  • Is the .text guaranteed to be valid JSON string?
    – Idlehands
    Nov 9 at 18:37















Can you put few lines of the data?
– pygo
Nov 9 at 17:21




Can you put few lines of the data?
– pygo
Nov 9 at 17:21












@pygo why the list comprehension?
– roganjosh
Nov 9 at 17:24




@pygo why the list comprehension?
– roganjosh
Nov 9 at 17:24












@pygo I won't be reading JSON from file, I want to put requests.get(url,params).text response into a dataframe.
– barciewicz
Nov 9 at 17:26





@pygo I won't be reading JSON from file, I want to put requests.get(url,params).text response into a dataframe.
– barciewicz
Nov 9 at 17:26













@barciewicz, yes got it thats y i said i misread tehe question sorry :-)
– pygo
Nov 9 at 17:28




@barciewicz, yes got it thats y i said i misread tehe question sorry :-)
– pygo
Nov 9 at 17:28












Is the .text guaranteed to be valid JSON string?
– Idlehands
Nov 9 at 18:37




Is the .text guaranteed to be valid JSON string?
– Idlehands
Nov 9 at 18:37












2 Answers
2






active

oldest

votes

















up vote
0
down vote













I usually create dataframe from json using "read_json"



import pandas as pd
import requests

data = requests.get(url, params).content
df = pd.read_json(data)
df.head()





share|improve this answer




















  • Thanks, sadly this gives me ValueError: arrays must all be same length at read_json line, I guess because my JSON is nested. Do you know how to work around it?
    – barciewicz
    Nov 9 at 18:01











  • replace read_json line by: from pandas.io.json import json_normalize, loads df = json_normalize(loads(data))
    – lfkopp
    Nov 10 at 1:09

















up vote
0
down vote













If your response = requests.get(url,params).text is guaranteed to give you a valid JSON string, then all you need to do is as follows:



from pandas.io.json import json_normalize, loads
import requests

response = requests.get(url, params).text
df = json_normalize(loads(response))


Here we make use of json's loads to convert the JSON string to a Python object before passing back to json_normalize.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230467%2fdataframe-from-json-formatted-as-text%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I usually create dataframe from json using "read_json"



    import pandas as pd
    import requests

    data = requests.get(url, params).content
    df = pd.read_json(data)
    df.head()





    share|improve this answer




















    • Thanks, sadly this gives me ValueError: arrays must all be same length at read_json line, I guess because my JSON is nested. Do you know how to work around it?
      – barciewicz
      Nov 9 at 18:01











    • replace read_json line by: from pandas.io.json import json_normalize, loads df = json_normalize(loads(data))
      – lfkopp
      Nov 10 at 1:09














    up vote
    0
    down vote













    I usually create dataframe from json using "read_json"



    import pandas as pd
    import requests

    data = requests.get(url, params).content
    df = pd.read_json(data)
    df.head()





    share|improve this answer




















    • Thanks, sadly this gives me ValueError: arrays must all be same length at read_json line, I guess because my JSON is nested. Do you know how to work around it?
      – barciewicz
      Nov 9 at 18:01











    • replace read_json line by: from pandas.io.json import json_normalize, loads df = json_normalize(loads(data))
      – lfkopp
      Nov 10 at 1:09












    up vote
    0
    down vote










    up vote
    0
    down vote









    I usually create dataframe from json using "read_json"



    import pandas as pd
    import requests

    data = requests.get(url, params).content
    df = pd.read_json(data)
    df.head()





    share|improve this answer












    I usually create dataframe from json using "read_json"



    import pandas as pd
    import requests

    data = requests.get(url, params).content
    df = pd.read_json(data)
    df.head()






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 9 at 17:30









    lfkopp

    692




    692











    • Thanks, sadly this gives me ValueError: arrays must all be same length at read_json line, I guess because my JSON is nested. Do you know how to work around it?
      – barciewicz
      Nov 9 at 18:01











    • replace read_json line by: from pandas.io.json import json_normalize, loads df = json_normalize(loads(data))
      – lfkopp
      Nov 10 at 1:09
















    • Thanks, sadly this gives me ValueError: arrays must all be same length at read_json line, I guess because my JSON is nested. Do you know how to work around it?
      – barciewicz
      Nov 9 at 18:01











    • replace read_json line by: from pandas.io.json import json_normalize, loads df = json_normalize(loads(data))
      – lfkopp
      Nov 10 at 1:09















    Thanks, sadly this gives me ValueError: arrays must all be same length at read_json line, I guess because my JSON is nested. Do you know how to work around it?
    – barciewicz
    Nov 9 at 18:01





    Thanks, sadly this gives me ValueError: arrays must all be same length at read_json line, I guess because my JSON is nested. Do you know how to work around it?
    – barciewicz
    Nov 9 at 18:01













    replace read_json line by: from pandas.io.json import json_normalize, loads df = json_normalize(loads(data))
    – lfkopp
    Nov 10 at 1:09




    replace read_json line by: from pandas.io.json import json_normalize, loads df = json_normalize(loads(data))
    – lfkopp
    Nov 10 at 1:09












    up vote
    0
    down vote













    If your response = requests.get(url,params).text is guaranteed to give you a valid JSON string, then all you need to do is as follows:



    from pandas.io.json import json_normalize, loads
    import requests

    response = requests.get(url, params).text
    df = json_normalize(loads(response))


    Here we make use of json's loads to convert the JSON string to a Python object before passing back to json_normalize.






    share|improve this answer
























      up vote
      0
      down vote













      If your response = requests.get(url,params).text is guaranteed to give you a valid JSON string, then all you need to do is as follows:



      from pandas.io.json import json_normalize, loads
      import requests

      response = requests.get(url, params).text
      df = json_normalize(loads(response))


      Here we make use of json's loads to convert the JSON string to a Python object before passing back to json_normalize.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        If your response = requests.get(url,params).text is guaranteed to give you a valid JSON string, then all you need to do is as follows:



        from pandas.io.json import json_normalize, loads
        import requests

        response = requests.get(url, params).text
        df = json_normalize(loads(response))


        Here we make use of json's loads to convert the JSON string to a Python object before passing back to json_normalize.






        share|improve this answer












        If your response = requests.get(url,params).text is guaranteed to give you a valid JSON string, then all you need to do is as follows:



        from pandas.io.json import json_normalize, loads
        import requests

        response = requests.get(url, params).text
        df = json_normalize(loads(response))


        Here we make use of json's loads to convert the JSON string to a Python object before passing back to json_normalize.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 18:39









        Idlehands

        3,2901316




        3,2901316



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230467%2fdataframe-from-json-formatted-as-text%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo