Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92758 > unrolled thread

JSON Object to CSV file

Started bySaran A <ahlusar.ahluwalia@gmail.com>
First post2015-06-17 08:00 -0700
Last post2015-08-19 18:54 -0700
Articles 13 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  JSON Object to CSV file Saran A <ahlusar.ahluwalia@gmail.com> - 2015-06-17 08:00 -0700
    Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-17 08:45 -0700
      Re: JSON Object to CSV file Peter Otten <__peter__@web.de> - 2015-06-17 20:19 +0200
        Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-17 13:20 -0700
    Re: JSON Object to CSV file Denis McMahon <denismfmcmahon@gmail.com> - 2015-06-21 06:45 +0000
      Re: JSON Object to CSV file sahluwalia@wynyardgroup.com - 2015-06-21 06:57 -0700
        Re: JSON Object to CSV file Denis McMahon <denismfmcmahon@gmail.com> - 2015-06-21 20:52 +0000
          Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-21 16:38 -0700
      Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-21 07:22 -0700
      Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-21 07:54 -0700
        Re: JSON Object to CSV file Ned Batchelder <ned@nedbatchelder.com> - 2015-06-21 08:31 -0700
          Re: JSON Object to CSV file Sahlusar <ahlusar.ahluwalia@gmail.com> - 2015-06-21 08:39 -0700
    Re: JSON Object to CSV file ryguy7272 <ryanshuell@gmail.com> - 2015-08-19 18:54 -0700

#92758 — JSON Object to CSV file

FromSaran A <ahlusar.ahluwalia@gmail.com>
Date2015-06-17 08:00 -0700
SubjectJSON Object to CSV file
Message-ID<dd791644-35b4-4400-a683-d409808c0c5b@googlegroups.com>
I would like to have this JSON object written out to a CSV file so that the keys are header fields (for each of the columns) and the values are values that are associated with each header field. Is there a best practice for working with this? Ideally I would like to recursively iterate through the key value pairs. Thank you in advance. I am using Python 3.4 on Windows. My editor is Sublime 2. 

{
"CF": {
"A": "5",
"FEC": "1/1/0001 12:00:00 AM",
"TE": null,
"Locator": null,
"Message": "Transfer Fee",
"AT": null,
"FT": null,
"FR": "True",
"FY": null,
"FR": null,
"FG": "0",
"Comment": null,
"FUD": null,
"cID": null,
"GEO": null,
"ISO": null,
"TRID": null,
"XTY": "931083",
"ANM": null,
"NM": null
},
"CF": "Fee",
"ID": "2"
}

[toc] | [next] | [standalone]


#92765

FromSahlusar <ahlusar.ahluwalia@gmail.com>
Date2015-06-17 08:45 -0700
Message-ID<5747bb7f-04c0-4097-8c09-1102499b49b2@googlegroups.com>
In reply to#92758
On Wednesday, June 17, 2015 at 11:00:24 AM UTC-4, Saran A wrote:
> I would like to have this JSON object written out to a CSV file so that the keys are header fields (for each of the columns) and the values are values that are associated with each header field. Is there a best practice for working with this? Ideally I would like to recursively iterate through the key value pairs. Thank you in advance. I am using Python 3.4 on Windows. My editor is Sublime 2. 
> 
> {
> "CF": {
> "A": "5",
> "FEC": "1/1/0001 12:00:00 AM",
> "TE": null,
> "Locator": null,
> "Message": "Transfer Fee",
> "AT": null,
> "FT": null,
> "FR": "True",
> "FY": null,
> "FR": null,
> "FG": "0",
> "Comment": null,
> "FUD": null,
> "cID": null,
> "GEO": null,
> "ISO": null,
> "TRID": null,
> "XTY": "931083",
> "ANM": null,
> "NM": null
> },
> "CF": "Fee",
> "ID": "2"
> }

My apologies:

I originally parsed this from an XML file.

It really should be:

{
"Fee": {
"A": "5",
"FEC": "1/1/0001 12:00:00 AM",
"TE": null,
"Locator": null,
"Message": "Transfer Fee",
"AT": null,
"FT": null,
"FR": "True",
"FY": null,
"FR": null,
"FG": "0",
"Comment": null,
"FUD": null,
"cID": null,
"GEO": null,
"ISO": null,
"TRID": null,
"XTY": "931083",
"ANM": null,
"NM": null
},
"CF": "Fee",
"ID": "2"
}

The value, "Fee" associated with the key, "CF" does should not be included as a column header.

The CSV file, when opened with an application such as MS Excel, should be as follows:

(Column Header)----> CF               A          FEC

(Field Value)---->   Fee              5        1/1/0001 12:00:00 AM

My apologies for an confusion.

[toc] | [prev] | [next] | [standalone]


#92771

FromPeter Otten <__peter__@web.de>
Date2015-06-17 20:19 +0200
Message-ID<mailman.561.1434565217.13271.python-list@python.org>
In reply to#92765
Sahlusar wrote:

> On Wednesday, June 17, 2015 at 11:00:24 AM UTC-4, Saran A wrote:
>> I would like to have this JSON object written out to a CSV file so that
>> the keys are header fields (for each of the columns) and the values are
>> values that are associated with each header field. Is there a best
>> practice for working with this? Ideally I would like to recursively
>> iterate through the key value pairs. Thank you in advance. I am using
>> Python 3.4 on Windows. My editor is Sublime 2.
>> 
>> {
>> "CF": {
>> "A": "5",
>> "FEC": "1/1/0001 12:00:00 AM",
>> "TE": null,
>> "Locator": null,
>> "Message": "Transfer Fee",
>> "AT": null,
>> "FT": null,
>> "FR": "True",
>> "FY": null,
>> "FR": null,
>> "FG": "0",
>> "Comment": null,
>> "FUD": null,
>> "cID": null,
>> "GEO": null,
>> "ISO": null,
>> "TRID": null,
>> "XTY": "931083",
>> "ANM": null,
>> "NM": null
>> },
>> "CF": "Fee",
>> "ID": "2"
>> }
> 
> My apologies:
> 
> I originally parsed this from an XML file.
> 
> It really should be:
> 
> {
> "Fee": {
> "A": "5",
> "FEC": "1/1/0001 12:00:00 AM",
> "TE": null,
> "Locator": null,
> "Message": "Transfer Fee",
> "AT": null,
> "FT": null,
> "FR": "True",
> "FY": null,
> "FR": null,
> "FG": "0",
> "Comment": null,
> "FUD": null,
> "cID": null,
> "GEO": null,
> "ISO": null,
> "TRID": null,
> "XTY": "931083",
> "ANM": null,
> "NM": null
> },
> "CF": "Fee",
> "ID": "2"
> }
> 
> The value, "Fee" associated with the key, "CF" does should not be included
> as a column header.
> 
> The CSV file, when opened with an application such as MS Excel, should be
> as follows:
> 
> (Column Header)----> CF               A          FEC
> 
> (Field Value)---->   Fee              5        1/1/0001 12:00:00 AM
> 
> My apologies for an confusion.

Forget about Excel, what should the CSV look like when opened in a text 
editor?

Here's my guess:


$ cat input.json
{
"Fee": {
"A": "5",
"FEC": "1/1/0001 12:00:00 AM",
"TE": null,
"Locator": null,
"Message": "Transfer Fee",
"AT": null,
"FT": null,
"FR": "True",
"FY": null,
"FR": null,
"FG": "0",
"Comment": null,
"FUD": null,
"cID": null,
"GEO": null,
"ISO": null,
"TRID": null,
"XTY": "931083",
"ANM": null,
"NM": null
},
"CF": "Fee",
"ID": "2"
}
$ cat json2csv.py
import csv
import json
import sys

def hook(obj):
    return obj

def flatten(obj):
    for k, v in obj:
        if isinstance(v, list):
            yield from flatten(v)
        else:
            yield k, v

if __name__ == "__main__":
    with open("input.json") as f:
        data = json.load(f, object_pairs_hook=hook)

    pairs = list(flatten(data))

    writer = csv.writer(sys.stdout)
    writer.writerow([k for k, v in pairs])
    writer.writerow([v for k, v in pairs])
$ python3 json2csv.py 
A,FEC,TE,Locator,Message,AT,FT,FR,FY,FR,FG,Comment,FUD,cID,GEO,ISO,TRID,XTY,ANM,NM,CF,ID
5,1/1/0001 12:00:00 AM,,,Transfer Fee,,,True,,,0,,,,,,,931083,,,Fee,2

But do you really want duplicate column names?

[toc] | [prev] | [next] | [standalone]


#92776

FromSahlusar <ahlusar.ahluwalia@gmail.com>
Date2015-06-17 13:20 -0700
Message-ID<c51635d4-174d-4ef0-8117-d80f4a8623ed@googlegroups.com>
In reply to#92771
On Wednesday, June 17, 2015 at 2:21:05 PM UTC-4, Peter Otten wrote:
> Sahlusar wrote:
> 
> > On Wednesday, June 17, 2015 at 11:00:24 AM UTC-4, Saran A wrote:
> >> I would like to have this JSON object written out to a CSV file so that
> >> the keys are header fields (for each of the columns) and the values are
> >> values that are associated with each header field. Is there a best
> >> practice for working with this? Ideally I would like to recursively
> >> iterate through the key value pairs. Thank you in advance. I am using
> >> Python 3.4 on Windows. My editor is Sublime 2.
> >> 
> >> {
> >> "CF": {
> >> "A": "5",
> >> "FEC": "1/1/0001 12:00:00 AM",
> >> "TE": null,
> >> "Locator": null,
> >> "Message": "Transfer Fee",
> >> "AT": null,
> >> "FT": null,
> >> "FR": "True",
> >> "FY": null,
> >> "FR": null,
> >> "FG": "0",
> >> "Comment": null,
> >> "FUD": null,
> >> "cID": null,
> >> "GEO": null,
> >> "ISO": null,
> >> "TRID": null,
> >> "XTY": "931083",
> >> "ANM": null,
> >> "NM": null
> >> },
> >> "CF": "Fee",
> >> "ID": "2"
> >> }
> > 
> > My apologies:
> > 
> > I originally parsed this from an XML file.
> > 
> > It really should be:
> > 
> > {
> > "Fee": {
> > "A": "5",
> > "FEC": "1/1/0001 12:00:00 AM",
> > "TE": null,
> > "Locator": null,
> > "Message": "Transfer Fee",
> > "AT": null,
> > "FT": null,
> > "FR": "True",
> > "FY": null,
> > "FR": null,
> > "FG": "0",
> > "Comment": null,
> > "FUD": null,
> > "cID": null,
> > "GEO": null,
> > "ISO": null,
> > "TRID": null,
> > "XTY": "931083",
> > "ANM": null,
> > "NM": null
> > },
> > "CF": "Fee",
> > "ID": "2"
> > }
> > 
> > The value, "Fee" associated with the key, "CF" does should not be included
> > as a column header.
> > 
> > The CSV file, when opened with an application such as MS Excel, should be
> > as follows:
> > 
> > (Column Header)----> CF               A          FEC
> > 
> > (Field Value)---->   Fee              5        1/1/0001 12:00:00 AM
> > 
> > My apologies for an confusion.
> 
> Forget about Excel, what should the CSV look like when opened in a text 
> editor?
> 
> Here's my guess:
> 
> 
> $ cat input.json
> {
> "Fee": {
> "A": "5",
> "FEC": "1/1/0001 12:00:00 AM",
> "TE": null,
> "Locator": null,
> "Message": "Transfer Fee",
> "AT": null,
> "FT": null,
> "FR": "True",
> "FY": null,
> "FR": null,
> "FG": "0",
> "Comment": null,
> "FUD": null,
> "cID": null,
> "GEO": null,
> "ISO": null,
> "TRID": null,
> "XTY": "931083",
> "ANM": null,
> "NM": null
> },
> "CF": "Fee",
> "ID": "2"
> }
> $ cat json2csv.py
> import csv
> import json
> import sys
> 
> def hook(obj):
>     return obj
> 
> def flatten(obj):
>     for k, v in obj:
>         if isinstance(v, list):
>             yield from flatten(v)
>         else:
>             yield k, v
> 
> if __name__ == "__main__":
>     with open("input.json") as f:
>         data = json.load(f, object_pairs_hook=hook)
> 
>     pairs = list(flatten(data))
> 
>     writer = csv.writer(sys.stdout)
>     writer.writerow([k for k, v in pairs])
>     writer.writerow([v for k, v in pairs])
> $ python3 json2csv.py 
> A,FEC,TE,Locator,Message,AT,FT,FR,FY,FR,FG,Comment,FUD,cID,GEO,ISO,TRID,XTY,ANM,NM,CF,ID
> 5,1/1/0001 12:00:00 AM,,,Transfer Fee,,,True,,,0,,,,,,,931083,,,Fee,2
> 
> But do you really want duplicate column names?

@Peter Otten: Thank you for your feedback. No, I do not want to have duplicates. Any thoughts on how to avoid this?

[toc] | [prev] | [next] | [standalone]


#92931

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-06-21 06:45 +0000
Message-ID<mm5mj6$5m8$3@dont-email.me>
In reply to#92758
On Wed, 17 Jun 2015 08:00:11 -0700, Saran A wrote:

> I would like to have this JSON object written out to a CSV file so that
> the keys are header fields (for each of the columns) and the values are
> values that are associated with each header field.

> {
> "CF": {
.......
> "CF": "Fee",

Your json object seems to have the same key used for two elements at the 
same level, are you sure this is legal json?

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#92959

Fromsahluwalia@wynyardgroup.com
Date2015-06-21 06:57 -0700
Message-ID<e1a0eb4d-9594-480f-b082-142dc566dea2@googlegroups.com>
In reply to#92931
On Sunday, 21 June 2015 02:47:31 UTC-4, Denis McMahon  wrote:
> On Wed, 17 Jun 2015 08:00:11 -0700, Saran A wrote:
> 
> > I would like to have this JSON object written out to a CSV file so that
> > the keys are header fields (for each of the columns) and the values are
> > values that are associated with each header field.
> 
> > {
> > "CF": {
> .......
> > "CF": "Fee",
> 
> Your json object seems to have the same key used for two elements at the 
> same level, are you sure this is legal json?
> 
> -- 
> Denis McMahon, denismfmcmahon@gmail.com

I converted this from an XML file given to me from a third party. It is as is. I am not sure what you mean by "valid"; that is a very subjective measure for any form of quantitative or qualitative data. 

[toc] | [prev] | [next] | [standalone]


#92974

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-06-21 20:52 +0000
Message-ID<mm7879$n8l$1@dont-email.me>
In reply to#92959
On Sun, 21 Jun 2015 06:57:01 -0700, sahluwalia wrote:

> On Sunday, 21 June 2015 02:47:31 UTC-4, Denis McMahon  wrote:
>> On Wed, 17 Jun 2015 08:00:11 -0700, Saran A wrote:
>> 
>> > I would like to have this JSON object written out to a CSV file so
>> > that the keys are header fields (for each of the columns) and the
>> > values are values that are associated with each header field.
>> 
>> > {
>> > "CF": {
>> .......
>> > "CF": "Fee",
>> 
>> Your json object seems to have the same key used for two elements at
>> the same level, are you sure this is legal json?

> I converted this from an XML file given to me from a third party. It is
> as is. I am not sure what you mean by "valid"; that is a very subjective
> measure for any form of quantitative or qualitative data.

Put it this way, when I feed your json object into a jason lint, the 
output is the following:

{
    "CF": "Fee",
    "ID": "2"
}

The second occurrence in the parent object of name "CF" with a value of 
string literal "Fee" overwrites the earlier name "CF" whose value is (in 
python terms) a dictionary or (in json terms) an object.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#92985

FromSahlusar <ahlusar.ahluwalia@gmail.com>
Date2015-06-21 16:38 -0700
Message-ID<0eadbf86-420c-4916-b94d-612b222e92a0@googlegroups.com>
In reply to#92974
On Sunday, June 21, 2015 at 4:54:27 PM UTC-4, Denis McMahon wrote:
> On Sun, 21 Jun 2015 06:57:01 -0700, sahluwalia wrote:
> 
> > On Sunday, 21 June 2015 02:47:31 UTC-4, Denis McMahon  wrote:
> >> On Wed, 17 Jun 2015 08:00:11 -0700, Saran A wrote:
> >> 
> >> > I would like to have this JSON object written out to a CSV file so
> >> > that the keys are header fields (for each of the columns) and the
> >> > values are values that are associated with each header field.
> >> 
> >> > {
> >> > "CF": {
> >> .......
> >> > "CF": "Fee",
> >> 
> >> Your json object seems to have the same key used for two elements at
> >> the same level, are you sure this is legal json?
> 
> > I converted this from an XML file given to me from a third party. It is
> > as is. I am not sure what you mean by "valid"; that is a very subjective
> > measure for any form of quantitative or qualitative data.
> 
> Put it this way, when I feed your json object into a jason lint, the 
> output is the following:
> 
> {
>     "CF": "Fee",
>     "ID": "2"
> }
> 
> The second occurrence in the parent object of name "CF" with a value of 
> string literal "Fee" overwrites the earlier name "CF" whose value is (in 
> python terms) a dictionary or (in json terms) an object.
> 
> -- 
> Denis McMahon, denismfmcmahon@gmail.com

I am stepping back and rethinking the notion of JSON/dictionaries. Maybe this was a very overcomplicated approach for this type of data. 

[toc] | [prev] | [next] | [standalone]


#92961

FromSahlusar <ahlusar.ahluwalia@gmail.com>
Date2015-06-21 07:22 -0700
Message-ID<33c13578-e0c4-4fe1-b925-b7d57cf038ed@googlegroups.com>
In reply to#92931
On Sunday, June 21, 2015 at 2:47:31 AM UTC-4, Denis McMahon wrote:
> On Wed, 17 Jun 2015 08:00:11 -0700, Saran A wrote:
> 
> > I would like to have this JSON object written out to a CSV file so that
> > the keys are header fields (for each of the columns) and the values are
> > values that are associated with each header field.
> 
> > {
> > "CF": {
> .......
> > "CF": "Fee",
> 
> Your json object seems to have the same key used for two elements at the 
> same level, are you sure this is legal json?
> 
> -- 
> Denis McMahon, denismfmcmahon@gmail.com

I converted this from an XML file given to me from a third party. It is as is. I am not sure what you mean by "valid"; that is a very subjective measure for any form of quantitative or qualitative data. 

[toc] | [prev] | [next] | [standalone]


#92963

FromSahlusar <ahlusar.ahluwalia@gmail.com>
Date2015-06-21 07:54 -0700
Message-ID<c934be6b-c732-466c-849e-7891d4dd461b@googlegroups.com>
In reply to#92931
On Sunday, June 21, 2015 at 2:47:31 AM UTC-4, Denis McMahon wrote:
> On Wed, 17 Jun 2015 08:00:11 -0700, Saran A wrote:
> 
> > I would like to have this JSON object written out to a CSV file so that
> > the keys are header fields (for each of the columns) and the values are
> > values that are associated with each header field.
> 
> > {
> > "CF": {
> .......
> > "CF": "Fee",
> 
> Your json object seems to have the same key used for two elements at the 
> same level, are you sure this is legal json?
> 
> -- 
> Denis McMahon, denismfmcmahon@gmail.com

This is a duplicate to the post titled: JSON to CSV Troubleshooting:

It is difficult to explain this to someone asynchronously and without an in person meeting. Moreover, the strict guidelines for disclosing information make it difficult for me to explain the client's requirements and the problems that they face. 

I do agree with you Denis that this is an unconventional approach. I was wondering then that perhaps I should add additional functionality at the XML to JSON step? So far, with JSON objects without nested lists (as values) I have been successful with this (the following is rather lengthy): 

import xml.etree.cElementTree as ElementTree 
from xml.etree.ElementTree import XMLParser 
import json 
import csv 
import tokenize 
import token 
try: 
    from collections import OrderedDict 
    import json 
except ImportError: 
    from ordereddict import OrderedDict 
    import simplejson as json 
import itertools 
import six 
import string 
from csvkit import CSVKitWriter 


class XmlListConfig(list): 
    def __init__(self, aList): 
        for element in aList: 
            if element: 
                # treat like dict 
                if len(element) == 1 or element[0].tag != element[1].tag: 
                    self.append(XmlDictConfig(element)) 
                # treat like list 
                elif element[0].tag == element[1].tag: 
                    self.append(XmlListConfig(element)) 
            elif element.text: 
                text = element.text.strip() 
                if text: 
                    self.append(text) 


class XmlDictConfig(dict): 
    ''' 
    Example usage: 

    >>> tree = ElementTree.parse('your_file.xml') 
    >>> root = tree.getroot() 
    >>> xmldict = XmlDictConfig(root) 

    Or, if you want to use an XML string: 

    >>> root = ElementTree.XML(xml_string) 
    >>> xmldict = XmlDictConfig(root) 

    And then use xmldict for what it is..a dictionary. 
    ''' 
    def __init__(self, parent_element): 
        if parent_element.items(): 
            self.update(dict(parent_element.items())) 
        for element in parent_element: 
            if element: 
                # treat like dict - we assume that if the first two tags 
                # in a series are different, then they are all different. 
                if len(element) == 1 or element[0].tag != element[1].tag: 
                    aDict = XmlDictConfig(element) 
                # treat like list - we assume that if the first two tags 
                # in a series are the same, then the rest are the same. 
                else: 
                    # here, we put the list in dictionary; the key is the 
                    # tag name the list elements all share in common, and 
                    # the value is the list itself 
                    aDict = {element[0].tag: XmlListConfig(element)} 
                # if the tag has attributes, add those to the dict 
                if element.items(): 
                    aDict.update(dict(element.items())) 
                self.update({element.tag: aDict}) 
            # this assumes that if you've got an attribute in a tag, 
            # you won't be having any text. This may or may not be a 
            # good idea -- time will tell. It works for the way we are 
            # currently doing XML configuration files... 
            elif element.items(): 
                self.update({element.tag: dict(element.items())}) 
            # finally, if there are no child tags and no attributes, extract 
            # the text 
            else: 
                self.update({element.tag: element.text}) 



def main(): 

    #Lines 88-89stantiate the class Elementree 
    #and applies the method to recursively traverse from the root node 
    #XmlDictConfig is instantiated in line 90 

    with open('C:\\Users\\somefile.xml', 'r', encoding='utf-8') as f: 
        xml_string = f.read() 
    xml_string= xml_string.replace('&#x0;', '') #optional to remove ampersands. 
    root = ElementTree.XML(xml_string) 
    xmldict = XmlDictConfig(root) 
    json_str = json.dumps(xmldict, sort_keys=True, indent=4, separators=(',', ': ')) 
    newly_formatted_data = json.loads(json_str) #encode into JSON 
    with open('data2.json', 'w') as f:  #writing JSON file 
        json.dump(newly_formatted_data, f) 

Peter Otten was very helpful with subsequently converting aJ SON string to a CSV file: 


import csv 
import json 
import sys 

def hook(obj): 
    return obj 

def flatten(obj): 
    for k, v in obj: 
        if isinstance(v, list): 
            yield from flatten(v) 
        else: 
            yield k, v 

if __name__ == "__main__": 
    with open("somefileneame.json") as f: 
        data = json.load(f, object_pairs_hook=hook) 

    pairs = list(flatten(data)) 

    writer = csv.writer(sys.stdout) 
    header = writer.writerow([k for k, v in pairs]) 
    row = writer.writerow([v for k, v in pairs]) #writer.writerows for any other iterable object 

However, for the nested keys as dictionaries with values as dictionaries, I have been taking smaller chunks of the JSON objects and developing some functional programs that allow me to blow up lists: 

For example for this example JSON snippet, to de-nest by level the nested dictionaries, I use this function: 

def flatten(d, parent_key=''): 
    items = [] 
    for k, v in d.items(): 
        try: 
            items.extend(flatten(v, '%s%s_' % (parent_key, k)).items()) 
        except AttributeError: 
            items.append(('%s%s' % (parent_key, k), v)) 
    return dict(items) 
final = (flatten(data2, parent_key ='')) 


##JSON sample: 

data2 = { 
    "OTF": "0", 
    "F": "False", 
    "F": { 
        "Int32": ["0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0", 
        "0"] 
    }, 
                "D": { 
                    "B": ["0", 
                    "0", 
                    "0", 
                    "0", 
                    "0", 
                    "0", 
                    "0", 
                    "0", 
                    "0", 
                    "0", 
                    "0"] 
                }, 
                "PBDS": { 
                    "DateTime": ["1/1/0001 12:00:00 AM", 
                    "1/1/0001 12:00:00 AM", 
                    "1/1/0001 12:00:00 AM", 
                    "1/1/0001 12:00:00 AM", 
                    "1/1/0001 12:00:00 AM", 
                    "1/1/0001 12:00:00 AM", 
                    "1/1/0001 12:00:00 AM", 
                    "1/1/0001 12:00:00 AM"] 
                }, 
                "PBDS": { 
                    "Double": ["0", 
                    "0", 
                    "0", 
                    "0", 
                    "0", 
                    "0", 
                    "0", 
                    "0"] 
                }, 
                "SCS": { 
                    "String": ["1", 
                    "2"] 
                } 
            } 

The result: 


{'D_B': ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], 
 'F_Int32': ['0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0', 
  '0'], 
 'OTF': '0', 
 'PBDS_Double': ['0', '0', '0', '0', '0', '0', '0', '0'], 
 'SCS_String': ['1', '2']} 

I then have used a decorator to explode the lists (thank you to Gribouillis for feedback): 

from postprocess import post_process 
@post_process(dict) 
def explode_lists(adict): 
    for key, value in adict.items(): 
        if isinstance(value, list): 
            if all(isinstance(x, str) for x in value): 
                for i, x in enumerate(value, 1): 
                    yield ('{}{}'.format(key, i), x) 
                continue 
        yield key, value 


# postprocess.py 
def post_process(*filters): 
    """Decorator to post process a function's return value through a 
    sequence of filters (functions with a single argument). 
    Example: 
        @post_process(f1, f2, f3) 
        def f(*args, **kwd): 
            ... 
            return value 
        then calling f(...) will actually return f3( f2( f1( f(...)))). 
        This can also be used to convert a generator to a function 
        returning a sequence type: 
        @post_process(dict) 
        def my_generator(): 
            ... 
            yield key, value 
    """ 
    def decorate(func): 
        from functools import wraps 
        @wraps(func) 
        def wrapper(*args, **kwd): 
            rv = func(*args, **kwd) 
            for f in filters: 
                rv = f(rv) 
            return rv 
        return wrapper 
return decorate 

I know that this is alot of sequential steps. I am wondering if I could insert or conditionally pass these functions when originally parsing the XML, so that the JSON is formatted for more recursive reading of the JSON dictionary and then writing to CSV? I welcome constructive feedback for refactoring.... 

[toc] | [prev] | [next] | [standalone]


#92965

FromNed Batchelder <ned@nedbatchelder.com>
Date2015-06-21 08:31 -0700
Message-ID<09fe59ad-5895-4190-85ca-e068659f60a1@googlegroups.com>
In reply to#92963
On Sunday, June 21, 2015 at 10:54:44 AM UTC-4, Sahlusar wrote:

> This is a duplicate to the post titled: JSON to CSV Troubleshooting:

Please don't do that.

--Ned.

[toc] | [prev] | [next] | [standalone]


#92966

FromSahlusar <ahlusar.ahluwalia@gmail.com>
Date2015-06-21 08:39 -0700
Message-ID<f14ccc24-723e-4a5c-a7dd-7f0967fb9f01@googlegroups.com>
In reply to#92965
On Sunday, 21 June 2015 11:31:44 UTC-4, Ned Batchelder  wrote:
> On Sunday, June 21, 2015 at 10:54:44 AM UTC-4, Sahlusar wrote:
> 
> > This is a duplicate to the post titled: JSON to CSV Troubleshooting:
> 
> Please don't do that.
> 
> --Ned.

My apologies - is it possible to delete one of my posts?

[toc] | [prev] | [next] | [standalone]


#95502

Fromryguy7272 <ryanshuell@gmail.com>
Date2015-08-19 18:54 -0700
Message-ID<45204962-beac-401e-b529-b28720df0fcd@googlegroups.com>
In reply to#92758
On Wednesday, June 17, 2015 at 11:00:24 AM UTC-4, kbtyo wrote:
> I would like to have this JSON object written out to a CSV file so that the keys are header fields (for each of the columns) and the values are values that are associated with each header field. Is there a best practice for working with this? Ideally I would like to recursively iterate through the key value pairs. Thank you in advance. I am using Python 3.4 on Windows. My editor is Sublime 2. 
> 
> {
> "CF": {
> "A": "5",
> "FEC": "1/1/0001 12:00:00 AM",
> "TE": null,
> "Locator": null,
> "Message": "Transfer Fee",
> "AT": null,
> "FT": null,
> "FR": "True",
> "FY": null,
> "FR": null,
> "FG": "0",
> "Comment": null,
> "FUD": null,
> "cID": null,
> "GEO": null,
> "ISO": null,
> "TRID": null,
> "XTY": "931083",
> "ANM": null,
> "NM": null
> },
> "CF": "Fee",
> "ID": "2"
> }



Please see this link.
https://pypi.python.org/pypi/xmlutils

I'm not sure if that will help you.  I just found it toady.  Also, I'm pretty new to Python.  Anyway, hopefully, it gets you going in the right direction...

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web