I am trying to convert an existing json string field to json array/object as I have recently moved data from mysql to mongodb.
{
"_id": {
"$oid": "63f241012a9551202e909257"
},
"title": "Jumanji: Welcome To The Jungle",
"description": "...",
...
"info": "[{\"year\": \"2017\", ... },{\"year\": \"2019\", ... }]",
...
}
I need this to be
{
"_id": {
"$oid": "63f241012a9551202e909257"
},
"title": "Jumanji: Welcome To The Jungle",
"description": "...",
...
"info": [{
"year": 2017,
...
}, {
"year": 2019,
...
}],
...
}
Here's one way to convert your JSON string by letting Javascript parse it.
db.movies.update({},
[
{
"$set": {
"info": {
"$function": {
"lang": "js",
"args": ["$info"],
"body": "function(infoStr) {return JSON.parse(infoStr)}"
}
}
}
}
],
{
"multi": true
})
Try it on mongoplayground.net.
Related
I am trying to fetch data from nested JSON, I need only a few fields from the JSON,
I have created case classes for the required data, the solution I found from google suggested to use read function, but I get an empty Object
I tried to google with no success, What I am missing?
my code
val rawDataFromFile = Source.fromFile(path).mkString
case class Data(listOfPersons: List[Person])
case class Person(bio: Bio, terms: List[Term])
case class Bio(birthday: String, gender: String)
case class Term(`type`: String, start: String, end: String)
read[Data](rawDataFromFile)
res >> Data(List())
and the JSON
[
{
"id": {
"not_intresting_field_1": "B000944",
"not_intresting_field_4": [
"H2OH13033",
"S6OH00163"
]
},
"name": {
"first": "first_name_1",
"last": "last_name_1"
},
"bio": {
"birthday": "1952-11-09",
"gender": "M"
},
"terms": [
{
"type": "rep",
"start": "1993-01-05",
"end": "1995-01-03"
},
{
"type": "rep",
"start": "1995-01-04",
"end": "1997-01-03"
}
]
},
{
"id": {
"not_intresting_field_1": "C000127",
"not_intresting_field_4": [
"S8WA00194",
"H2WA01054"
]
},
"name": {
"first": "first_name_1",
"last": "last_name_1"
},
"bio": {
"birthday": "1958-10-13",
"gender": "F"
},
"terms": [
{
"type": "rep",
"start": "1993-01-05",
"end": "1995-01-03"
},
{
"type": "sen",
"start": "2001-01-03",
"end": "2007-01-03"
}
]
}
]
Your case class is not the same as your json structure.
Here your define Data type which will read json like following
{
"listOfPersons": [
{
"id": {
"not_intresting_field_1": "B000944",
"not_intresting_field_4": [
"H2OH13033",
"S6OH00163"
]
},
"name": {
"first": "first_name_1",
"last": "last_name_1"
},
"bio": {
"birthday": "1952-11-09",
"gender": "M"
},
... //your original json
}
]
}
Try this
read[List[Person]](rawDataFromFile)
I was wondering if there is any 'cleaner' way of looping through nested JSON in ruby?
This is my JSON object:
{
"data": [
{
"file": "test/test_project_js/jquery.js",
"results": [
{
"vulnerabilities": [
{
"severity": "high"
},
{
"severity": "medium"
},
{
"severity": "none"
},
{
"severity": "high"
}
]
}
]
},
{
"file": "test/test_project_js/jquery.js",
"results": [
{
"vulnerabilities": [
{
"severity": "none"
},
{
"severity": "none"
},
{
"severity": "none"
},
{
"severity": "high"
}
]
}
]
}
]
}
I want to extract severity under each vulnerability present inside each results[] which is under data[]
Current code approach is
severity_arr = raw['data'].each do |data|
data['results'].each do |result|
result['vulnerabilities'].map {|vulnerability| vulnerability['severity']}
end
end
You can use flat_map and dig:
data[:data].flat_map { |datum| datum.dig(:results, 0, :vulnerabilities) }
# [{:severity=>"high"}, {:severity=>"medium"}, {:severity=>"none"}, {:severity=>"high"}, {:severity=>"none"}, {:severity=>"none"}, {:severity=>"none"}, {:severity=>"high"}]
What's maybe not convenient, is that data.results holds an array with a single hash. Maybe a hash is enough for that.
Input json -
{
"data": {
"Testresults": [
{
"mydata": {
"id": "111",
"uri": "url",
"type": "demo"
},
"name": "",
"address": "",
in side Parse Json schema added as below -
{
"properties": {
"data": {
"properties": {
"Testresults": {
"items": {
"properties": {
"name": {
"type": "string"
},
"address": {
"type": "string"
}
I have removed mydata from Parse Json schema still in the output getting same input json.
I want to remove mydata element from input json and pass it further , what should be done for that in parseJson?
expected output after Parse Json
{
"data": {
"Testresults": [
{
"name": "",
"address": "",
For your requirement, I post my steps below for your reference.
In my logic app, I use the json below as the input data:
{
"data": {
"Testresults": [
{
"mydata": {
"id": "111",
"uri": "url",
"type": "demo1"
},
"name": "Michael",
"address": "abc"
},
{
"mydata": {
"id": "222",
"uri": "url",
"type": "demo2"
},
"name": "Daniel",
"address": "def"
}
]
}
}
First I create a variable to store it.
Then, parse json.
After that, we can use liquid. We need to create an integration account and link to the logic app, then upload the liquid map shown as below:
{
"data": {
"Testresults": [
{% for testresult in content.data.Testresults %}
{
"name": "{{testresult.name}}",
"address": "{{testresult.address}}"
},
{% endfor %}
]
}
}
Now, we can use "Transform JSON to JSON" action to do convert it.
Run the logic app, I get the result we expect.(without "mydata")
Hope it would be helpful to your requirement~
I am attempting to use the Postman collection runner to post several objects to an API from a JSON file structured something like this:
[
{
"id": "170",
"body": {
"name": "prod1",
"category": "category1"
},
"anotherProperty": "xyz"
},
{
"id": "171",
"body": {
"name": "prod3",
"category": "category1"
},
"anotherProperty": "dfg"
},
{
"id": "172",
"body": {
"name": "prod3",
"category": "category1"
},
"anotherProperty": "abc"
}
]
My problem seems to be with the body since it is an object:
Here is what I have in the Body > raw application/json of the request that my collection is using:
{
"$id": "{{id}}",
"body": {{body}},
"anotherProperty": "{{anotherProperty}}"
}
When viewing what it is plugging in it looks like:
{
"id": "170",
"body": {[object Object]}, // instead of the actual object
"anotherProperty": "xyz"
}
I needed to add the following to the Pre-request script:
let properties = pm.iterationData.get('properties');
pm.variables.set('properties', JSON.stringify(properties));
let body = pm.iterationData.get('body');
pm.variables.set('body', JSON.stringify(body));
And the raw JSON is plugged in with no problem!
I've got an MVC 3 web app and am returning a JSON object which I would like to use Linq against to limit the result returned to the client jquery.
The Json response takes the following structure:
{
"Data": {
"Items": [
{
"Result": {
"Id": "e2ba4912-c387-4f54-b55e-06742a6858db",
"Name": "SomeOtherSetting",
"Value": "500",
"Archived": false
},
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting2",
"Value": "600",
"Archived": false
},
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting3",
"Value": "700",
"Archived": false
}
}]
}
}
....
I need to return or grab just the json items that have a Name like 'Setting' for example. In the example, this would return just the 2 Json nodes.
My Linq is very limited and what I have is: Settings is where the Json response is stored.
NewtonSoft.Json.Linq.JObject data = NewtonSoft.Json.Linq.JObject.Parse(Settings);
var result = from p in data["Data"]["Items"].Children()
where (p["Result"]["Name"].Contains("Expenses.Help.Tip"))
select new { Name = (string)p["Result"]["Name"], Value = (string)p["Result"]["Value"] };
When I run this I get nothing in my result. Can anyone help and tell me what I'm doing wrong?
Thanks.
Well, I'm not a Json specialist, but I think your Json structure has some problems.
I tried an online parser, and parsing took only the third result... (try to copy past your code in the left window, and look at JS eval.
Instead of
"Items": [
{
"Result": {
},
"Result": {
},
"Result": {
}
}]
you should have each element of Items array (each 'Result') into {}.
"Items": [
{
{"Result": {
}
},
{"Result": {
}
},
{"Result": {
}
}]
I got it to work by changing your Json file to
{
"Data": {
"Items": [
{
"Result": {
"Id": "e2ba4912-c387-4f54-b55e-06742a6858db",
"Name": "SomeOtherSetting",
"Value": "500",
"Archived": false
}
},
{
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting2",
"Value": "600",
"Archived": false
}
},
{
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting3",
"Value": "700",
"Archived": false
}
}]
}
}
using
var data = JObject.Parse(test)["Data"]["Items"].Children()
.Where(m => m["Result"]["Name"].Value<string>().Contains("Setting"))
.Select(m => new
{
Name = m["Result"]["Name"].Value<string>(),
Value = m["Result"]["Value"].Value<int>()
})
.ToList();