AttributeError: 'str' object has no attribute 'keys' error in getting json object keysSafely turning a JSON string into an objectGetting key with maximum value in dictionary?How to sort a list of objects based on an attribute of the objects?How to know if an object has an attribute in PythonDeserialize JSON into C# dynamic object?Convert JS object to JSON stringHow do I turn a C# object into a JSON string in .NET?How do I get ASP.NET Web API to return JSON instead of XML using Chrome?Parsing JSON to CSV using Python: AttributeError: 'unicode' object has no attribute 'keys'Str Attribute has no keys when trying to write dictionary to a CSV file

Risk of getting Chronic Wasting Disease (CWD) in the United States?

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

Minkowski space

What's the point of deactivating Num Lock on login screens?

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

Did Shadowfax go to Valinor?

Is it possible to do 50 km distance without any previous training?

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

Mathematical cryptic clues

What does it mean to describe someone as a butt steak?

Font hinting is lost in Chrome-like browsers (for some languages )

Is it important to consider tone, melody, and musical form while writing a song?

Theorem, big Paralist and Amsart

How does one intimidate enemies without having the capacity for violence?

Why can't I see bouncing of a switch on an oscilloscope?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

Do VLANs within a subnet need to have their own subnet for router on a stick?

How much RAM could one put in a typical 80386 setup?

Example of a continuous function that don't have a continuous extension

Why do I get two different answers for this counting problem?

"to be prejudice towards/against someone" vs "to be prejudiced against/towards someone"

Which models of the Boeing 737 are still in production?

Prove that NP is closed under karp reduction?



AttributeError: 'str' object has no attribute 'keys' error in getting json object keys


Safely turning a JSON string into an objectGetting key with maximum value in dictionary?How to sort a list of objects based on an attribute of the objects?How to know if an object has an attribute in PythonDeserialize JSON into C# dynamic object?Convert JS object to JSON stringHow do I turn a C# object into a JSON string in .NET?How do I get ASP.NET Web API to return JSON instead of XML using Chrome?Parsing JSON to CSV using Python: AttributeError: 'unicode' object has no attribute 'keys'Str Attribute has no keys when trying to write dictionary to a CSV file






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm trying to convert a json object to csv file. When I pass the obj and print it, it works perfectly. But when I continue from line #Problem, I get an AttributeError: 'str' object has no attribute 'keys'.
I pass the exact same object. How can I fix this error and let it run properly?



import json
import csv
import os

def flattenjson( b, delim ):
val =
for i in b.keys():
if isinstance( b[i], dict ):
get = flattenjson( b[i], delim )
for j in get.keys():
val[ i + delim + j ] = get[j]
else:
val[i] = b[i]
return val

jjj =
"pk": 22,
"model": "auth.permission",
"fields":
"codename": "add_message",
"name": "Can add message",
"content_type": 8



print(flattenjson(jjj , "__" ))
#Problem
input = map( lambda x: flattenjson( x, "__" ), jjj )

columns = [ x for row in input for x in row.keys() ]
columns = list( set( columns ) )

with open( 'sad.csv', 'wb' ) as out_file:
csv_w = csv.writer( out_file )
csv_w.writerow( columns )

for i_r in input:
csv_w.writerow( map( lambda x: i_r.get( x, "" ), columns ) )









share|improve this question




























    1















    I'm trying to convert a json object to csv file. When I pass the obj and print it, it works perfectly. But when I continue from line #Problem, I get an AttributeError: 'str' object has no attribute 'keys'.
    I pass the exact same object. How can I fix this error and let it run properly?



    import json
    import csv
    import os

    def flattenjson( b, delim ):
    val =
    for i in b.keys():
    if isinstance( b[i], dict ):
    get = flattenjson( b[i], delim )
    for j in get.keys():
    val[ i + delim + j ] = get[j]
    else:
    val[i] = b[i]
    return val

    jjj =
    "pk": 22,
    "model": "auth.permission",
    "fields":
    "codename": "add_message",
    "name": "Can add message",
    "content_type": 8



    print(flattenjson(jjj , "__" ))
    #Problem
    input = map( lambda x: flattenjson( x, "__" ), jjj )

    columns = [ x for row in input for x in row.keys() ]
    columns = list( set( columns ) )

    with open( 'sad.csv', 'wb' ) as out_file:
    csv_w = csv.writer( out_file )
    csv_w.writerow( columns )

    for i_r in input:
    csv_w.writerow( map( lambda x: i_r.get( x, "" ), columns ) )









    share|improve this question
























      1












      1








      1








      I'm trying to convert a json object to csv file. When I pass the obj and print it, it works perfectly. But when I continue from line #Problem, I get an AttributeError: 'str' object has no attribute 'keys'.
      I pass the exact same object. How can I fix this error and let it run properly?



      import json
      import csv
      import os

      def flattenjson( b, delim ):
      val =
      for i in b.keys():
      if isinstance( b[i], dict ):
      get = flattenjson( b[i], delim )
      for j in get.keys():
      val[ i + delim + j ] = get[j]
      else:
      val[i] = b[i]
      return val

      jjj =
      "pk": 22,
      "model": "auth.permission",
      "fields":
      "codename": "add_message",
      "name": "Can add message",
      "content_type": 8



      print(flattenjson(jjj , "__" ))
      #Problem
      input = map( lambda x: flattenjson( x, "__" ), jjj )

      columns = [ x for row in input for x in row.keys() ]
      columns = list( set( columns ) )

      with open( 'sad.csv', 'wb' ) as out_file:
      csv_w = csv.writer( out_file )
      csv_w.writerow( columns )

      for i_r in input:
      csv_w.writerow( map( lambda x: i_r.get( x, "" ), columns ) )









      share|improve this question














      I'm trying to convert a json object to csv file. When I pass the obj and print it, it works perfectly. But when I continue from line #Problem, I get an AttributeError: 'str' object has no attribute 'keys'.
      I pass the exact same object. How can I fix this error and let it run properly?



      import json
      import csv
      import os

      def flattenjson( b, delim ):
      val =
      for i in b.keys():
      if isinstance( b[i], dict ):
      get = flattenjson( b[i], delim )
      for j in get.keys():
      val[ i + delim + j ] = get[j]
      else:
      val[i] = b[i]
      return val

      jjj =
      "pk": 22,
      "model": "auth.permission",
      "fields":
      "codename": "add_message",
      "name": "Can add message",
      "content_type": 8



      print(flattenjson(jjj , "__" ))
      #Problem
      input = map( lambda x: flattenjson( x, "__" ), jjj )

      columns = [ x for row in input for x in row.keys() ]
      columns = list( set( columns ) )

      with open( 'sad.csv', 'wb' ) as out_file:
      csv_w = csv.writer( out_file )
      csv_w.writerow( columns )

      for i_r in input:
      csv_w.writerow( map( lambda x: i_r.get( x, "" ), columns ) )






      python json csv export-to-csv






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 4:36









      SadafSadaf

      62




      62






















          1 Answer
          1






          active

          oldest

          votes


















          0














          while iterating in your lambda function you're giving the only key which is considered as the string



          So here is the following solution that might work for you.



          Edited the code instead of passing the only key as an argument I'm passing a new dict.



          input = map( lambda x: flattenjson(x: jjj[x], "__" ), jjj )





          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',
            autoActivateHeartbeat: false,
            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%2f55056797%2fattributeerror-str-object-has-no-attribute-keys-error-in-getting-json-objec%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            while iterating in your lambda function you're giving the only key which is considered as the string



            So here is the following solution that might work for you.



            Edited the code instead of passing the only key as an argument I'm passing a new dict.



            input = map( lambda x: flattenjson(x: jjj[x], "__" ), jjj )





            share|improve this answer



























              0














              while iterating in your lambda function you're giving the only key which is considered as the string



              So here is the following solution that might work for you.



              Edited the code instead of passing the only key as an argument I'm passing a new dict.



              input = map( lambda x: flattenjson(x: jjj[x], "__" ), jjj )





              share|improve this answer

























                0












                0








                0







                while iterating in your lambda function you're giving the only key which is considered as the string



                So here is the following solution that might work for you.



                Edited the code instead of passing the only key as an argument I'm passing a new dict.



                input = map( lambda x: flattenjson(x: jjj[x], "__" ), jjj )





                share|improve this answer













                while iterating in your lambda function you're giving the only key which is considered as the string



                So here is the following solution that might work for you.



                Edited the code instead of passing the only key as an argument I'm passing a new dict.



                input = map( lambda x: flattenjson(x: jjj[x], "__" ), jjj )






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 8 at 4:51









                Shubham SrivastavaShubham Srivastava

                380416




                380416





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55056797%2fattributeerror-str-object-has-no-attribute-keys-error-in-getting-json-objec%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

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко