I'm getting a Json object which contains other json object as properties .
[
{
"prop": "\{"p" : "1\"}"
}
]
I'm parsing it this way :
this.Auth.process().subscribe((x: any[]) => {
console.log(JSON.parse(x[0].prop)); /// Works
console.log('res: ' + JSON.parse(x[0].prop).p); /// Undefined.
});
when i try to retreive the json property i get undefined as result .
JSON.parse(x[0].prop).p is undefined because value of x[0].prop is string and not a JSON.
You can parse x[0] first and then fetch the value of p from the derived object -
JSON.parse(x)[0].prop
Your JSON is invalid. Try removing the quotes like this example.
[
{
"prop": {"p" : "1"}
}
]
Related
I'm trying to extract a value from JSON to a flowfile-attribute. When I run the EvaluateJsonPath processor I get an error stating
"Unable to get a scalar value for expression $..fields.storyBoard.stringValue.
Input JSON looks like this:
{
"name" : "projects/fakedims-0000/databases/(default)/documents/device/0000",
"fields" : {
"reportKey" : {
"stringValue" : "abc123"
},
"dateOccured" : {
"timestampValue" : "2018-10-14T04:00:00Z"
},
"storyBoard" : {
"stringValue" : "https://path/to/media"
},
"new" : {
"integerValue" : "25"
},
"name" : {
"stringValue" : "device one"
},
"location" : {
"geoPointValue" : {
"latitude" : -78.413751,
"longitude" : 38.156487
}
}
},
"createTime" : "2018-10-19T00:02:26.209335Z",
"updateTime" : "2018-10-19T22:22:24.382136Z"
}
The JSONPath expression is $..fields.storyBoard.stringValue
What I think is happening is that the processor is returning ["https://path/to/media"] rather than just the string.
This is what I get if a evaluate to flowfile-content rather than an attribute. Why? What can I do to fix it?
Change the Return Type property value to json in EvaluateJsonPath processor, if you are extracting as flowfile-attribute
Return Type property description:
Indicates the desired return type of the JSON Path expressions.
Selecting 'auto-detect' will set the return type to 'json' for a
Destination of 'flowfile-content', and 'scalar' for a Destination of
'flowfile-attribute'.
As you are trying to extract nested key not the key on the root level(ex:name,createTime..), that's the reason why we need to configure the Return Type as Json not as scalar.
In Addition you can use FlattenJson processor(seperator '_') to flatten-out all nested json then use Return Type as auto detect in EvaluateJsonPath processor.
EvaluateJsonConfigs:
Output:
We are going to have attribute value without enclosing in an array
Here is my sample JSON data:
var testData =
{
"Level1": [
{
"Level2": [
{
"Level3": [
{
"body": "AAAAA"
},
{
"body": "BBBBB"
}
]
}
]
}
]
};
When I use JSON.stringify like this:
var x = JSON.stringify(testData).replace(/[\[\]]/g,"");
console.log(x);
It works as expected and correctly replaces the square brackets and returns this result:
{"Level1":{"Level2":{"Level3":{"body":"AAAAA"},{"body":"BBBBB"}}}}
The error occurs when I try to add JSON.parse like this which returns an error:
var x = JSON.parse(JSON.stringify(testData).replace(/[\[\]]/g,""));
The specific error is SyntaxError: Unexpected token { in JSON. What seems to be happening is that JSON.parse is treating the comma inside the key/value list as the end of the JSON string, when it is not the end.
Does anyone have any idea why this is happening?
{"Level1":{"Level2":{"Level3":{"body":"AAAAA"},{"body":"BBBBB"}}}}
This is not valid JSON
The level 3 should be:
"Level3":[{"body":"AAAAA"}, {"body":"BBBBB"}]
For you first levels, you have arrays with only 1 element, so the array brackets [] kan be removed without consequence. The level 3 is an actual array with 2 elements, so removing the [] breaks your valid JSON syntax.
So I have Json obj which I convert via JSON.stringfy and the result is:
{
"data" : [ {
"id" : 417206355511802,
"name" : "test01"
}, {
"id" : 421211003974634,
"name" : "test02"
}, {
"id" : 403713622404901,
"name" : "test03"
}]
}
How can I access each name value? I was trying:
var test = result[0].name;
alert(test);
You can't access anything from the result of stringify() - it produces a string, hence its name. Rather, it sounds like you started with a string and converted it to an object via JSON.parse().
If that's not the case, and you already have the object, you don't need to stringify if you want to access properties.
That out of the way, you're missing the data step.
myobj.data[0].name; //<-- where myobj is the variable holding the object
JSON.stringify will not help you.becouse it's give a string as output.you can directly access the object elements by
var arr = myObj.data;
arr.forEach(function(elem) {
console.log(elem.id);
console.log(elem.name);
});
I come from a Python Background and recently started programming using TypeScript and Angular2. I want to know how to obtain keys from a JSON object using TypeScript.
I have a response like so:
response.text()
I pass this object to a function
removeMetaData (my_data: string){
//(Various manipulation)...etc
}
i have read that I can call a json() method instead of text(). If I do that, what is the type I should use for my_data?
Then,
If my JSON looks like this:
{
"count": 100,
"next_page": "http://www.test.com/users/?page=2",
"prev_page": "http://www.test.com/users/?page=3",
"results":[
{
"username": "johnny"
},
Etc....
]
How do I parse that?
I've read I might have to use an interface but I don't understand how.
In python it's just response["next_page"] to get the key of a dictionary, then I can assign that value to a variable within the class. That is exactly what I'm trying to achieve within a component.
Thank you.
ADDITION
list() {
this.requestService.get(this.api_path)
.subscribe(
response => this.populate(response.json()),
error => this.response = error.text()
)
}
populate(payload: object) {
this.count = payload.count;
this.next = payload.next;
this.previous = payload.previous;
*payload.results => this.users = payload.results;******
}
Declare an interface which will be used as value object.
export interface IPage
{
count:number;
next_page:string;
prev_page:string;
results:Array<any>;
...
...
}
var my_data:IPage;
Now assign parsed json value to my_data and access all the properties with '.' operator i.e. my_data.count, my_data.results.
Feel free to throw any question.
If I do that, what is the type I should use for my_data?
Its just a javascript object.
As an example if you json looks like:
{
"foo": {
"bar": "bas"
}
}
Then in the parsed json (in variable someObj) the value someObj.foo.bar would be bas 🌹
I am trying to parse a mongodb query in json to dictionary and the JObject.Parse throws exception.
The JSON string is something like below
{ vendor: "xyx", product: { $in : [ /prod1/i, /prod2/i, /prod3/i ] } }
The exception is message is
Error parsing comment. Expected: *, got p. Path 'product.$in', line 1, position 50.
JSON doesn't have support for regular expressions but you could change your JSON string to use the $regex query operator syntax instead:
{ vendor: "xyx", product: { $in: [
{$regex: "prod1", $options: "i"},
{$regex: "prod2", $options: "i"},
{$regex: "prod3", $options: "i"}
] } }
All in one string, of course. And to be valid JSON, the keys all need to be quoted too, but JObject.Parse may allow them to be omitted as it doesn't sound like that part was giving you trouble.
Looks like you didn't mean to say JSON, but rather MongoDB, no?
For MongoDB queries, you need to do this:
{ vendor: "xyx", product: { $in : [ Pattern.compile(/prod1/i), Pattern.compile(/prod2/i), Pattern.compile(/prod3/i) ] } }