I have a javascript file which includes the data in the following format.
index = {
"me" : {
"id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
"firstName" : "A=",
"lastName" : "T",
"pictureName" : "66s2c.jpg",
"username" : "a-t"
},
"spaces" : [ {
"org" : {
"id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
"firstName" : "An",
"lastName" : "Tuli",
"pictureName" : "66s2c.jpg",
"username" : "arli"
}
}, {
"user" : {
"id" : "60c4a171-172f-4f66-9014-8b4cf3e476e6",
"firstName" : "Ban",
"lastName" : "Idris",
"pictureName" : "../../../../default-pic/butterfly_200.png",
"username" : "banun-idris"
}
} ]
}
users["524bd089-2a7b-4a71-ba23-16354d6351ae"] = {
"id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
"firstName" : "A",
"lastName" : "T",
"pictureName" : "66s2c.jpg",
"username" : "a",
"libraries" : [ "lEy27AZavfSR", "l0yApAoo2l4b", "lJl22YOtacxY", "l0UhMCvrMmka", "lJMWpIoFnaK4", "lCZ9cYYjVJcv", "l8kynpyoaej7" ]
}
libraries["lEy27AZavfSR"] = {
"id" : "lEy27AZavfSR",
"name" : "My Main Library",
"description" : null,
"numKeeps" : 0,
"keeps" : [ ]
}
keeps["k4r5UIugqgfk"] = {
"id" : "k4r5UIugqgfk",
"keptAt" : 1449613295000,
"lastActivityAt" : 1449613295000,
"title" : "",
"url" : "",
"note" : null,
"tags" : [ ],
"libraries" : [ "lJl22YOtacxY" ],
"summary" : "",
"messages" : [ ]
}
I need to import data from this type of format into my mysql database.
I can't finalize and nail down the approach to do this
I tried to get the file content into a php file and then convert them into a array string but i get a null result.
$jsondata = file_get_contents('trial_data.js');
$data = json_decode($jsondata, true);
var_dump($data);
The second approach I think i can take is load the data in jquery file and send it using AJAX to a php file which adds it to the database.
Which approach do you think i should take and how to go about it.
Plus i need to figure out the ID from the first index array, so that I can get the ID from the me and org object for which i need to get the rest of the information
Since your data is not a valid JSON, you can use a regex to parse it, like this:
$file_content = file_get_contents('trial_data.js');
preg_match_all("/(\w+)\[\"([^\]]*)\"\]\s*\=\s*\{([^\}]*)\}/", $file_content, $matches);
foreach ($matches[1] as $key => $value) {
$data[$value][$matches[2][$key]] = json_decode('{'.$matches[3][$key].'}');
}
print_r($data);
//For example:
echo $data['users']['524bd089-2a7b-4a71-ba23-16354d6351ae']->pictureName;
//Output: 66s2c.jpg
Demo: https://3v4l.org/fW5gH
Related
I have a Firebase realtime-database export-json that has nested informations, that I would like to extract. The structure of the JSON looks like this:
{
"users" : {
"024w97mv8NftGFY8THfQIU6PhaJ3" : {
"email" : "xxx",
"id" : "024w97mv8NftGFY8THfQIU6PhaJ3",
"name" : "xxx",
"items" : {
"-LQL9n-r9BGdo3HJZ2sk" : {
"disliked" : true,
"id" : 396,
"name" : "Aaa"
},
"-LQL9oO63nH-QW2w6zz0" : {
"liked" : true,
"id" : 3674,
"name" : "Bbb"
}
}
},
"0ERLT5DLRvbZUnjlnM7Ow0qItpz2" : {
"email" : "zzz",
"id" : "0ERLT5DLRvbZUnjlnM7Ow0qItpz2",
"name" : "zzz",
"items" : {
"-LIZnriSVQMzqTsPFNYa" : {
"id" : 396,
"liked" : true,
"name" : "Aaa"
},
"-LIZnrzOuk4WyjqEiLG8" : {
"disliked" : true,
"id" : 4805,
"name" : "Ccc"
}
}
}
}
}
What I need to achieve is getting a list of all liked item-names, and ideally counting how often an item is liked.
Is there a simple tool or script to do that? Ruby or Javascript would be preferred. Thanks a lot!
You can parse your JSON data in ruby like below
result_hash = JSON.parse(result)
result_ary = result_hash["users"].collect do |k,v|
v["items"].values.select{|v1| v1["liked"] == true }
end
result_data = result_ary.flatten
result of parsing
=> [{"liked"=>true, "id"=>3674, "name"=>"Bbb"}, {"id"=>396, "liked"=>true, "name"=>"Aaa"}]
Now its very easy for getting your required result
result_data.collect{|x| x["name"] }
=> ["Bbb", "Aaa"]
result_data.count {|x| x["name"] == "Aaa"}
=> 1
result_data.count {|x| x["name"] == "Bbb"}
=> 1
I have an document that contains array of arrays i am using embedded document in MongoDb.Say i have collection name Orders looks like:-
"_id" : "HjPGrdkffg7dQPtiX",
"ListOrdersResult" : [
{
"Orders" : {
"Order" : [
{
"LatestShipDate" : "2016-01-13T18:29:59Z",
"OrderType" : "StandardOrder",
"PurchaseDate" : "2016-01-11T10:24:49Z",
"PaymentExecutionDetail" : {
"PaymentExecutionDetailItem" : {
"PaymentMethod" : "COD",
"Payment" : {
"CurrencyCode" : "INR",
"Amount" : "839.30"
}
}
},
"BuyerEmail" : "vccdbptpx2ssd74882#marketplace.amazon.in",
"AmazonOrderId" : "402-4031538-7451469",
"LastUpdateDate" : "2016-01-14T06:47:17Z",
"ShipServiceLevel" : "IN Exp Dom 2",
"NumberOfItemsShipped" : "1",
"OrderStatus" : "Shipped",
"SalesChannel" : "Amazon.in",
"ShippedByAmazonTFM" : "false",
"LatestDeliveryDate" : "2016-01-19T18:29:59Z",
"NumberOfItemsUnshipped" : "0",
"BuyerName" : "xyz",
"EarliestDeliveryDate" : "2016-01-13T18:30:00Z",
"OrderTotal" : {
"CurrencyCode" : "INR",
"Amount" : "839.30"
},
"IsPremiumOrder" : "false",
"EarliestShipDate" : "2016-01-11T18:30:00Z",
"MarketplaceId" : "A21TJRRWUN4KGVC",
"FulfillmentChannel" : "MFN",
"TFMShipmentStatus" : "Delivered",
"PaymentMethod" : "COD",
"ShippingAddress" : {
"StateOrRegion" : "HARYANA",
"City" : "GURGAON",
"Phone" : "9999999999",
"CountryCode" : "IN",
"PostalCode" : "122001",
"Name" : "Murthy",
"AddressLine1" : "House No. , J Block, Badshahpur"
},
"IsPrime" : "false",
"ShipmentServiceLevelCategory" : "Expedited"
},
{
"LatestShipDate" : "2016-01-13T18:29:59Z",
"OrderType" : "StandardOrder",
"PurchaseDate" : "2016-01-11T13:16:49Z",
"PaymentExecutionDetail" : {
"PaymentExecutionDetailItem" : {
"PaymentMethod" : "COD",
"Payment" : {
"CurrencyCode" : "INR",
"Amount" : "899.40"
}
}
},
"BuyerEmail" : "xyz#marketplace.amazon.in",
"AmazonOrderId" : "402-2142159-5087541",
"LastUpdateDate" : "2016-01-14T06:47:15Z",
"ShipServiceLevel" : "IN Exp Dom 2",
"NumberOfItemsShipped" : "1",
"OrderStatus" : "Cancel",
"SalesChannel" : "Amazon.in",
"ShippedByAmazonTFM" : "false",
"LatestDeliveryDate" : "2016-01-19T18:29:59Z",
"NumberOfItemsUnshipped" : "0",
"BuyerName" : "demo prakash",
"EarliestDeliveryDate" : "2016-01-13T18:30:00Z",
"OrderTotal" : {
"CurrencyCode" : "INR",
"Amount" : "899.40"
},
"IsPremiumOrder" : "false",
"EarliestShipDate" : "2016-01-11T18:30:00Z",
"MarketplaceId" : "A21TJEUUN4WGV",
"FulfillmentChannel" : "MFN",
"TFMShipmentStatus" : "Delivered",
"PaymentMethod" : "COD",
"ShippingAddress" : {
"StateOrRegion" : "DELHI",
"City" : "DELHI",
"Phone" : "99999999",
"CountryCode" : "IN",
"PostalCode" : "110038",
"Name" : "Demo prakash",
"AddressLine1" : "Hn 638 gali n 04 Wazirabad new delhi"
},
"IsPrime" : "false",
"ShipmentServiceLevelCategory" : "Expedited"
},
}
]
},
"CreatedBefore" : "2015-03-19T06:17:59Z"
}
],
"ResponseMetadata" : {
"RequestId" : "cf94645e-ada7-4ec6-b161-a97d07a77817"
},
"seller_user_id" : "yg4e34ccodzf3GPR2",
}
So as you can see this is the single document that contains the whole data of array i want to fetch the orders whose status is cancel from this order array.
So for that i have use :-
var orderDetails =
orders.find({"ListOrdersResult.Orders.Order":{$elemMatch:
{ OrderStatus:"Canceled"}}}).fetch();
Also i tried with:-
orders.find({"ListOrdersResult.Orders.Order.OrderStatus":'Canceled'}).fetch();
So this will return the whole document that contains status as canceled and other as well but i want only selected result from the document that contains status as pending.
So is there any way in mongoDb to query a selected value from a single document that contains nested array of arrays as object.
Or I need to staore the values into diff diff documents thats only the solution.
Any help would be appriciated please contribute
Thanks!
You can go with any meteor aggregate package. You can use match and then group the data then send it to client-side.Like :
var ordersLines = orders.aggregate([
{$unwind : "$ListOrdersResult.Orders.Order"},
{$match : { OrderStatus:"Canceled"} },
{$project : {
OrderType : '$ListOrdersResult.Orders.Order.OrderType'
....
}
}
]);
return ordersLines;
But I suggest you go with a different document.
I am trying to get only the ObjectId's from One specific Document that is embedded in the projects Array.
Basically I am trying to make a database that will have users and each user will have there own projects.
Thank you !
db.users.find().pretty()
{
"_id" : ObjectId("5762c0cf2b9a78006373a684"),
"name" : "seq",
"pass" : "seq",
"projects" : [
{
"pid" : ObjectId("5762c0ba2b9a78006373a682"),
"name" : "aaa"
},
{
"pid" : ObjectId("5762c0ba2b9a78006373a683"),
"name" : "bbb"
}
]
}
{
"_id" : ObjectId("5762c28d2b9a78006373a687"),
"name" : "leq",
"pass" : "leq",
"projects" : [
{
"pid" : ObjectId("5762c2892b9a78006373a685"),
"name" : "ccc"
},
{
"pid" : ObjectId("5762c2892b9a78006373a686"),
"name" : "ddd"
}
]
}
let say we want two pids
{"pid" : ObjectId("5762c0ba2b9a78006373a682")} and
{"pid" : ObjectId("5762c2892b9a78006373a686"),}
and only inner documents
so required response should look like:
{
"_id" : ObjectId("5762c0ba2b9a78006373a682"),
"name" : "aaa"
},{
"_id" : ObjectId("5762c2892b9a78006373a686"),
"name" : "ddd"
}
Aggregation framework can manipulate documents, match only needed ones and transform inner structure by project phase:
var match = {
$match : {
"projects.pid" : {
$in : [ObjectId("5762c0ba2b9a78006373a682"),
ObjectId("5762c2892b9a78006373a686")]
}
}
}
var unwind = {
$unwind : "$projects"
};
// now move array objet as top level object
var project = {
$project : {
_id : "$projects.pid",
name : "$projects.name",
// list other fields here
}
}
db.vic.aggregate([match, unwind, match, project])
var avg_temp = req.params.rating;
var destination = req.params.name;
var temp = { "results.name" : destination };
var wew = {$set: {"results.$.rating" :avg_temp} };
mach.update(temp ,wew , function(err,jop){
if(err)
{
res.status(500);
res.send('Destination not found');
}
else
{
res.status(202);
res.send('Rating is posted');
}
});
Sample document :
{
"_id" : ObjectId("56247581846ec164bb6be7bf"),
"results" : [
`enter code here`{
"name" : "Mk.Mechanics",
"ph" : NumberLong(9899991817),
"error" : false,
"lat" : 28.5833958899999980,
"lon" : 77.1687398899999980,
"rating" : 3,
"add" : "1123, Nanak pura, Delhi, 110021, India",
"local" : "Nanak pura",
"city" : "Delhi",
"contry" : "India"
},
{
"name" : "Pahadi",
"ph" : NumberLong(9876591817),
"error" : false,
"lat" : 28.6776553099999990,
"lon" : 77.2616931799999950,
"rating" : 3.5000000000000000,
"add" : "plot-6 saranshpur, Shastri park,Delhi,110018, India",
"local" : "saranshpur",
"city" : "Delhi",
"contry" : "India"
},
{
"name" : "mirza",
"ph" : 1.23123e+007,
"error" : false,
"lat" : 28.6775564800000000,
"lon" : 77.2621437899999960,
"rating" : 1,
"add" : " plopur, Shastri park, Delhi,110099,India",
"local" : "Shastri park",
"city" : "Delhi",
"contry" : "India"
}
] }
Heading
When i use static value in destination and avg_temp (eg. "Pahadi" and 12 respectively) this works but in this case it does not
Request is a patch request which I am making to the Node server :
localhost:8000\api\machs?name=Pahadi&rating=1
Any idea How to access JavaScript object in json
Output : Destination Not Found (Eventhough i've put exact string values in name params)
just put a console.log(err); above res.status(500);. So that we can know what the error is from DB.
Ahh Silly Mistake , Changed req.params to req.query as the values were sent by the http request in query string ! Problem solved , Now the code works as it should . Thank you guys for participating though :)
My mongoDB collection looks like this :
{
"_id" : ObjectId("5070310e0f3350482b00011d"),
"emails" : [
{
"_id" : ObjectId("5070310e0f3350482b000120"),
"_type" : "Email",
"name" : "work",
"email" : "peter.loescher#siemens.com",
"current" : true
}
]
}
and this is the .js code i use to print the contents :
c = db.contacts.findOne( { "emails.email" : { $ne : null } }, { "emails" : 1 } )
print(c._id.toString() + " " + c.emails[0]);
when I try to run this javascript file, it is just displaying the id but not the email array.
output:
5070310e0f3350482b00011d [object bson_object]
but when I try c.emails[0].email is is giving proper result. i.e. peter.loescher#siemens.com
All I need is I want to display the whole emails embedded object.
i.e.
"emails" : [
{
"_id" : ObjectId("5070310e0f3350482b000120"),
"_type" : "Email",
"name" : "work",
"email" : "peter.loescher#siemens.com",
"current" : true
}
]
Where I am going wrong?. Any help would be appreciated.
You need printjson to output a nicely formatted JSON:
printjson(c.emails[0]);
Here it is the documentation.