Parse mixed string

Hello,

I have a string which I'd like to parse into something meaningful i.e keys & values

The structure of it is a little weird in that it has a couple of lines as the header for each section, which seem to be a repeat of the info in that section. In the sample below, there are 2 sections, beginning with the lines:

  • Predicted class name: truck
  • Predicted class name: excavator

I really just need the 'display_name' and 'score' fields

  • {display_name: "truck", score: 0.9870228171348572}
  • {display_name: "excavator", score: 0.9359530210494995}
  • {display_name: "truck", score: 0.9870228171348572}
  • {display_name: "excavator", score: 0.9359530210494995}

I've tried various nodes, including functions- but if someone could point me in the right direction, I'd be very grateful

Predicted class name: truck
Predicted class score: 0.0
[annotation_spec_id: "481228201931046912"
image_object_detection {
  bounding_box {
    normalized_vertices {
      x: 0.8229872584342957
      y: 0.5793604850769043
    }
    normalized_vertices {
      x: 0.9742650985717773
      y: 0.6732610464096069
    }
  }
  score: 0.9870228171348572
}
display_name: "truck"
, annotation_spec_id: "2041373629094035456"
image_object_detection {
  bounding_box {
    normalized_vertices {
      x: 0.005605409853160381
      y: 0.5414829254150391
    }
    normalized_vertices {
      x: 0.1891111582517624
      y: 0.7010807991027832
    }
  }
  score: 0.9359530210494995
}
display_name: "excavator"
]
Predicted class name: excavator
Predicted class score: 0.0
[annotation_spec_id: "481228201931046912"
image_object_detection {
  bounding_box {
    normalized_vertices {
      x: 0.8229872584342957
      y: 0.5793604850769043
    }
    normalized_vertices {
      x: 0.9742650985717773
      y: 0.6732610464096069
    }
  }
  score: 0.9870228171348572
}
display_name: "truck"
, annotation_spec_id: "2041373629094035456"
image_object_detection {
  bounding_box {
    normalized_vertices {
      x: 0.005605409853160381
      y: 0.5414829254150391
    }
    normalized_vertices {
      x: 0.1891111582517624
      y: 0.7010807991027832
    }
  }
  score: 0.9359530210494995
}
display_name: "excavator"
]

thanks

Andrew

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

Hi @zenofmud

thanks for the quick reply..!

I should have made it clearer- the basic problem is that my data is just one big string.
It's not like JSON or an array where you can get the path etc

There are some lines outside of the square brackets

but thanks for the link- I'll have a look through it

can you copy the value (using the copy value button that appears under your mouse when you hover the debug value) & paste it as code (use the forums </> code button)

I'm confused looking at your data and what you say the output should look like.

You say the first output whould be

How do you know what score goes with what? And should it be based off the 'display_name' or the 'Predicted class name'?

TBH, it looks like wherever that data is coming from (I'm guessing a python program) should really be modified to send clean/proper JSON.

Do you have access to the source program - a far better fix is to produce good JSON instead of parsing this tangle of strings. I'm not saying it isn't possible (you could very will split by line, remove brackets [ { } ], split by :, trim each value, parse the data type to a number, string or object & loop through it all) - just not ideal!

Hi Steve

I think this is what I did in my first post- but I've done it again to be sure
NR seems to display the line break symbols in the debug window & when you click the output, or copy/paste, it is displayed with the lines

thanks

Predicted class name: truck
Predicted class score: 0.0
[annotation_spec_id: "481228201931046912"
image_object_detection {
  bounding_box {
    normalized_vertices {
      x: 0.8229872584342957
      y: 0.5793604850769043
    }
    normalized_vertices {
      x: 0.9742650985717773
      y: 0.6732610464096069
    }
  }
  score: 0.9870228171348572
}
display_name: "truck"
, annotation_spec_id: "2041373629094035456"
image_object_detection {
  bounding_box {
    normalized_vertices {
      x: 0.005605409853160381
      y: 0.5414829254150391
    }
    normalized_vertices {
      x: 0.1891111582517624
      y: 0.7010807991027832
    }
  }
  score: 0.9359530210494995
}
display_name: "excavator"
]
Predicted class name: excavator
Predicted class score: 0.0
[annotation_spec_id: "481228201931046912"
image_object_detection {
  bounding_box {
    normalized_vertices {
      x: 0.8229872584342957
      y: 0.5793604850769043
    }
    normalized_vertices {
      x: 0.9742650985717773
      y: 0.6732610464096069
    }
  }
  score: 0.9870228171348572
}
display_name: "truck"
, annotation_spec_id: "2041373629094035456"
image_object_detection {
  bounding_box {
    normalized_vertices {
      x: 0.005605409853160381
      y: 0.5414829254150391
    }
    normalized_vertices {
      x: 0.1891111582517624
      y: 0.7010807991027832
    }
  }
  score: 0.9359530210494995
}
display_name: "excavator"
]


Hi Steve,

This data is just what a Google API spits out- I have queried it with Google- response pending
This is the API Package google.cloud.automl.v1  |  Cloud AutoML Documentation  |  Google Cloud

There are some input parameters to filter results (such as 'score_threshold', but as far as I can establish, nothing that determines the output format

thanks

Andrew

Could you share a minimal flow that results in this data?

E.g. no dashboard or custom nodes other than the node that you use to communicate your input to GC - use inject or change nodes to prepare the call to GC etc.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.