0

I have a text file which consists of Nested JSON Structure, I need to pick the objects from the file and print it in separate file in row wise fashion. For Example,

Let's say that

I have a array of records inside my JSON file

object: [{"ID":198888,"sub":"nnn","topic":"python"},{"ID":19889,"sub":"nnj","topic":"jython"}]

I have to parse above file and reprint the objects in another file as

{"ID":198888,"sub":"nnn","topic":"python"}
{"ID":19889,"sub":"nnj","topic":"jython"}

This has to be done in Python

QuestionC
  • 10,006
  • 4
  • 26
  • 44
  • is `object:` actually in the file? If so, it's not valid JSON... – mgilson Mar 12 '14 at 16:33
  • 1
    And what problems have you ran into so far? You have a JSON list there, it's not hard to parse that with the `json` module, and write out each separate value with a newline in between to another file. – Martijn Pieters Mar 12 '14 at 16:33
  • This is rolled into Python already and it is pretty well documented. http://docs.python.org/2.7/library/json.html – QuestionC Mar 12 '14 at 16:37
  • have a look at the help(json) from your terminal >>> – Zuko Mar 12 '14 at 16:46

2 Answers2

0

See this question ...

Parsing values from a JSON file using Python?

Have information of your question. @mgilson is correct. You need extra caracters for parse your "string" see below:

import json
>>> data = json.loads('{"object": [{"ID":198888,"sub":"nnn","topic":"python"},{"ID":19889,"sub":"nnj","topic":"jython"}]}')
>>> data
{u'object': [{u'topic': u'python', u'ID': 198888, u'sub': u'nnn'}, {u'topic': u'jython', u'ID': 19889, u'sub': u'nnj'}]}

This method access the first object of data object

>>> data['object']
[{u'topic': u'python', u'ID': 198888, u'sub': u'nnn'}, {u'topic': u'jython', u'ID': 19889, u'sub': u'nnj'}]

This command access the first item of object

>>> data['object'][0]
{u'topic': u'python', u'ID': 198888, u'sub': u'nnn'}

This command access the second item of object

>>> data['object'][1]
{u'topic': u'jython', u'ID': 19889, u'sub': u'nnj'}

Data Structures Python is good reading to understand how Python works.

Community
  • 1
  • 1
Jones
  • 1,480
  • 19
  • 34
0

Python's built-in json library

look into json.loads() and json.dumps()

drez90
  • 814
  • 5
  • 17