Can I delete one item in JSON by jsonpath use golang - go

I have a json like this:
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
And get a json path expression: $.phoneNumbers.*.type
How can I delete the keys by this expression in Go?

Related

Extract the id by validating the child element from the JSON response

For the following response as a form of JSON, I want to extract a particular value on the validating first name.
{
"system": false,
"redirect": false,
"url": "user",
"infinite": false,
"csrf": {
"name": "csrf_token",
"value": "1fc51ba92c87ad1f086195e36a4a6b02"
},
"version": "2.0.1869",
"view": {
"data": {
"items": [
{
"id": null,
"name": "No Workspace",
"users": [
{
"id": "170303",
"account_id": "143003",
"email": "Perfuser554#gmail.com",
"first_name": "Perf-WxC",
"last_name": "Eve-971"
},
{
"id": "170318",
"account_id": "143003",
"user_type_id": "3",
"email": "Perfuser555#gmail.com",
"password_format": "1",
"first_name": "Perf-WMV",
"last_name": "Eve-972"
},
{
"id": "170320",
"account_id": "143003",
"email": "Perfuser556#gmail.com",
"first_name": "Perf-YTC",
"last_name": "Eve-973"
},
{
"id": "170321",
"account_id": "143003",
"email": "Perfuser557#gmail.com",
"first_name": "Perf-ZvF",
"last_name": "Eve-974"
},
{
"id": "170322",
"account_id": "143003",
"email": "Perfuser558#gmail.com",
"first_name": "Perf-WmO",
"last_name": "Eve-975"
}
],
"company_name": "Local host"
}
]
},
"show_nav": true
}
}
I want to extract the id and account_id for a particular user on the basis of first name. For example for Perf-YTC the id is 170320 and account_id is 143003.
Could use the JSON extractor, but dont know the index value mentioned in the image.
You could use the following as a JSON extractor,
$..[?(#.first_name == "Perf-YTC")].id for id and
$..[?(#.first_name == "Perf-YTC")].account_id for account_id

Jolt Specification

I have the following input format
INPUT.JSON
[
{
"name": "adam",
"age": 12,
"address": {
"city": "delhi",
"country": "india",
"zip": "110011"
}
},
{
"name": "louis",
"age": 23,
"address": {
"city": "goa",
"country": "india",
"zip": "110022"
}
}
]
After applying jolt transformation i want to get the following output
DESIRED OUTPUT.JSON
[
{
"name": "adam",
"age": 12,
"address": {
"current_city": "delhi", //change here
"current_country": "india", //change here
"zipode": "110011" //change here
}
},
{
"name": "louis",
"age": 23,
"address": {
"current_city": "goa", //change here
"current_country": "india", //change here
"zipode": "110022" //change here
}
}
]
Can you please help me with the jolt spec.
thanks
This might help,
[
{
"operation": "shift",
"spec": {
"*": {
"name": "[&1].name",
"age": "[&1].age",
"address": {
// Shifting inside address object
"city": "[&2].address.current_city",
"country": "[&2].address.current_country",
"zip": "[&2].address.zipode"
}
}
}
}
]

Update property in array in RethinkDB

I have the following structure in RethinkDB
{ "id": "1", "values": [{ "id": 1, "firstname": "foo" }] },
{ "id": "2", "values": [{ "id": 2, "firstname": "bar" }] }
How can I update each "values" array and add an additional property to it?
For instance I'd like to achieve
{ "id": "1", "values": [{ "id": 1, "firstname": "foo", "lastname": null }] },
{ "id": "2", "values": [{ "id": 2, "firstname": "bar", "lastname": null }] }
Something like:
r.table('test').get(id).update(function(row) {
return {values: row('values').map(function(val)
return val.merge({lastname: null});
});
})

Get particular key's and value's from JSON array - Ruby [duplicate]

This question already has answers here:
Ruby: Easiest Way to Filter Hash Keys?
(14 answers)
Closed 6 years ago.
I am a beginner with Ruby, and I have the following Json array:
"elements": [
{
"type": "Contact",
"id": "1",
"createdAt": "131231235",
"name": "test",
"updatedAt": "1456328049",
"accountName": "Mr Test",
"country": "China",
"firstName": "Test",
"lastName": "lastNameTest",
},
{
"type": "Contact",
"id": "2",
"createdAt": "156453447",
"name": "test2",
"updatedAt": "124464554",
"accountName": "Mr Test2",
"country": "Germany",
"firstName": "Test2",
"lastName": "lastNameTest2",
},...
]
I want to filter out only a few keys + values: for example I want to return only the id,name,accountName,firstname and lastname.
So the exspected output is the following:
"elements": [
{
"id": "1",
"name": "test",
"accountName": "Mr Test",
"firstName": "Test",
"lastName": "lastNameTest",
},
{
"id": "2",
"name": "test2",
"accountName": "Mr Test2",
"firstName": "Test2",
"lastName": "lastNameTest2",
},...
]
I tried the following: create a filter array which has the elements I want to return and then map over the items but then I get stuck..
filters = []
filters.push("accountName")
filters.push("lastName")
filters.push("firstName")
filters.push("Id")
output["elements"].each do |item|
result = []
item.map {|key,value|filters.include? key}
result.push(?)
Thank you for the help.
Check this out, you should be able to work out from this:
output = { "elements": [
{
"id": "1",
"name": "test",
"accountName": "Mr Test",
"firstName": "Test",
"lastName": "lastNameTest",
"somethoong": "sdsad"
},
{
"id": "2",
"name": "test2",
"accountName": "Mr Test2",
"firstName": "Test2",
"lastName": "lastNameTest2"
}
]}
attribs = %w(accountName lastName firstName id)
output[:elements].each do |item|
item.delete_if{|k,v| !attribs.include?(k.to_s)}
end

add score to elasticsearch completion suggester inputs

I need to implement elasticsearch completion suggester.
I have an index mapped like this:
{
"user": {
"properties": {
"username": {
"index": "not_analyzed",
"analyzer": "simple",
"type": "string"
},
"email": {
"index": "not_analyzed",
"analyzer": "simple",
"type": "string"
},
"name": {
"index": "not_analyzed",
"analyzer": "simple",
"type": "string"
},
"name_suggest": {
"payloads": true,
"type": "completion"
}
}
}
}
I add documents to the index like this:
{
"doc": {
"id": 1,
"username": "jack",
"name": "Jack Nicholson",
"email": "nick#myemail.com",
"name_suggest": {
"input": [
"jack",
"Jack Nicholson",
"nick#myemail.com"
],
"payload": {
"id": 1,
"name": "Jack Nicholson",
"username": "jack",
"email": "nick#myemail.com"
},
"output": "Jack Nicholson (jack) - nick#myemail.com"
}
},
"doc_as_upsert": true
}
And I send this request to my_index/_suggest:
{
"user": {
"text": "jack",
"completion": {
"field": "name_suggest"
}
}
}
I get the resulting options that look like this:
[
{
"text": "John Smith",
"score": 1.0,
"payload": {
"id": 11,
"name": "John Smith",
"username": "jack",
"email": "john#myemail.com"
}
},
{
"text": "Jack Nickolson",
"score": 1.0,
"payload": {
"id": 1,
"name": "Jack Nickolson",
"username": "jack.n",
"email": "nickolson#myemail.com"
}
},
{
"text": "Jackson Jermaine",
"score": 1.0,
"payload": {
"id": 10,
"name": "Jackson Jermaine",
"username": "jermaine",
"email": "jermaine#myemail.com"
}
},
{
"text": "Tito Jackson",
"score": 1.0,
"payload": {
"id": 9,
"name": "Tito Jackson",
"username": "tito",
"email": "jackson#myemail.com"
}
},
{
"text": "Michael Jackson",
"score": 1.0,
"payload": {
"id": 6,
"name": "Michael Jackson",
"username": "michael_jackson",
"email": "jackson_michael#myemail.com"
}
}
]
This works fine but, I need to have the options sorted that way that those that have username matched come first. I can do it manually, but that would prevent me to use length and offset and would be slower.
Is it possible to add scoring to the individual inputs (not the whole suggests), and that way affect the sorting? With the approach that I use it seems it is not.
Another related question, is it possible to specify in the input an array of fields instead of an array of values, and that way avoid the duplication? If yes, would setting the score on the fields be taken into account when ES generates suggestions?
You can add score to your input with the weight option.
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#indexing

Resources