convert JSON from one key value pair to another - json

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)

Related

extract values from JSON using jq

I have a JSON object that looks like this:
{
"Accounts": [
{
"Id": "1",
"Name": "Joe",
"Zip": "11111"
},
{
"Id": "2",
"Name": "Jack",
"Zip": "22222"
}
]
}
I am trying to write a jq query that gives me this:
[
{
"Id": "1",
"Name": "Joe"
},
{
"Id": "2",
"Name": "Jack"
}
]
How can I do that? Thanks.
jq '.Accounts | map({ Id, Name })'
Will produce
[
{
"Id": "1",
"Name": "Joe"
},
{
"Id": "2",
"Name": "Jack"
}
]
as you can try online using this demo.
.Accounts selects the Accounts key
map() will apply the following for each object [docs]
Create object with Id and Name key [docs]
Demo https://jqplay.org/s/v01P2gDVc8
You can do
[.Accounts[] | {Id, Name}]

Retrieving Values of Json Data Inside JSON Array in Mysql

I have JSON column, containing the JSON array. My Scenario, is to get all the the records where value of url is
'"example.com/user1"' is present. I have trouble writing the query for this operation.
Record1
[
{
"id": "1",
"firstname": "user1",
"url": "example.com/user1"
},
{
"id": "2",
"firstname": "user2",
"url": "example.com/user2"
}
]
Record2
[
{
"id": "1",
"firstname": "user3",
"url": "example.com/user3"
},
{
"id": "2",
"firstname": "user2",
"url": "example.com/user2"
}
]
......
......
......
Record10
[
{
"id": "1",
"firstname": "user10",
"url": "example.com/user10"
},
{
"id": "2",
"firstname": "user1",
"url": "example.com/user1"
}
]
The Query Which I ran is:
Select internal_id from users_dummy where JSON_EXTRACT(user_friends, '$[0].url') = "example.com/user1" or JSON_EXTRACT(user_friends, '$[1].url') = "example.com/user1";
So o/p was:
Record1, Record10
Is this the proper way to search for the values across the records?
Thanks in advance.
You can use JSON_SEARCH like this:
SELECT *
FROM users_dummy
WHERE JSON_SEARCH(user_friends, 'one', 'example.com/user1', NULL, '$[*].url') IS NOT NULL
demo on dbfiddle.uk
You can use the following solution in case you are using objects instead of arrays:
SELECT *
FROM users_dummy
WHERE JSON_SEARCH(user_friends, 'one', 'example.com/user1', NULL, '$.*.url') IS NOT NULL
demo on dbfiddle.uk

lodash sort an array of objects by a property which has an array of objects

I have a an object. I am able to sort the items by using lodash's _.orderBy().
However, in one of the scenario I have to sort by subject, which is an array of objects. Items inside the subject array are already sorted based on the name.
As subject is an array of the objects, I need to consider the first item for sorting.
[
{
"id": "1",
"name": "peter",
"subject": [
{
"id": "1",
"name": "maths"
},
{
"id": "2",
"name": "social"
}
]
},
{
"id": "2",
"name": "david",
"subject": [
{
"id": "2",
"name": "physics"
},
{
"id": "3",
"name": "science"
}
]
},
{
"id": "3",
"name": "Justin",
"subject": [
]
}
]
You can use _.get() to extract the name (or id) of the 1st item in subjects. If no item exists, _.get() will return undefined, which can be replaced with a default value. In this case, we don't want to use an empty string as a default value, since the order would change. Instead I'm checking if the value is a string, if it is I use lower case on it, if not I return it as is.
const arr = [{"id":"1","name":"peter","subject":[{"id":"1","name":"maths"},{"id":"2","name":"social"}]},{"id":"2","name":"david","subject":[{"id":"2","name":"physics"},{"id":"3","name":"science"}]},{"id":"3","name":"Justin","subject":[]}]
const result = _.orderBy(arr, o => {
const name = _.get(o, 'subject[0].name')
return _.isString(name) ? name.toLowerCase() : name
})
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Use _.sortBy with a comparison/sorting function argument. Your function itself can look into the receiving arguments subject key (I think its the subject you want to compare?)
Since you have the question also tagged with ES6 here is an JS only solution via Array.sort:
let arr = [ { "id": "1", "name": "peter", "subject": [ { "id": "1", "name": "maths" }, { "id": "2", "name": "social" } ] }, { "id": "2", "name": "david", "subject": [ { "id": "2", "name": "physics" }, { "id": "3", "name": "science" } ] }, { "id": "3", "name": "Justin", "subject": [] }, ]
const result = arr.sort((a,b) =>
a.subject.length && b.subject.length
? a.subject[0].name.localeCompare(b.subject[0].name)
: a.subject.length ? -1 : 1)
console.log(result)

How can i remove key's from json doc array in nodeJS

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);

Validate JSON response with array values

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++;
}