Been playing with the new Twitter API, version 1.1, and want to preserve a bunch of code, so I wrote a routine to convert the new format to the old format. Maybe it’ll save someone time. (Note that I didn’t convert the metadata because I didn’t need it.)

Here’s the code which uses JSON.NET:

            JObject newapiresults = null;  
            newapiresults = JObject.Parse(response.Content);
            JArray tweets = new JArray();
            foreach (JObject status in tweets)
            {
                JObject oldStatus = new JObject();
                oldStatus["created_at"] = status["created_at"];
                oldStatus["from_user"] = status["user"]["screen_name"];
                oldStatus["from_user_id"] = status["user"]["id"];
                oldStatus["from_user_id_str"] = status["user"]["id_str"];
                oldStatus["from_user_id_name"] = status["user"]["name"];
                oldStatus["geo"] = status["geo"];
                oldStatus["id"] = status["id"];
                oldStatus["id_str"] = status["id_str"];
                oldStatus["iso_language_code"] = status["metadata"]["iso_language_code"];
                oldStatus["metadata"] = status["metadata"];
                oldStatus["profile_image_url"] = status["user"]["profile_image_url"];
                oldStatus["profile_image_url_https"] = status["user"]["profile_image_url_https"];
                oldStatus["source"] = status["source"];
                oldStatus["text"] = status["text"];
                oldStatus["to_user"] = status["in_reply_to_screen_name"];
                oldStatus["to_user_id"] = status["in_reply_to_user_id"];
                oldStatus["to_user_id_str"] = status["in_reply_to_user_id_str"];
                oldStatus["to_user_name"] = status["in_reply_to_screen_name"];
                oldStatus["in_reply_to_status_id"] = status["in_reply_to_status_id"];
                oldStatus["in_reply_to_status_id_str"] = status["in_reply_to_status_id_str"];
                results.Add(oldStatus);


            }
            JObject result = new JObject();
            result["results"] = results;
            //do something with the string
            UploadText(result.ToString());

For what it is worth, here’s the old JSON and then the new JSON – wow the new JSON is a lot more verbose!

{ "completedin" : 0.029000000000000001, 
"max
id" : 265608574640738304, "maxidstr" : "265608574640738304", "nextpage" : "?page=2&maxid=265608574640738304&q=%40twitterapi%20-via", "page" : 1, "query" : "%40twitterapi+-via", "refreshurl" : "?sinceid=265608574640738304&q=%40twitterapi%20-via", "results" : [ { "createdat" : "Tue, 06 Nov 2012 00:16:33 +0000", "fromuser" : "morgules", "fromuserid" : 636921780, "fromuseridstr" : "636921780", "fromusername" : "Дмитрий Моргулес", "geo" : null, "id" : 265608574640738304, "idstr" : "265608574640738304", "isolanguagecode" : "ru", "metadata" : { "resulttype" : "recent" }, "profileimageurl" : "http://a0.twimg.com/profileimages/2647733681/0daea79d4d5858a39bb1801d64b20e14normal.jpeg", "profileimageurlhttps" : "https://si0.twimg.com/profileimages/2647733681/0daea79d4d5858a39bb1801d64b20e14normal.jpeg", "source" : "<a href="http://twitter.com/tweetbutton">Tweet Button</a>", "text" : "Руслан Нурисламов: безрукий барабанщик из Златоуста | Общество | Слово http://t.co/ziOSiA78 с помощью @twitterapi", "touser" : null, "touserid" : 0, "touseridstr" : "0", "tousername" : null }, { "createdat" : "Tue, 06 Nov 2012 00:13:12 +0000", "fromuser" : "oxkarlomejor", "fromuserid" : 221827509, "fromuseridstr" : "221827509", "fromusername" : "oscar david ", "geo" : null, "id" : 265607731417870336, "idstr" : "265607731417870336", "isolanguagecode" : "es", "metadata" : { "resulttype" : "recent" }, "profileimageurl" : "http://a0.twimg.com/profileimages/1602975094/Imagen010normal.jpg", "profileimageurlhttps" : "https://si0.twimg.com/profileimages/1602975094/Imagen010normal.jpg", "source" : "<a href="http://twitter.com/">web</a>", "text" : "@twitterapi quiero ser Famoso", "touser" : "twitterapi", "touserid" : 6253282, "touseridstr" : "6253282", "tousername" : "Twitter API" }]}
 
And here’s the new JSON:
{ "searchmetadata" : { "completedin" : 0.089999999999999997,
      "count" : 100,
      "maxid" : 265255057749053440,
      "maxidstr" : "265255057749053440",
      "nextresults" : "?maxid=265212014639603711&q=nekocase&count=100&includeentities=1",
      "query" : "nekocase",
      "refreshurl" : "?sinceid=265255057749053440&q=nekocase&includeentities=1",
      "sinceid" : 0,
      "sinceidstr" : "0"
    },
  "statuses" : [ { "contributors" : null,
        "coordinates" : null,
        "createdat" : "Mon Nov 05 00:51:49 +0000 2012",
        "entities" : { "hashtags" : [  ],
            "media" : [ { "displayurl" : "pic.twitter.com/PEzikMU5",
                  "expandedurl" : "http://twitter.com/jtspicer/status/265255057749053440/photo/1",
                  "id" : 265255057753247745,
                  "idstr" : "265255057753247745",
                  "indices" : [ 115,
                      135
                    ],
                  "mediaurl" : "http://p.twimg.com/A65gEdRCEAEf0xo.jpg",
                  "mediaurlhttps" : "https://p.twimg.com/A65gEdRCEAEf0xo.jpg",
                  "sizes" : { "large" : { "h" : 766,
                          "resize" : "fit",
                          "w" : 1024
                        },
                      "medium" : { "h" : 449,
                          "resize" : "fit",
                          "w" : 600
                        },
                      "small" : { "h" : 254,
                          "resize" : "fit",
                          "w" : 340
                        },
                      "thumb" : { "h" : 150,
                          "resize" : "crop",
                          "w" : 150
                        }
                    },
                  "type" : "photo",
                  "url" : "http://t.co/PEzikMU5"
                } ],
            "urls" : [  ],
            "usermentions" : [ { "id" : 126406217,
                  "idstr" : "126406217",
                  "indices" : [ 20,
                      29
                    ],
                  "name" : "Neko Case",
                  "screenname" : "NekoCase"
                } ]
          },
        "favorited" : false,
        "geo" : null,
        "id" : 265255057749053440,
        "idstr" : "265255057749053440",
        "inreplytoscreenname" : null,
        "inreplytostatusid" : null,
        "inreplytostatusidstr" : null,
        "inreplytouserid" : null,
        "inreplytouseridstr" : null,
        "metadata" : { "isolanguagecode" : "en",
            "resulttype" : "recent"
          },
        "place" : null,
        "possiblysensitive" : false,
        "retweetcount" : 0,
        "retweeted" : false,
        "source" : "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>",
        "text" : "The best way to get @NekoCase to RT you is to tweet a context-free photo of an adorable animal. Here goes nothing! http://t.co/PEzikMU5",
        "truncated" : false,
        "user" : { "contributorsenabled" : false,
            "createdat" : "Mon Feb 23 19:57:21 +0000 2009",
            "defaultprofile" : false,
            "defaultprofileimage" : false,
            "description" : "Writer of things. Lover of music. Appreciator of film. Unabashed know-it-all. Street walkin' cheetah with a heart full of napalm. And so on. Views=mine.",
            "entities" : { "description" : { "urls" : [  ] } },
            "favouritescount" : 768,
            "followrequestsent" : null,
            "followerscount" : 360,
            "following" : null,
            "friendscount" : 336,
            "geoenabled" : false,
            "id" : 21686478,
            "idstr" : "21686478",
            "istranslator" : false,
            "lang" : "en",
            "listedcount" : 8,
            "location" : "Columbus, Ohio (Short North)",
            "name" : "Justin Spicer",
            "notifications" : null,
            "profilebackgroundcolor" : "D3D9DB",
            "profilebackgroundimageurl" : "http://a0.twimg.com/profilebackgroundimages/243592391/x3ce2eb796060291bf2a9d40db53a85f.jpg",
            "profilebackgroundimageurlhttps" : "https://si0.twimg.com/profilebackgroundimages/243592391/x3ce2eb796060291bf2a9d40db53a85f.jpg",
            "profilebackgroundtile" : false,
            "profilebannerurl" : "https://si0.twimg.com/profilebanners/21686478/1351622268",
            "profileimageurl" : "http://a0.twimg.com/profileimages/2785722127/42dbe3599093209c9abab988e56f2cdfnormal.jpeg",
            "profileimageurlhttps" : "https://si0.twimg.com/profileimages/2785722127/42dbe3599093209c9abab988e56f2cdfnormal.jpeg",
            "profilelinkcolor" : "DB6995",
            "profilesidebarbordercolor" : "5AC3E9",
            "profilesidebarfillcolor" : "2D1E29",
            "profiletextcolor" : "A177AB",
            "profileusebackgroundimage" : true,
            "protected" : false,
            "screenname" : "jtspicer",
            "showallinlinemedia" : true,
            "statusescount" : 11964,
            "timezone" : "Eastern Time (US & Canada)",
            "url" : null,
            "utcoffset" : -18000,
            "verified" : false
          }
      },
      { "contributors" : null,
        "coordinates" : null,
        "createdat" : "Mon Nov 05 00:42:28 +0000 2012",
        "entities" : { "hashtags" : [  ],
            "urls" : [  ],
            "usermentions" : [ { "id" : 126406217,
                  "idstr" : "126406217",
                  "indices" : [ 0,
                      9
                    ],
                  "name" : "Neko Case",
                  "screenname" : "NekoCase"
                },
                { "id" : 134073162,
                  "idstr" : "134073162",
                  "indices" : [ 10,
                      21
                    ],
                  "name" : "brigid ",
                  "screenname" : "MBbyBrigid"
                }
              ]
          },
        "favorited" : false,
        "geo" : null,
        "id" : 265252709555376128,
        "idstr" : "265252709555376128",
        "inreplytoscreenname" : "NekoCase",
        "inreplytostatusid" : 265237602242818049,
        "inreplytostatusidstr" : "265237602242818049",
        "inreplytouserid" : 126406217,
        "inreplytouseridstr" : "126406217",
        "metadata" : { "isolanguagecode" : "en",
            "resulttype" : "recent"
          },
        "place" : null,
        "retweetcount" : 0,
        "retweeted" : false,
        "source" : "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>",
        "text" : "@NekoCase @mbbybrigid thank you so much for sharing the fundraiser!",
        "truncated" : false,
        "user" : { "contributorsenabled" : false,
            "createdat" : "Thu Dec 16 20:06:39 +0000 2010",
            "defaultprofile" : false,
            "defaultprofileimage" : false,
            "description" : "The owner/crafter/dreamer behind Cambridge's first stitch lounge and craft studio. Avid gardener. Opinionated stitcher. And I want you to make something...",
            "entities" : { "description" : { "urls" : [  ] },
                "url" : { "urls" : [ { "expandedurl" : null,
                          "indices" : [ 0,
                              27
                            ],
                          "url" : "http://gatherhereonline.com"
                        } ] }
              },
            "favouritescount" : 520,
            "followrequestsent" : null,
            "followerscount" : 1024,
            "following" : null,
            "friendscount" : 437,
            "geoenabled" : false,
            "id" : 227419407,
            "idstr" : "227419407",
            "istranslator" : false,
            "lang" : "en",
            "listedcount" : 47,
            "location" : "Cambridge, MA",
            "name" : "virginia b. johnson",
            "notifications" : null,
            "profilebackgroundcolor" : "C0DEED",
            "profilebackgroundimageurl" : "http://a0.twimg.com/profilebackgroundimages/293836111/spoolsofthread.jpg",
            "profilebackgroundimageurlhttps" : "https://si0.twimg.com/profilebackgroundimages/293836111/spoolsofthread.jpg",
            "profilebackgroundtile" : true,
            "profileimageurl" : "http://a0.twimg.com/profileimages/1192172838/etsygatherhereavatarnormal.jpg",
            "profileimageurlhttps" : "https://si0.twimg.com/profileimages/1192172838/etsygatherhereavatarnormal.jpg",
            "profilelinkcolor" : "0084B4",
            "profilesidebarbordercolor" : "C0DEED",
            "profilesidebarfillcolor" : "DDEEF6",
            "profiletextcolor" : "333333",
            "profileusebackgroundimage" : true,
            "protected" : false,
            "screenname" : "gatherhere",
            "showallinlinemedia" : false,
            "statusescount" : 7655,
            "timezone" : "Central Time (US & Canada)",
            "url" : "http://gatherhereonline.com",
            "utcoffset" : -21600,
            "verified" : false
          }
      }]}