LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2015, 02:17 PM   #1
zinon75
LQ Newbie
 
Registered: Mar 2011
Location: europe
Distribution: debian, ubuntu, mint, redhat, ttylinux
Posts: 14

Rep: Reputation: 0
Question Python: load json file and print the values of a field


I'm new in python.

I have a json file with this structure:

{ "decision":[{"first":"01", "second":"02", "third":"03"},
{"first":"04", "second":"05", "third":"06"}] }

I'm trying to print the values of the "first" field until the
end of the file.

With the following program i'm printing all the contents
of the file:


#!/usr/bin/python

import json
from pprint import pprint

json_data=open('./temp.json')
data = json.load(json_data)

pprint(data)

json_data.close()


What should i add to this program to print the values
of the "first" field ?
 
Old 03-11-2015, 02:24 PM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Code:
data['decision'][0]['first']
 
1 members found this post helpful.
Old 03-11-2015, 03:09 PM   #3
zinon75
LQ Newbie
 
Registered: Mar 2011
Location: europe
Distribution: debian, ubuntu, mint, redhat, ttylinux
Posts: 14

Original Poster
Rep: Reputation: 0
SoftSprocket,
ok it worked for the first value of the "first" field.

How can i print all the values of the "first" field
from the beggining of the file until the end ?
 
Old 03-11-2015, 03:22 PM   #4
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Code:
for i in range (0, len (data['decision'])):
    print data['decision'][i]['first']
len is a python builtin that will give you the number of elements in the 'decision' array. The for loop creates a range for 0 to that length and iterates over it assigning an ascending value to i.
 
1 members found this post helpful.
Old 03-11-2015, 03:33 PM   #5
zinon75
LQ Newbie
 
Registered: Mar 2011
Location: europe
Distribution: debian, ubuntu, mint, redhat, ttylinux
Posts: 14

Original Poster
Rep: Reputation: 0
Thumbs up

Problem solved

Thanks SoftSprocket !
Two answers in zero time !!
 
  


Reply

Tags
json, python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read json files using python danoc93 Programming 1 08-19-2012 02:15 AM
write a file fo data array (with float values) in a binary format in Python justinlapin Programming 3 04-06-2012 04:35 AM
[SOLVED] Count occurrence of character in field and print in a new field Trd300 Linux - Newbie 5 03-21-2012 07:57 PM
[SOLVED] awk: how to print a field when field position is unknown? elfoozo Programming 12 08-18-2010 03:52 AM
how not to print the 4th field from a text file with six fields livetoday Red Hat 3 10-02-2007 01:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration