How do you convert JSON Blob into Columns and Values - mysql

I have a VP/MS MySQL table with a "ref" column incorporating a unique identifier and a "request" column essentially incorporating a complete JSON response message in BLOB format. The message looks like this:
{
"vars": {
},
"computes": {
"P_PRERefId_str": {
"=": "12421321"
},
"P_PolicyNumber_str": {
"=": "1234567890"
},
"P_YearlyPolicyFeeAmt_rea": {
"=": "70.56"
}
}
}
Is there a way that I can somehow extract the JSON key-value pairs into 3 columns and corresponding values like:
P_PRERefId_str P_Policy P_YearlyPolicyFeeAmt_rea
12421321 1234567890 70.56

This might be what you are looking for. First, turn your JSON into a PHP object.
$myObject = json_decode('{
"vars": {},
"computes": {
"P_PRERefId_str": {
"=": "12421321"
},
"P_PolicyNumber_str": {
"=": "1234567890"
},
"P_YearlyPolicyFeeAmt_rea": {
"=": "70.56"
}
}
}');
Then, iterate through the "computes" attribute and insert the properties into your DB.
foreach ($myObject->computes as $column => $attr) {
foreach ($attr as $equalSign => $val) {
// INSERT $val INTO $column
// Depending on your Driver (PDO, framework ORM)
// Your code will differ here
}};

Related

How do I add values I got from forEach? (reactjs)

I'm trying to extract data from an API call that gives me this JSON:
JSON data:
{ "rows":
[
{
"customer_id": 1,
"customer_details":
{
"customer_id": 1,
"email":"john#mail.com",
"first_name":"John",
"last_name":"Doe",
"phone":"+123456123"
},
"order_items":[
{
"name": "random name",
"quantity":1
}
]
},
{
"customer_id": 2,
"customer_details":
{
"customer_id": 2,
"email":"johnny#mail.com",
"first_name":"Johnny",
"last_name":"Silverhand",
"phone":"+123456123"
},
"order_items":[
{
"name": "random name",
"quantity":1
},
{
"name": "another random name",
"quantity":1
}
]
}
]
}
I am actually able to get the the quantity but when there are two or more objects from the array in order_items like with customer 2, what I get is 11 how do I add the values I get? Sorry if this is really a simple problem but I am really confused.
here is my code for how I get them:
function getQuantity(qty) {
if(qty.length === 1) {
return qty[0].quantity;
} else {
let tempArr = [];
qty.forEach(prod => {
// console.log(prod.quantity)
tempArr.push(prod.quantity)
});
return tempArr;
}
}
I mapped used the function I made inside a map here is part of the code:
{orderList.map((val, key) => {
if (val.status_id !== 5) {
return (
<tr key={key} className='text-center'>
<td className='tdata'>{getQuantity(val.order_items)}</td>
Your getQuantity function returns an array of numbers (given this json data) and when you set the value of your td to be, as in your example, [1, 1] it will output "1""1" which will look like "11".
If you want to display the sum of your values, you could do something like this (although there is probably a better way to sum all the numbers in an array)
function getQuantity(qty) {
if(qty.length === 1) {
return qty[0].quantity;
} else {
let tempVal = 0;
qty.forEach(prod => {
tempVal += prod.quantity
});
return tempVal;
}
}

Create a new JSON after manipulating json values in angular6

Below is my JSON
[
{
"Key": "doc/1996-78/ERROR-doc-20200103.xlsx"
}
},
{
"Key": "doc/1996-78/SUCCESS-doc-20200103.xlsx"
},
{
"Key": "doc/1996-78/PENDING-doc-20200103.xlsx"
}
]
First i want to split key value by backslash and after that will split the [2] json value by hyphen and then will check in string that if there is SUCCESS/PENDING/ERROR word found in the newly spitted JSON. If any word is present would like to add new status field and add Done/Processing/Failure respective values in newly created JSON. this is a dynamic json so without manipulating it i can't get status value
This is what i would like to achive in my new JSON
[
{
"Key": "doc/1996-78/ERROR-doc-20200103.xlsx",
"status":"Failure"
}
},
{
"Key": "doc/1996-78/SUCCESS-doc-20200103.xlsx",
"Status":"Done"
},
{
"Key": "doc/1996-78/PENDING-doc-20200103.xlsx",
"Status":"Processing"
}
]
As i'm new to this kindly let me know how to achieve this
You can use includes function and if true then add string.
Try my code:
let myJson = [
{
"Key": "doc/1996-78/ERROR-doc-20200103.xlsx"
},
{
"Key": "doc/1996-78/SUCCESS-doc-20200103.xlsx"
},
{
"Key": "doc/1996-78/PENDING-doc-20200103.xlsx"
},
{
"Key": "doc/1996-78/WRONG-doc-20200103.xlsx"
}
];
myJson = myJson.map(obj => ({
...obj,
"Status": obj.Key.includes("ERROR") ? 'Failure' : obj.Key.includes('SUCCESS') ? 'Done' : obj.Key.includes('PENDING') ? 'Processing' : false
}))
console.log(myJson)
for(let object of objectArra) {
if(object.key === 'doc....')
object['status']="Failure";
else if (object.key === 'doc..')
object['status'] = "Done";
else if....
}
Iterate on object array if the key is equal to that you want status is "failure", you insert a status property in the object with "Failure" as value ...
This can be achieved in the following way
yourArrayName.forEach((val)=>{
if(val.Key.includes('ERROR')){
val['Status']="Failure"
}
else if(val.Key.includes('SUCCESS')){
val['Status']="Done"
}
else if(val.Key.includes('PENDING')){
val['Status']="Processing"
}
})
Hope it helps!
How to do it in ES 6
The key advantage is the use of Regex which makes it more flexible to other requirements than includes.
const data = [
{
"Key": "doc/1996-78/ERROR-doc-20200103.xlsx"
},
{
"Key": "doc/1996-78/SUCCESS-doc-20200103.xlsx"
},
{
"Key": "doc/1996-78/PENDING-doc-20200103.xlsx"
}
];
data.map((entry) => {
let status;
if (/^.*ERROR.*$/.test(entry.Key)) {
status = 'Failure';
} else if (/^.*SUCCESS.*$/.test(entry.Key)) {
status = 'Done'
} else if (/^.*PENDING.*$/.test(entry.Key)) {
status = 'Processing'
}
return {...entry, status};
});

MongoDB Split document field into two fields

I have a MongoDB document with over 2.8m documents of common passwords (hashed in SHA1) and their popularity.
Currently I've imported the documents with the following schema
{"_id":"5ded1a559015155eb8295f48","password":"20EABE5D64B0E216796E834F52D61FD0B70332FC:2512537"}
Although I'd like to split this so I can have the popularity value and it would look something like this
{"_id":"5ded1a559015155eb8295f48","password":"20EABE5D64B0E216796E834F52D61FD0B70332FC","popularity":2512537}
Question is im unsure how I can split the password into two password, popularity using : to split the string
You can use Aggregation Framework to split current password into two fields. You need to start with $indexOfBytes to get the position of : and then you need $substr to create new fields based on evaluated position.
db.collection.aggregate([
{
$addFields: {
colonPos: { $indexOfBytes: ["$password",":"] }
}
},
{
$addFields: {
password: { $substr: [ "$password", 0, "$colonPos" ] },
popularity: { $substr: [ "$password", "$colonPos", { $strLenBytes: "$password" } ] }
}
},
{
$project: {
colonPos: 0
}
}
])
Mongo Playground
As a last step you can use $out which takes all your aggregation results and writes them into new or existing collection.
EDIT: Alternative approach using $split (thank to #matthPen):
db.collection.aggregate([
{
$addFields: {
password: { $arrayElemAt: [ { "$split": [ "$password", ":"] }, 0 ] },
popularity: { $arrayElemAt: [ { "$split": [ "$password", ":"] }, 1 ] }
}
}
])
Mongo Playground

check for duplicate date from data and assign to new one json

i have a json which contain duplicate date , i want to merge duplicate date into single json object.
Data:
[
{"date":"2017-06-26","mac":"66"},
{"date":"2017-06-26","window":"400"},
{"date":"2017-07-03","mac":"19"},
{"date":"2017-07-03","window":"12"}
]
output should be:
[
{"date":"2017-06-26","mac":"66","window":"400"},
{"date":"2017-07-03","mac":"19","window":"12"}
]
Here a javascript function that does that, you can apply JSON.stringify(...) to the output after passing your array and obtain the new json.
(( a ) => {
// used to check for already inserted dates
let withoutDupes = { };
if(Array.isArray(a)) {
a.forEach( (item) => {
// assuming item has a "date" property inside
if(withoutDupes[item.date]) {
withoutDupes[item.date] = Object.assign( item, withoutDupes[item.date] );
} else {
withoutDupes[item.date] = item;
}
} );
}
return Object.values( withoutDupes );
})( a )
You can try using jq command line parser and its group_by function:
jq '[group_by(.date)|.[]|add]' file
[
{
"date": "2017-06-26",
"mac": "66",
"window": "400"
},
{
"date": "2017-07-03",
"mac": "19",
"window": "12"
}
]

how to read json file that can have different entries

I have json files where the various fields can change.
"eventHistory": [{
"occurredAt": "2018-03-17T10:40:05.707 0000",
"calluuid": "G8EMGR6EKD7DLDRP79FOEONVQ4000031",
"eventId": "2018-03-17T10:40:05.707Z_1521283205_G8EMGR6EKD7DLDRP79FOEONVQ4000031",
"event": "Data",
"data": {
"added": {
"OtherTrunkName": "sbc-trunk",
"OriginationDN": "1234",
"BusinessCall": "0",
"OriginationDN_location": "MNLSwitch"
}
}
}, {
"occurredAt": "2018-03-17T10:40:06.033 0000",
"calluuid": "G8EMGR6EKD7DLDRP79FOEONVQ4000031",
"eventId": "2018-03-17T10:40:06.033Z_1521283206_G8EMGR6EKD7DLDRP79FOEONVQ4000031",
"event": "Data",
"data": {
"added": {
"IW_CaseUid": "04d575ba-32e3-48da-8986-a19a6ff493b3",
"IW_BundleUid": "bf3ac19e-e2ea-4d7b-9b48-ef5e17dfdaa1"
}
}
}, {
"occurredAt": "2018-03-17T10:40:10.407 0000",
"calluuid": "G8EMGR6EKD7DLDRP79FOEONVQ4000031",
"eventId": "2018-03-17T10:40:10.407Z_1521283210_G8EMGR6EKD7DLDRP79FOEONVQ4000031",
"event": "Data",
"data": {
"added": {
"WrapUpTime": "0"
},
"deleted": {
"OriginationDN_location": "MNLSwitch",
"OriginationDN": "1234"
}
}
},
Is there an 'easy' way to simply read through and add these as variables to a js. I gather each field and then post to PHP which saves.
then I need to display in a tree... another question likely to appear on that one too.
Any help much appreciated.
It is unclear if you require the variables to be declared in a JS file, or if you simply want each key and value pair to be extracted out. Thus, I've provided 2 approaches that could be useful for your use case.
Suggestion: If it doesn't matter, why not handle the whole JSON object in your PHP save script?
1. Extract Key Value pairs
If you require a recursive JS function to get all the keys and values, you may use this function, referenced from here.
Caveat: This function ignores keys with nested key value pairs, so you will need to modify it to suit your needs.
var json = `
{
"occurredAt": "2018-03-17T10:40:06.033 0000",
"calluuid": "G8EMGR6EKD7DLDRP79FOEONVQ4000031",
"eventId": "2018-03-17T10:40:06.033Z_1521283206_G8EMGR6EKD7DLDRP79FOEONVQ4000031",
"event": "Data",
"data": {
"added": {
"IW_CaseUid": "04d575ba-32e3-48da-8986-a19a6ff493b3",
"IW_BundleUid": "bf3ac19e-e2ea-4d7b-9b48-ef5e17dfdaa1"
}
}
}
`
var obj = JSON.parse(json);
iterate(obj,'');
function iterate(obj, stack) {
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (typeof obj[property] == "object") {
iterate(obj[property], stack + '.' + property);
} else {
console.log(property + " " + obj[property]); //get key and value pair here
}
}
}
}
2. Declare Variables in JS
If you require variables to be declared in a JS <script> tag, you can consider using PHP to generate a HTML page with the variables declared.
The code below produces a html file with the following script tag:
Advantage: This approach allows you to get creative, and generate a lot of JS code that is variable specific.
Disadvantage: Debugging this could be hard.
Caveat: The example below is not recursive, so you'll have to do that on your own.
<?php
$string = <<<EOD
{
"occurredAt": "2018-03-17T10:40:06.033 0000",
"calluuid": "G8EMGR6EKD7DLDRP79FOEONVQ4000031",
"eventId": "2018-03-17T10:40:06.033Z_1521283206_G8EMGR6EKD7DLDRP79FOEONVQ4000031",
"event": "Data",
"data": {
"added": {
"IW_CaseUid": "04d575ba-32e3-48da-8986-a19a6ff493b3",
"IW_BundleUid": "bf3ac19e-e2ea-4d7b-9b48-ef5e17dfdaa1"
}
}
}
EOD;
$json = json_decode($string);
echo '<script>';
foreach($json as $key => $value)
{
$output = <<<EOD
var $key = "$value";
EOD;
echo $output;
}
echo '</script>';
?>