I have a multidimensional JSON data set that need multiple values replaced so that I can load my application to the web for some troubleshooting. The values need to be replaced because they are private records that may identify individuals' protected medical information. I suck at regex and have yet to be able to do this efficiently. What is want is to replace the following fields as described in the ():
PROV_NAME (Drlastname001, Drfirstname001 with # portion incrementing)
PAT_NAME (Patlname0001 , Patfname0001 with # portion incrementing)
PERS_ID, MRN, ENC_ID, FIN, ORD_ID (random # for each)
HOME_PHONE, CELL_PHONE (random ph #)
CMT_PRSNL (Stflastn0001 , Stffirstn0001 with # portion incrementing)
Below is a sample of the JSON dataset with the information manually replace. My real data set has multiple items for most of the arrays []. I was trying to do this in Sublime Text but once again I suck at regex. How can I do this quickly? PHP, JavaScript, within Sublime Text?
{
"TICKLER_ORDERED": {
"PROVIDER": [
{
"PROV_NAME": "Drlastname001, Drfirstname001",
"PERSON": [
{
"PERS_ID": 234213423,
"PAT_NAME": "Patlname0001 , Patfname0001",
"MRN": "45246",
"HOME_PHONE": "984-435-5673",
"CELL_PHONE": "745-547-6544",
"OLDEST_ORDER": "/Date(2012-01-03T13:34:28.000-04:00)/",
"ENCOUNTERS": [
{
"ENC_ID": 8854774,
"FIN": "78787457",
"ORDERS": [
{
"ORD_ID": 23423413,
"ORD_NAME": "MA Digital Diagnostic Mammo-Bil/CAD",
"ORD_TYPE": "Radiology",
"ORD_STATUS": "Canceled",
"ORD_PRIORITY": "Routine",
"ORD_DATE": "01/03/2012 13:34",
"DUE_DATE": "01/06/2012 13:00",
"COMMENTS": [
{
"CMT_DISP": "This is a test",
"SEQUENCE": "1",
"CMT_DTTM": "02/04/13 11:40",
"CMT_PRSNL": "Stflastn , Stffirstn"
},
{
"CMT_DISP": "This isn't a test",
"SEQUENCE": "2",
"CMT_DTTM": "02/04/13 11:42",
"CMT_PRSNL": "Stflastn0001 , Stffirstn0001"
}
]
},
{
"ORD_ID": 123234235,
"ORD_NAME": "US Breast Bilateral",
"ORD_TYPE": "Radiology",
"ORD_STATUS": "Canceled",
"ORD_PRIORITY": "Routine",
"ORD_DATE": "01/03/2012 13:34",
"DUE_DATE": "01/06/2012 14:20",
"COMMENTS": []
}
]
}
]
}
]
}
]
}
}
PHP provides great tools for iterating over an array of JSON data. As adeneo suggests, json_decode and json_encode will let you convert your data from JSON to a PHP multi-dimensional array, and back to JSON, respectively. Once you have your data in a PHP array, iterate over that array and use your knowledge of its structure, simple regular expressions (something like '/[0-9]{3}$/'), and counter variables to appropriately increase the indices.
Edited 6/2/13 for punctuation.
Related
Incoming JSON:
[
{
"name": "Name1"
},
{
"name": "Name2"
},
{
"name": "Name3",
"surname": "Surname3"
}
]
If we use JSONPath like $[:].name, we will receive:
[
0: "Name1",
1: "Name2",
2: "Name3"
]
But if will use the same to get surname ($[:].surname), we will receive:
[
0: "Surname3"
]
Is this possible to get surname values with empty string (or nulls) to keep right indexes? E.g.
[
0: "",
1: "",
2: "Surname3"
]
P.S.: at the moment I'm using this library.
JSON Path doesn't support returning placeholder values like that. It's a query language for JSON documents, much like SQL is for relational databases. Could you imagine if a SQL query returned placeholder values for every record in a database that didn't match your query?
I expect that the reason you want this is to determine where in the original document the value appears. To that end, JSON Path implementations should support returning the paths to the values instead of the values themselves:
[
"$[2]['surname']"
]
But I can't see in the README of that library where such a feature is supported (though it still might).
I am indexing multiple documents to Solr cloud with on query via API and json like this:
[
{
"id": "1",
"title": "Doc 1",
"author": "exmaple"
},
{
"id": "2",
"title": "Doc 2",
"author": "exmaple"
}
]
The documents are indexed, including all values, but some values are stored in an array in solr:
"response":{"numFound":2,"start":0,"maxScore":1.0,"docs":[
{
"id":"1",
"title":["Doc 1"],
"author":"exmaple",
"author_s":"exmaple",
"_version_":1631766743831543808},
{
"id":"2",
"title":["Doc 2"],
"author":"exmaple",
"author_s":"exmaple",
"_version_":1631766743831543808}]
}
Does anyone have an idea why in this case title is stored in an array?
You must have added the attribute as multivalued = true for the field Title.
This attribute is useful when there are more than one value present for particular field. If don't want to store the field in multivalue form, remove the attribute for the same field.
Remove the same and restart the server.
Re-index the data.
I am getting json array after getting applying query logic.
[
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-20"
},
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "CRP",
"booking_date": "2018-05-20"
},
{
"id": "4",
"diag_name": "Seepz Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-21"
}
]
But i want a more justified json array written below.
[
{
"diag_name": "LT Diagnostics",
"test_name": [
{
"id": "3",
"name" : "Alk PO4"
},
{
"id": "3",
"name" : "CRP"
}
],
"booking_date": "2018-05-20"
},
{
"diag_name": "Seepz Diagnostics",
"test_name": [
{
"id": "4",
"name" : "Alk PO4"
}
],
"booking_date": "2018-05-21"
},
]
I am not getting it,How to do in php. I want a more consolidate json format.
Have you tried changing your SQL query to group by diag_name and booking_date? That would be the first step I’d employ to get the outer data.
Formatting the data in the nested manner you’re after could be a function of whatever record serializer you’re using — does it support nested JSON as a return type, or only flat JSON as your example return value shows?
If the record set -> JSON serializer only ever returns flat data, the comments above are correct that you will have to write your own formatter to change the shape of the JSON yourself...
The accepted answer of this other question may be of help:
Create multi-level JSON with PHP and MySQL
I'm not a PHP guy but this is a typical scenario to use functional programming by means of the monad Map.
Looking online I've found this article that could help you.
Changing datasource output is not always (seldom indeed) a viable option.
Enjoy coding
Hi i'm really mongodb newbie.
I have a document like this:
{
"_id": ObjectId("53182e32e4b0feedb1dea751"),
"solutions": [
[
{
"solution": "Double Room Economy (Without Breakfast)",
"board": "Room Only",
"id": "HK-15501871",
"price": 5000,
"available": "1",
"CXL": "[]",
"unique": 0
},
{
"solution": "Double Room Economy (With Breakfast)",
"board": "Room Only",
"id": "HK-15501871",
"price": 4600,
"available": "1",
"CXL": "[]",
"unique": 1
},
{
"solution": "Double Room Economy (Room Only)",
"board": "Room Only",
"id": "HK-15501871",
"price": 5500,
"available": "1",
"CXL": "[]",
"unique": 2
}
]
]
}
And i need to update the field CXL inside the second array of solutions.
so solutions.1.CXL
This is how i take document:
$collection = $this->getCollection();
$query = array("_id"=>new MongoId($id));
$document = $collection->findOne($query);
now i need to update that field without touch the other.
How can i do?
Thanks!
SOLVED THANKS TO #Sammaye
i solved in this way:
$collection->update(
array('_id' => new MongoId('..')),
array('$set' => array('solutions.0.1.CXL' => 'something'))
);
Edit
To actually update by the first index then you can do:
$db->collection->update(
['_id' => new \MongoId($id)],
['$set' => ['solutions.0.1.CLX' => 'whatever']]
);
I misread the question in posting the information below:
So what you wanna update all CXL fields in the document (since you are only searching by top level document _id)?
That isn't possible without manually pulling this document out and iterating the subdocuments in the solutions field and then resaving it.
This is becausde there is currently no way of saying, "Update all that match"
This, however, is most likely the JIRA you would want to look for: https://jira.mongodb.org/browse/SERVER-1243
As long as you know you are going to update the second element then use the index of the array to do so. But that problem next. First you need the $set operator in order not to blow away your document and just set the field value:
db.collection.update(
{ _id: ObjectId("53182e32e4b0feedb1dea751") },
{ $set: { "solutions.0.1.CXL": [ 1, 2, 3 ] } }
)
If you just want to add to the array rather than replace the whole thing, then just use $push instead:
db.collection.update(
{ _id: ObjectId("53182e32e4b0feedb1dea751") },
{ $push: { "solutions.0.1.CXL": 4 } }
)
If you are paying attention to the notation, then you will notice that the array index values are present in the field to be updated. There is a very good reason for this, which can be read on the documentation for the positional $ operator.
The issue is that you have a nested array, which as the documentation refers to, causes a problem if you try to match items within that nested array. That problem is, if you try to use the "positional" operator to find the matched index of something you look for in a query, then it will contain the value of the first array index match that it finds.
In this case that would be your "top level" array and the "found" index is 0 and not 1 as you may expect.
Please be aware of this issue if you intend to use nested arrays.
You can update like this:
update({
_id: ObjectId("53182e32e4b0feedb1dea751"),
solutions.id: HK-15501871,
solutions.CLX: "Whatever!",")
},{
$set: {"comments.$.type": abc}
}, false, true
);
You may want to go through this once
http://docs.mongodb.org/manual/reference/method/db.collection.update/
I've been trying to figure out how to reformat multiple JSON files into a single one using php, but am having a difficult time understanding how to use complex regular expressions. Suppose I hade multiple instances of the following JSON data:
{
"felines": {
"cats": [
{
"age": 7,
"name": "frank"
},
{
"age": 4,
"name": "popeye"
}
]
},
"canines": {
"dogs": [
{
"age": 2,
"name": "lucy"
},
{
"age": 12,
"name": "wilson"
}
]
}
}
Lets say I had 2 instances of this JSON object in a php script, and wanted to create a single JSON object that combined both "feline" objects from the two separate JSON instances I had, removing the "canines" objects. The file I'd ultimately want would look like this:
{
"felines": {
"cats": [
{
"age": 7,
"name": "frank"
},
{
"age": 4,
"name": "popeye"
}
]
},
"felines": {
"cats": [
{
"age": 6,
"name": "sam"
},
{
"age": 4,
"name": "kelly"
}
]
}
}
Does anyone know how i might be able splice and combine these JSON objects with regular expressions using php?
Thanks.
why don't you use json_encode & json_decode to do the works on php arrays seems to be a lot more easy then doing that with regular expressions.
I doubt this is a problem you should try to solve with regexes. Consider converting the JSON files to associative arrays, do your merging, and then change back to JSON.
Regular expressions are, in general, really bad at dealing with arbitrarily nested contexts like JSON data, HTML tags, programming languages, etc. Some extended regular expression libraries patch around those deficiencies.
But, really, is there a reason you need to do this in JSON itself? And with regex? You're probably going to have a much easier time deserializing the data to real PHP data structures, and merging/manipulating things there. Then, when you're done, re-serialize the result.
The best way to do it would be as RageZ suggested, using json_encode and json_decode, however JSON doesn't allow you to have the same key name, does it? The best you can get would be this:
{
"felines": {
"cats": [
{
"age": 7,
"name": "frank"
},
{
"age": 4,
"name": "popeye"
},
{
"age": 6,
"name": "sam"
},
{
"age": 4,
"name": "kelly"
}
]
}
}