How to create json file from plist file?

I want to create a json file from an existing plist file. How to create a json file from a plist file using one of these programming languages: Javascript or Java or Objective-c or Python or Ruby?

+4
source share
3 answers

Python has a plistlib module that you can use to read plist files. plistlib is available in python> = 2.6, so if you have an older version, you can get plistlib from here .

After reading plist as a dict using plistlib.readPlist you can unload it as JSON using json.dumps , for this use the json module or for the old version of python get simplejson

Here is an example:

 plist = """<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>aDict</key> <dict> <key>anotherString</key> <string>&lt;hello hi there!&gt;</string> </dict> <key>aList</key> <array> <string>A</string> <string>B</string> <integer>12</integer> <real>32.100000000000001</real> <array> <integer>1</integer> <integer>2</integer> <integer>3</integer> </array> </array> <key>aString</key> <string>Doodah</string> </dict> </plist> """ import json from plistlib import readPlist import StringIO in_file = StringIO.StringIO(plist) plist_dict = readPlist(in_file) print json.dumps(plist_dict) 

Conclusion:

 {"aList": ["A", "B", 12, 32.100000000000001, [1, 2, 3]], "aDict": {"anotherString": "<hello hi there!>"}, "aString": "Doodah"} 
+11
source

On OSX, you can use the plutil command line tool:

 plutil -convert json Data.plist 

It does not use one of these languages, but it saves you from implementing it or interacting with libraries.

+7
source

Download the property list and extract the dictionary from it. Then, using one of the many JSON frameworks, initialize an object that looks like a repository (an object that takes a dictionary as input) and writes it to a file. Should create your JSON just fine.

+3
source

All Articles