Is there a quick and stable way to remove all a key value pair from a json Doc array. In my case i have data returned from my DB which contains more fields then i want to show to user so i want to query my db and see what key's he is supposed to get before returning the json to client.
In this sample the data has 3 keys, FirstName,LastName and dob how would i go to remove all dob Key and values from the json, also if i have to remove more then one Key Value pair does it make difference when you do it ?
{
"result":[
{
"FirstName": "Test1",
"LastName": "User",
"dob": "01/01/2011"
},
{
"FirstName": "user",
"LastName": "user",
"dob": "01/01/2017"
},
{
"FirstName": "Ropbert",
"LastName": "Jones",
"dob": "01/01/2001"
},
{
"FirstName": "hitesh",
"LastName": "prajapti",
"dob": "01/01/2010"
}
]
}
You can use the delete operator on the obj while looping through your data.
let data = {
"result": [{
"FirstName": "Test1",
"LastName": "User",
"dob": "01/01/2011"
},
{
"FirstName": "user",
"LastName": "user",
"dob": "01/01/2017"
},
{
"FirstName": "Ropbert",
"LastName": "Jones",
"dob": "01/01/2001"
},
{
"FirstName": "hitesh",
"LastName": "prajapti",
"dob": "01/01/2010"
}
]
}
// #param keys: an array of keys to remove
function removeKeyValue(obj, keys) {
obj.forEach(currObj => {
keys.forEach(key => {
delete currObj[key];
});
});
}
removeKeyValue(data.result, ["dob", "LastName"]);
console.log(data.result);
Related
I want to generate new JSON from the existing, but I am not able to do so.
Given JSON:
[
{
"id": "1",
"firstName": "john",
"lastName": "doe"
},
{
"id": "2",
"firstName": "michal",
"lastName": "foo"
},
{
"id": "3",
"firstName": "john",
"lastName": "smith"
}
]
want to convert it to:
[
{
"id": "1",
"firstName": "john-doe"
},
{
"id": "2",
"firstName": "michal"
},
{
"id": "3",
"name": "john-smith"
}
]
I want only id name, but want lastName when only the firstName is same.
So, I am not able to do this condition of merging name, and add surname only when firstName is same
Please try to mention the following in your question:
Technologies you are using
What you are trying to do/build
Code you have already tried to write
Errors (if any)
I have a json that comes from InvokeHTTP. I did a Split Json and JoltTransform to get the Key, Values, but I need to change all the Keys from Camelcase to snakecase.
My Keys will be different with every InvokeHttp call. I've tried AttributestoJson and EvaluateJsonPath and some replace text, but haven't figured out a way to dynamically change just the keys and then merge back to the values without writing a custom processor.
Original Data from InvokeHTTP:
{
"data": {
"Table": [
{
"Age": 51,
"FirstName": "Bob",
"LastName": "Doe"
},
{
"Age": 26,
"FirstName": "Ryan",
"LastName": "Doe"
}
]
}
}
Input after Split Json (Gives me each json in a separate flowfile) and Jolt:
[
{
"Key": "Age",
"Value": 51
},
{
"Key": "FirstName",
"Value": "Bob"
},
{
"Key": "LastName",
"Value": "Doe"
}
]
Desired Output:
{
"data": {
"Table": [
{
"age": 51,
"first_name": "Bob",
"last_name": "Doe"
},
{
"age": 26,
"first_name": "Ryan",
"last_name": "Doe"
}
]
}
}
If you know the fields, you can use JoltTransformJSON on the original input JSON so you don't have to use SplitJson, here's a spec that will do the (explicit) field name conversion:
[
{
"operation": "shift",
"spec": {
"data": {
"Table": {
"*": {
"Age": "data.Table[#2].age",
"FirstName": "data.Table[#2].first_name",
"LastName": "data.Table[#2].last_name"
}
}
}
}
}
]
You could also use UpdateRecord, you'd just need separate schemas for the JsonTreeReader and JsonRecordSetWriter.
I wrote an answer to a similar question which uses ReplaceText to replace . in JSON keys with _. The same logic could be applied here (template available in link). As I pointed out in that answer, a cleaner solution would be to use ExecuteScript, especially as the transformation from camelcase to snakecase is done easily in most scripting languages.
I have a JSON response as shown below (in array values). I want to validate whether the Keys (attributes) are properly retrieved in the Response. I would like to compare a list of expected values against the response values (only the first set of array values)
Eg:
"participants": [
{
"FirstName": "Kim",
"LastName": "Hykes",
"Street1": "ABC",
"Street2": "ABCD",
"City": "city1",
"State": "NJ"
}
{
"FirstName": "John",
"LastName": "David",
"Street1": "XYZ",
"Street2": "UXYZ",
"City": "city2",
"State": "NY"
}
]
Using JSONparser, the above JSON is parsed and the result will be:
participants[0].FirstName, participants[0].LastName,
participants[0].Street1, participants[0].Street2, participants[0].City,
participants[0].State, participants[1].FirstName,
participants[1].LastName,
participants[1].Street1, participants[1].Street2, participants[1].City,
participants[1].State
And it goes on until 50 participants
I would like to see whether all the required Keys are fetched from the database and would like to check only the first Array fields i.e. only in Participants[0].
Can anyone help me to figure the solution here?
Do you mean something like this, to validate whether the keys are in there, and in the correct order?
var response = {"participants": [
{
"FirstName": "Kim",
"LastName": "Hykes",
"Street1": "ABC",
"Street2": "ABCD",
"City": "city1",
"State": "NJ"
},
{
"FirstName": "John",
"LastName": "David",
"Street1": "XYZ",
"Street2": "UXYZ",
"City": "city2",
"State": "NY"
}
]
}
var keysNeeded = ['FirstName', 'LastName', 'Street1', 'Street2', 'City', 'State']
var i = 0;
for (key in response.participants[0]) {
if (key == keysNeeded[i]) {
console.log('all is OK with key '+key)
}
i++;
}
I have a json object which has the following form
"data": [{
"firstName": "XYZ",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo"
}, {
"firstName": "ABC",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo1"
}, {
"firstName": "EFG",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo2"
}]
I want the following output to concat the logo name with its path using lodash in nodejs
"data": [{
"firstName": "XYZ",
"lastName": "Admin",
"userId": 1,
"companyLogo":"E:/Logo"
}, {
"firstName": "ABC",
"lastName": "Admin",
"userId": 1,
"companyLogo":"E:/Logo1"
}, {
"firstName": "EFG",
"lastName": "Admin",
"userId": 1,
"companyLogo":"E:/Logo2"
}]
Is there any function existing which supports this type of concatenation?
There's no need for an external library:
var data = [
{
"firstName": "XYZ",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo"
},
{
"firstName": "ABC",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo1"
},
{
"firstName": "EFG",
"lastName": "Admin",
"userId": 1,
"companyLogo":"Logo2"
}
]
var new_data = data.map(function(elem){ elem.companyLogo = "E: /" + elem.companyLogo; return elem });
console.log(new_data);
I am getting this result when I am using graph api . it is in array format
{
"id": "216805086",
"name": "raj sharma",
"first_name": "raj ",
"last_name": "sharma",
"link": "https://www.facebook.com/raj.sharma.5",
"username": "raj .sharma.5",
"favorite_teams": [
{
"id": "198358615428",
"name": "Mumbai Indians"
},
{
"id": "190313434323691",
"name": "Indian Cricket Team"
}
],
"favorite_athletes": [
{
"id": "100787839962234",
"name": "Saina Nehwal"
}
],
"gender": "male",
"email": "raj.discoverme#gmail.com",
"timezone": 5.5,
"locale": "en_GB",
"verified": true,
"updated_time": "2013-08-13T06:01:17+0000"
}
I am working in a php language and phpmyadmin database . Now i want to insert the array into my database . Should i make a column for id , name , first_name ,last_name,link,favorite_teams etc or should i make a one column for all of this ........
how toinsert tha array into the database
Actually this is not an array. This is JSON. In JSON there are two formats,
JSONArray [ ]
JSONObject { }
You are getting the JSONObject as your output. There is a function in PHP callerd JSONDecode.
Go through this you will get idea.
Storing facebook app data in a database is against Facebook policy http://developers.facebook.com/policy/
$data = '{
"id": "216805086",
"name": "raj sharma",
"first_name": "raj ",
"last_name": "sharma",
"link": "https://www.facebook.com/raj.sharma.5",
"username": "raj .sharma.5",
"favorite_teams": [
{
"id": "198358615428",
"name": "Mumbai Indians"
},
{
"id": "190313434323691",
"name": "Indian Cricket Team"
}
],
"favorite_athletes": [
{
"id": "100787839962234",
"name": "Saina Nehwal"
}
],
"gender": "male",
"email": "raj.discoverme#gmail.com",
"timezone": 5.5,
"locale": "en_GB",
"verified": true,
"updated_time": "2013-08-13T06:01:17+0000"
}';
//decode to get as php variable
$values = json_decode($data,true); //true to decode as a array not an object
$sql = "INSERT INTO TableName (id,name,first_name,last_name,link,username)
VALUES ('".$values['id']."','".$values['name']."','".$values['first_name']."','".$values['last_name']."','".$values['link']."','".$values['username']."')";
mysql_query($sql);
Json_decode() takes a JSON encoded string and converts it into a PHP variable.