Json dissect with python


 
Thread Tools Search this Thread
Top Forums Programming Json dissect with python
# 1  
Old 04-25-2013
Json dissect with python

hello,

I'm trying to parse a json file and print out each element along with some related infos such type and value.

I would like to do that in python and I found a nice support to quickly load the json file, instead I found a bit more difficult the information extraction.

Example, I have this json file:
Code:
{
    "glossary": {
        "title": "example glossary",
            "GlossList": {
                "GlossEntry": [
                {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossSee": "markup"
                }
                ]
            }
    }
}

I can easly load with:
Code:
import json
..
json_file = json.load(file)
..

Now I would like to print out such infos:
Code:
"glossary" is an object, its dept level is 0
"title" is a string, its dept level is 1, its value is "example glossary"
"GlossList" is an object, its dept level is 1
"GlossEntry" is an array, its dept level is 3
....
....

The json lib easly allows to match a object->dict and array->list.
How could I loop through the json file easly and print such infos?

thanks

D
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Shell Programming and Scripting

JSON Output format

Dear friends, I'm getting below API result and i would like to format them with Shell scripting. Input "id": 9, "description": "short desc", "name": "test", "name_with_namespace": "ABCD-PDFF-PLATFORM-TEST-V1 / test", "path": "test", "path_with_namespace":... (7 Replies)
Discussion started by: baluchen
7 Replies

3. Shell Programming and Scripting

Tweak python program to give result in json

Hi , Below is the script which prints result in json but when i validate it has some tab or extra space issues. JSON result { "data": } This is the line I tweaked. Please advise. print "\t{", "\"{#NAME}\":\""+container+hn+"\"}" #!/usr/bin/env python # (2 Replies)
Discussion started by: ashokvpp
2 Replies

4. Shell Programming and Scripting

Help With Json Code

Hello Experts, I have written a code to bootstrap my application and it is failing becasue it is not executing as expected. Command is as follows in my json template, "UserData" : { "Fn::Base64" : { "Fn::Join" : }, "-d", {"Ref" : "DBName"}, "-a", {"Ref" : "DBUser"}, ... (2 Replies)
Discussion started by: Siddheshk
2 Replies

5. Shell Programming and Scripting

Convert json to xml

Hello everyone, I have created a workflow that will pull down data via a RESTful API in JSON, then my code parses and modifies said data. The problem I have is that these APIs I am working with only accept XML to PUT/POST data, and I am looking for an easy way to convert my JSON file to XML. ... (2 Replies)
Discussion started by: Zaphod_B
2 Replies

6. Programming

Java HTTP PUT Request/JSON Not Working But Using Python It Does ?

I have some code that I have been playing around with learning Java: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; ... (1 Reply)
Discussion started by: metallica1973
1 Replies

7. Programming

Perl array / json

Hi All I have used the below code to print the dumper of a json #!/usr/bin/perl use LWP::Simple; use JSON qw( decode_json ); use Data::Dumper; use strict; use warnings; my (%list); my $trendsurl =... (5 Replies)
Discussion started by: ab52
5 Replies

8. Programming

Perl Json and Hash

Hi all, i am using the following code to that use curl to that outputs a json, i am stuck it into a hash but i canot figure out how to get just the value i need ( hostname) here is my code use warnings; use strict; use Data::Dumper qw(Dumper); ##use JSON; use JSON::XS; my $curl=... (2 Replies)
Discussion started by: ab52
2 Replies
Login or Register to Ask a Question