I have a json output like below,
{
"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},
"hits":{"total":2,"max_score":1.0,
"hits":
[
{
"_index":"management",
"_type":"",
"_id":"/home/myfld1/myid1",
"_score":1.0,
"_source" :
{
"newslides": "User Mgmt1 ",
"metaData":
{
"fileName": "file1",
}
}
},
{
"_index":"management",
"_type":"",
"_id":"/home/myfld3/myid3",
"_score":1.0,
"_source" :
{
"newslides": "User mgmt2 ",
"metaData":
{
"fileName": "file2",
}
}
}
]
}
}
I am trying to get the "fileName" field from the above json. I have tried like this,
for filenames in response["hits"]["hits"][0]["_source"]["newslides"]["metaData"]:
filearray.append(filenames["fileName"])
But i am getting one error like below,
for filenames in response["hits"]["hits"][0]["_source"]["newslides"]["metaData"]:
TypeError: string indices must be integers
Please help me to get that file name.
It is simply a dictionary with some lists and other dictionaries in it. Just be careful and go through the elements iteratively:
filearray = []
for x in response["hits"]["hits"]:
filearray.append(x["_source"]["metaData"]["fileName"])
or in one line:
filearray = [x["_source"]["metaData"]["fileName"] for x in response["hits"]["hits"]]
Related
I have a problem parsing a string object to JSON with Golang.
there are double quotes in some fields values.
I'm trying to use a regex pattern but it doesn't work for me, I'm doing the conversion from bigquery which is based on the Golang code but it doesn't work for me.
{
"Responses": [],
"SessionParameters(Updated)": {
"counter-no-match-nombre": "($session.params.counter-no-match, 1)",
"message": "Hello."
},
"SystemFunctionResults": {
"SystemFunctionErrors": [
{
"Message": "Wrong type of argument for function "NONE", expected 1th argument to be one of type: ["number"]."
}
]
}
}
here a example
([:[,{]\s*)"(.?)"(?=\s[:,]}])
Test regex
SELECT
REGEXP_REPLACE('{
"obj1": "value is "test" of test, \"obj1\" val",
"result": "your result is "out" in our website",
"info": {
"x": [
{
"desc": "shjjfdsajfjkfsdkf, "sfsdfsdfdsf" fjkdfgjkfd."
}
]
}
}','([:\\[,{]\\s*)"(.*?)"(?:\\s*[:,\\]}])','NONE')
AS json_data;
I would appreciate your help, thanks
I've a simple json array similar to the below. I'd like to find the record that contain a matching value, say for example name == jack.
{
"data": [
{
"Record": {
"attributes": {
"name": "Jack",
"age": "38",
"description": "1234",
}
}
}
]
}
Below python code works but it is very slow. Is there any way to get the results quicker?
with open('records.json') as f:
input_dict = json.load(f)
input_var = input_dict["data"]
for i in input_var:
if i["Record"]["attributes"]["name"] == "Jack":
print(i["Record"]["attributes"])
break
{
"responseCode": "200",
"data": {
"sequence": 1,
"used": true,
"sensingTags": [
{
"code": "LED",
"value": 1,
"updatedOn": 1587557350251
}
]
}
}
My goal is get updatedOn value from this json using jsonPath like this
1587557350251
i thought below jsonPath will work but it extract only empty list.
$..sensingTags[?(#.code == 'LED')][0].updatedOn
And i want to know how to extract value like below
{
"code": "LED",
"value": 1,
"updatedOn": 1587557350251
}
Not like this one.
[
{
"code" : "LED",
"value" : 1,
"updatedOn" : 1587557350251
}
]
As per Getting a single value from a JSON object using JSONPath, JsonPath will always return an array (or a false) at that point...
Best you can do is process it as an array of updatedOn and simply always grab the first value.
$..sensingTags[?(#.code == 'LED')].updatedOn
I have a JSON that I want to parse in Ruby. Ruby is completely new to me, but I have to work with it :-)
Here is my litte snippet, that should do the parsing:
response = File.read("app/helpers/example_announcement.json")
JSON.parse(response)
this works pretty fine. The only downside is, I do not know the properties at the point where I use it, it is not typesafe. So I created the objects for it
class Announcements
##announcements = Hash # a map key => value where key is string and value is type of Announcement
end
class Announcement
##name = ""
##status = ""
##rewards = Array
end
And this is how the json looks like
{
"announcements": {
"id1" : {
"name": "The Diamond Announcement",
"status": "published",
"reward": [
{
"id": "hardCurrency",
"amount": 100
}
]
},
"id2": {
"name": "The Normal Announcement",
"players": [],
"status": "published",
"reward": []
}
}
}
So I tried JSON parsing like this
response = File.read("app/helpers/example_announcement.json")
JSON.parse(response, Announcements)
But this is not how it works^^can anybody help me with this?
From the below JSON data, I want to cut out the attributes object and keep only Name of the Account. Sample JSON
{
"Accounts":[
{
"attributes":{
"type":"Account",
"url":"/services/data/v41.0/sobjects/Account/001S0000008mgjpIAA"
},
"Name":"Name+Test#Reseller"
},
{
"attributes":{
"type":"Account",
"url":"/services/data/v41.0/sobjects/Account/001S000000m5gyuIAA"
},
"Name":"Test Reseller Myself"
}
]
}
After matching with REGEX and replacing with "". The JSON should look like,
{
"Accounts" : [{
"Name" : "Name+Test#Reseller"
}, {
"Name" : "Test Reseller Myself"
}]
}
Use map and return only name property value
obj.accounts = obj.accounts.map( s => {Name: s.Name } );
I found myself an answer. I constructed a two regex
1. "attributes" : {\w*\W*\d*\D*\d*.\d*\D*\w*"\w*
2. {.\s*\S*