Command line tool to convert PLIST to JSON?

Is there a command line tool to convert .plist files to JSON?

If not, what will be the approach for creating one using Objective-C or C on Mac? For example, JSONKit for Objective-C. How can I open a .plist file, pass it JSONKit and serialize it as JSON?

+58
json objective-c plist
May 20 '11 at 12:39 a.m.
source share
6 answers

If you are on a Mac, you can use the plutil tool on the command line (this is related to the developer tools that I consider):

plutil -convert json Data.plist 

as stated in the comments, this will overwrite existing data. To output to a new file

 plutil -convert json -o Data.json Data.plist 
+150
Jun 12 2018-12-12T00
source share

The next one does the job -

 // convertPlistToJSON.m #import <Foundation/Foundation.h> #import "JSONKit.h" int main(int argc, char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; if(argc != 3) { fprintf(stderr, "usage: %s FILE_PLIST FILE_JSON\n", argv[0]); exit(5); } NSString *plistFileNameString = [NSString stringWithUTF8String:argv[1]]; NSString *jsonFileNameString = [NSString stringWithUTF8String:argv[2]]; NSError *error = NULL; NSData *plistFileData = [NSData dataWithContentsOfFile:plistFileNameString options:0UL error:&error]; if(plistFileData == NULL) { NSLog(@"Unable to read plist file. Error: %@, info: %@", error, [error userInfo]); exit(1); } id plist = [NSPropertyListSerialization propertyListWithData:plistFileData options:NSPropertyListImmutable format:NULL error:&error]; if(plist == NULL) { NSLog(@"Unable to deserialize property list. Error: %@, info: %@", error, [error userInfo]); exit(1); } NSData *jsonData = [plist JSONDataWithOptions:JKSerializeOptionPretty error:&error]; if(jsonData == NULL) { NSLog(@"Unable to serialize plist to JSON. Error: %@, info: %@", error, [error userInfo]); exit(1); } if([jsonData writeToFile:jsonFileNameString options:NSDataWritingAtomic error:&error] == NO) { NSLog(@"Unable to write JSON to file. Error: %@, info: %@", error, [error userInfo]); exit(1); } [pool release]; pool = NULL; return(0); } 

It does some reasonable error checking, but it is not bullet proof. Use at your own risk.

To create the tool you need JSONKit . Place JSONKit.m and JSONKit.h in the same directory as convertPlistToJSON.m , and then compile with:

 shell% gcc -o convertPlistToJSON convertPlistToJSON.m JSONKit.m -framework Foundation 

Using:

 shell% convertPlistTOJSON usage: convertPlistToJSON FILE_PLIST FILE_JSON shell% convertPlistTOJSON input.plist output.json 

Reads in input.plist and writes pretty printed JSON to output.json .

+5
May 21 '11 at 2:56
source share

The code is pretty simple for this:

 NSArray* array = [[NSArray arrayWithContentsOfFile:[@"~/input.plist" stringByExpandingTildeInPath]]retain]; SBJsonWriter* writer = [[SBJsonWriter alloc] init]; NSString* s = [[writer stringWithObject:array] retain]; [s writeToFile:[@"~/output.json" stringByExpandingTildeInPath] atomically:YES]; [array release]; 

I never looked back to get him to take arguments, since I only needed 3 files.

+2
May 20 '11 at 2:36
source share

I wrote a tool in python for this. See here:

http://sourceforge.net/projects/plist2json

Works from the command line on os x or linux distros, batch converts the directory. It is short and simple, so it is easy to change for your own purposes.

+2
Dec 31 '11 at 20:47
source share

There is a native way to convert plist to json . It was called NSJSONSerialization .

Here is an example of how to use it, and convert the plist file to a json file:

 NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:@"input.plist"]; NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:plistDict options:NSJSONWritingPrettyPrinted error:&error]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; [jsonString writeToFile:@"output.json" atomically:NO encoding:NSUTF8StringEncoding error:&error]; 
+2
Apr 30 '14 at 15:27
source share

Using Mac Utilities

Convert plist → json

 plutil -convert json -o output.json input.plist 

Convert json -> plist

 plutil -convert xml1 input.json -o output.plist 
+1
Jun 03 '19 at 15:03
source share



All Articles