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


Groups > comp.lang.python > #92808

Re: JSON Object to CSV Question

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!bcyclone02.am1.xlned.com!bcyclone02.am1.xlned.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'value,': 0.03; 'ideally': 0.04; 'python3': 0.05; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'column': 0.07; 'json': 0.07; 'null,': 0.07; 'subject:Question': 0.07; 'core,': 0.09; 'iterate': 0.09; 'obj': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.14; 'output': 0.15; 'skip:f 30': 0.15; '"2"': 0.16; 'clashes': 0.16; 'csv': 0.16; 'dictionary,': 0.16; 'dictionary.': 0.16; 'example)': 0.16; 'former,': 0.16; 'list):': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:CSV': 0.16; 'subject:Object': 0.16; 'writer': 0.16; 'wrote:': 0.16; 'string': 0.17; 'duplicate': 0.18; 'headers': 0.18; '(the': 0.22; 'keys': 0.22; 'names.': 0.22; 'tuples': 0.22; 'advance.': 0.23; 'import': 0.24; 'written': 0.24; 'header': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'yield': 0.27; "i'm": 0.29; 'dictionary': 0.29; 'key,': 0.29; 'unlikely': 0.29; 'values': 0.30; 'code': 0.31; 'core': 0.32; 'included': 0.32; 'align': 0.33; 'null': 0.33; 'particular,': 0.33; 'though.': 0.33; 'values.': 0.33; 'windows.': 0.33; 'another': 0.34; '(for': 0.34; 'editor': 0.34; 'this?': 0.34; 'file': 0.34; 'advice': 0.35; 'skip:c 30': 0.35; 'to:addr:python- list': 0.35; 'done': 0.35; 'follows:': 0.35; 'skip:o 20': 0.35; 'there': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'associated': 0.38; 'received:org': 0.38; 'mean': 0.38; 'thank': 0.39; 'engineering': 0.39; 'to:addr:python.org': 0.39; 'data': 0.40; 'received:de': 0.40; 'some': 0.40; 'field': 0.60; 'your': 0.60; 'more': 0.62; 'goal': 0.64; 'here': 0.66; 'strategy': 0.69; 'capabilities': 0.72; '3.4': 0.84; 'evening': 0.84; 'object:': 0.84; 'sublime': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: JSON Object to CSV Question
Date Thu, 18 Jun 2015 09:43:12 +0200
Organization None
References <CAC9q6M4w5mNj2Lb2MxND_46jbiSwk123mKGKBJckmYCo+_MoSw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd8c90.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.583.1434613423.13271.python-list@python.org> (permalink)
Lines 106
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1434613423 news.xs4all.nl 2900 [2001:888:2000:d::a6]:33184
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 6635
X-Received-Body-CRC 2724304481
Xref csiph.com comp.lang.python:92808

Show key headers only | View raw


Saran Ahluwalia wrote:

> Good Evening Everyone:
> 
> 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.
> 
> Here is the JSON object:
> 
> {
> "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" should not be included as a
> column header (only as a value of the key "CF").
> 
> Other than the former, the keys should be headers and the corresponding
> tuples - the field values.
> 
> In essence, my goal is to the following:
> 
> You get a dictionary object (the "outer" dictionary)
> You get the data from the "CF" key (fixed name?) which is a string ("Fee"
> in your example)
> You use that value as a key to obtain another value from the same "outer"
> dictionary, which should be a another dictionary (the "inner" dictionary)
> You make a CSV file with:
> 
>    - a header that contains "CF" plus all keys in the "inner" dictionary
>    that have an associated value
>    - the value from key "CF" in the "outer" dictionary plus all non-null
>    values in the "inner" dictionary.
> 
> I have done the following:

For some value of "I" :(
> 
> 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("data.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])
> 
> The output is as follows:
> 
> $ 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
> 
> I do not want to have duplicate column names.

If you tell the exact output you want and in particular how you want to 
resolve any name clashes I might give you a hint. I'm unlikely to produce 
more code though.

> Any advice on other best practices that I may utilize?

Align your engineering efforts and capabilities to deliver on your strategy 
and, in particular, your three core, I mean your four core ambitions...

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: JSON Object to CSV Question Peter Otten <__peter__@web.de> - 2015-06-18 09:43 +0200

csiph-web