Convert AWS DynamoDB Table JSON to Simple PHP Array or JSON

Amazon DynamoDB support these datatype for data

– String
– Binary
– Number
– StringSet
– NumberSet
– BinarySet
– Map
– List
– Boolean
– Null

AWS provides command line tool to SCAN DynamoDB Tables and export as JSON file

# /usr/local/bin/aws dynamodb scan –table-name Users –query ‘Items[*]’ –region us-east-1 –output json

Amazon DynamoDB Formatted JSON using JQ

But AWS DynamoDB exports Table in special JSON format … we can’t import in MySQL without process it to normal JSON file 🙁

AWS DynamoDB JSON

<?php

echo "<pre>";
$tmp = "Users.json";

function aws_dd_json_converter($old) {

    $new = array(); $new1 = array();

	foreach($old as $k=>$v) {
	   foreach($v as $k1=>$v1) {	
       $new[$k1] = isset($v[$k1]['S'])?$v[$k1]['S']:$v[$k1]['N'];  // we can add more datatype here
       }	
	   $new1[] = $new;	
	}
	return $new1;
}

echo "<strong>Original AWS DynamoDB JSON</strong><br>";
echo $line = file_get_contents($tmp);
$array = json_decode($line,true);

$array = aws_dd_json_converter($array);
echo "<br><strong>Simple PHP Array for AWS DynamoDB JSON</strong><br>";
print_r($array);

echo "<br><strong>Simple JSON for AWS DynamoDB JSON</strong><br>";
print(json_encode($array, JSON_PRETTY_PRINT));

?>

Published by

Sandeep Verma

I’m an Entrepreneur. I’m proud to work as Blogger, LAMP Programmer, Linux Admin, Web Consultant, Cloud Manager, Apps Developer, Searcher. Concentrate > Observe > Imagine > Launch