I want to create a json file from from existing plist file. How do I create json file from plist file using one of these programming languages: Javascript or Java or Objective-c or Python or Ruby?
Python has a module plistlib which you can use to read plist files. plistlib is available in python >= 2.6, so if you have old version you can get plistlib from here.
After reading plist as dict using plistlib.readPlist you can dump it as JSON using json.dumps, for that use json module or for old python version 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><hello hi there!></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)
Output:
{"aList": ["A", "B", 12, 32.100000000000001, [1, 2, 3]], "aDict": {"anotherString": "<hello hi there!>"}, "aString": "Doodah"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With